1
- import React from 'react' ;
1
+ import React , { useEffect } from 'react' ;
2
2
import { useDispatch , useSelector } from 'react-redux' ;
3
- import { launchContentScript } from '../slices/mainSlice' ;
3
+ import { launchContentScript , setTab } from '../slices/mainSlice' ;
4
4
import { MainState , RootState , ErrorContainerProps } from '../FrontendTypes' ;
5
5
import { RefreshCw , Github , PlayCircle } from 'lucide-react' ;
6
6
@@ -10,11 +10,50 @@ function ErrorContainer(props: ErrorContainerProps): JSX.Element {
10
10
( state : RootState ) => state . main ,
11
11
) ;
12
12
13
+ // Add effect to initialize currentTab if not set
14
+ useEffect ( ( ) => {
15
+ const initializeCurrentTab = async ( ) => {
16
+ if ( ! currentTab ) {
17
+ try {
18
+ // Query for the active tab
19
+ const [ activeTab ] = await chrome . tabs . query ( { active : true , currentWindow : true } ) ;
20
+ if ( activeTab ?. id ) {
21
+ dispatch ( setTab ( activeTab . id ) ) ;
22
+ }
23
+ } catch ( error ) {
24
+ console . error ( 'Error getting active tab:' , error ) ;
25
+ }
26
+ }
27
+ } ;
28
+
29
+ initializeCurrentTab ( ) ;
30
+ } , [ currentTab , dispatch ] ) ;
31
+
13
32
// function that launches the main app and refreshes the page
14
33
function launch ( ) : void {
15
34
// Add validation to ensure we have valid data
16
35
if ( ! currentTab ) {
17
- console . warn ( 'No current tab available' ) ;
36
+ console . warn ( 'No current tab available - attempting to get active tab' ) ;
37
+ // Try to get the active tab when launching
38
+ chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
39
+ if ( tabs [ 0 ] ?. id ) {
40
+ const activeTabId = tabs [ 0 ] . id ;
41
+ dispatch ( setTab ( activeTabId ) ) ;
42
+ // Create default payload and launch
43
+ const defaultPayload = {
44
+ status : {
45
+ contentScriptLaunched : false ,
46
+ reactDevToolsInstalled : false ,
47
+ targetPageisaReactApp : false ,
48
+ } ,
49
+ } ;
50
+ dispatch ( launchContentScript ( defaultPayload ) ) ;
51
+ // Allow the dispatch to complete before refreshing
52
+ setTimeout ( ( ) => {
53
+ chrome . tabs . reload ( activeTabId ) ;
54
+ } , 100 ) ;
55
+ }
56
+ } ) ;
18
57
return ;
19
58
}
20
59
0 commit comments