@@ -10,18 +10,31 @@ function ErrorContainer(props: ErrorContainerProps): JSX.Element {
10
10
( state : RootState ) => state . main ,
11
11
) ;
12
12
13
+ // Helper function to check if a URL is localhost
14
+ const isLocalhost = ( url : string ) : boolean => {
15
+ return url . startsWith ( 'http://localhost:' ) || url . startsWith ( 'https://localhost:' ) ;
16
+ } ;
17
+
13
18
// Add effect to initialize currentTab if not set
14
19
useEffect ( ( ) => {
15
20
const initializeCurrentTab = async ( ) => {
16
21
if ( ! currentTab ) {
17
22
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 ) ) ;
23
+ // Query specifically for localhost tabs first
24
+ const tabs = await chrome . tabs . query ( { currentWindow : true } ) ;
25
+ const localhostTab = tabs . find ( tab => tab . url && isLocalhost ( tab . url ) ) ;
26
+
27
+ if ( localhostTab ?. id ) {
28
+ dispatch ( setTab ( localhostTab . id ) ) ;
29
+ } else {
30
+ // Fallback to active tab if no localhost found
31
+ const [ activeTab ] = await chrome . tabs . query ( { active : true , currentWindow : true } ) ;
32
+ if ( activeTab ?. id ) {
33
+ dispatch ( setTab ( activeTab . id ) ) ;
34
+ }
22
35
}
23
36
} catch ( error ) {
24
- console . error ( 'Error getting active tab:' , error ) ;
37
+ console . error ( 'Error getting tab:' , error ) ;
25
38
}
26
39
}
27
40
} ;
@@ -30,52 +43,58 @@ function ErrorContainer(props: ErrorContainerProps): JSX.Element {
30
43
} , [ currentTab , dispatch ] ) ;
31
44
32
45
// function that launches the main app and refreshes the page
33
- function launch ( ) : void {
34
- // Add validation to ensure we have valid data
35
- if ( ! currentTab ) {
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 ) ;
46
+ async function launch ( ) : Promise < void > {
47
+ try {
48
+ // If no current tab, try to find localhost tab first
49
+ if ( ! currentTab ) {
50
+ const tabs = await chrome . tabs . query ( { currentWindow : true } ) ;
51
+ const localhostTab = tabs . find ( tab => tab . url && isLocalhost ( tab . url ) ) ;
52
+
53
+ if ( localhostTab ?. id ) {
54
+ dispatch ( setTab ( localhostTab . id ) ) ;
55
+ await initializeLaunch ( localhostTab . id ) ;
56
+ } else {
57
+ console . warn ( 'No localhost tab found' ) ;
55
58
}
56
- } ) ;
57
- return ;
58
- }
59
+ return ;
60
+ }
59
61
60
- if ( ! tabs || ! tabs [ currentTab ] ) {
61
- // If no tab data exists, create a minimal valid payload
62
- const defaultPayload = {
63
- status : {
64
- contentScriptLaunched : false ,
65
- reactDevToolsInstalled : false ,
66
- targetPageisaReactApp : false ,
67
- } ,
68
- } ;
69
- dispatch ( launchContentScript ( defaultPayload ) ) ;
70
- } else {
71
- dispatch ( launchContentScript ( tabs [ currentTab ] ) ) ;
62
+ // Verify current tab is still localhost
63
+ const tab = await chrome . tabs . get ( currentTab ) ;
64
+ if ( ! tab . url || ! isLocalhost ( tab . url ) ) {
65
+ // Try to find a localhost tab
66
+ const tabs = await chrome . tabs . query ( { currentWindow : true } ) ;
67
+ const localhostTab = tabs . find ( tab => tab . url && isLocalhost ( tab . url ) ) ;
68
+
69
+ if ( localhostTab ?. id ) {
70
+ dispatch ( setTab ( localhostTab . id ) ) ;
71
+ await initializeLaunch ( localhostTab . id ) ;
72
+ } else {
73
+ console . warn ( 'No localhost tab found' ) ;
74
+ }
75
+ return ;
76
+ }
77
+
78
+ await initializeLaunch ( currentTab ) ;
79
+ } catch ( error ) {
80
+ console . error ( 'Error during launch:' , error ) ;
72
81
}
82
+ }
73
83
84
+ async function initializeLaunch ( tabId : number ) : Promise < void > {
85
+ const defaultPayload = {
86
+ status : {
87
+ contentScriptLaunched : false ,
88
+ reactDevToolsInstalled : false ,
89
+ targetPageisaReactApp : false ,
90
+ } ,
91
+ } ;
92
+
93
+ dispatch ( launchContentScript ( defaultPayload ) ) ;
94
+
74
95
// Allow the dispatch to complete before refreshing
75
96
setTimeout ( ( ) => {
76
- if ( currentTab ) {
77
- chrome . tabs . reload ( currentTab ) ;
78
- }
97
+ chrome . tabs . reload ( tabId ) ;
79
98
} , 100 ) ;
80
99
}
81
100
@@ -96,8 +115,8 @@ function ErrorContainer(props: ErrorContainerProps): JSX.Element {
96
115
connect with your app and start monitoring state changes.
97
116
</ p >
98
117
< p className = 'error-description' >
99
- Important: Reactime requires React Developer Tools to be installed. If you haven't
100
- already, please{ ' ' }
118
+ Important: Reactime requires React Developer Tools to be installed and will only track state
119
+ changes on localhost development servers. If you haven't already, please{ ' ' }
101
120
< a
102
121
href = 'https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en'
103
122
target = '_blank'
@@ -134,4 +153,4 @@ function ErrorContainer(props: ErrorContainerProps): JSX.Element {
134
153
) ;
135
154
}
136
155
137
- export default ErrorContainer ;
156
+ export default ErrorContainer ;
0 commit comments