1
- import { useEffect , useState , useRef } from "react" ;
1
+ import { useEffect , useState , useRef , useMemo } from "react" ;
2
2
import Logo from "react:./logo-mark.svg"
3
3
import type { SupportedApplication } from "./button-contributions" ;
4
4
import classNames from "classnames" ;
5
5
import { STORAGE_KEY_ADDRESS , STORAGE_KEY_ALWAYS_OPTIONS , STORAGE_KEY_NEW_TAB } from "~storage" ;
6
- import { DEFAULT_GITPOD_ENDPOINT } from "~constants" ;
6
+ import { DEFAULT_GITPOD_ENDPOINT , EVENT_CURRENT_URL_CHANGED } from "~constants" ;
7
7
import { useStorage } from "@plasmohq/storage/hook" ;
8
8
import React from "react" ;
9
9
@@ -17,17 +17,30 @@ export const GitpodButton = ({ application, additionalClassNames }: GitpodButton
17
17
const [ openInNewTab ] = useStorage < boolean > ( STORAGE_KEY_NEW_TAB , true ) ;
18
18
const [ disableAutostart ] = useStorage < boolean > ( STORAGE_KEY_ALWAYS_OPTIONS , false ) ;
19
19
const [ showDropdown , setShowDropdown ] = useState ( false ) ;
20
+ const [ currentHref , setCurrentHref ] = useState ( window . location . href ) ;
20
21
21
- const actions = [
22
+ useEffect ( ( ) => {
23
+ const handleUrlChange = ( ) => {
24
+ setCurrentHref ( window . location . href )
25
+ } ;
26
+
27
+ document . addEventListener ( EVENT_CURRENT_URL_CHANGED , handleUrlChange ) ;
28
+
29
+ return ( ) => {
30
+ document . removeEventListener ( EVENT_CURRENT_URL_CHANGED , handleUrlChange ) ;
31
+ } ;
32
+ } , [ ] ) ;
33
+
34
+ const actions = useMemo ( ( ) => [
22
35
{
23
- href : `${ address } /?autostart=${ ! disableAutostart } #${ window . location . toString ( ) } ` ,
36
+ href : `${ address } /?autostart=${ ! disableAutostart } #${ currentHref } ` ,
24
37
label : "Open" ,
25
38
} ,
26
39
{
27
- href : `${ address } /?autostart=false#${ window . location . toString ( ) } ` ,
40
+ href : `${ address } /?autostart=false#${ currentHref } ` ,
28
41
label : "Open with options..." ,
29
42
} ,
30
- ] ;
43
+ ] , [ address , disableAutostart , currentHref ] ) ;
31
44
const dropdownRef = useRef < HTMLDivElement | null > ( null ) ;
32
45
const firstActionRef = useRef < HTMLAnchorElement | null > ( null ) ;
33
46
0 commit comments