Skip to content

Commit 576981a

Browse files
Set an explicit default Gitpod host for existing users (#192)
1 parent 100a2f3 commit 576981a

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,5 @@
8282
"overrides": {
8383
"sharp": "0.33.4"
8484
}
85-
},
86-
"packageManager": "[email protected]"
85+
}
8786
}

src/button/button.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useHotkeys } from "react-hotkeys-hook";
44
import Logo from "react:./logo-mark.svg";
55

66
import { useStorage } from "@plasmohq/storage/hook";
7+
import { Storage } from "@plasmohq/storage";
78

89
import { DEFAULT_GITPOD_ENDPOINT, EVENT_CURRENT_URL_CHANGED } from "~constants";
910
import { STORAGE_KEY_ADDRESS, STORAGE_KEY_ALWAYS_OPTIONS, STORAGE_KEY_NEW_TAB } from "~storage";
@@ -17,7 +18,7 @@ type Props = {
1718
urlTransformer?: (url: string) => string;
1819
};
1920
export const GitpodButton = ({ application, additionalClassNames, urlTransformer }: Props) => {
20-
const [address] = useStorage<string>(STORAGE_KEY_ADDRESS, DEFAULT_GITPOD_ENDPOINT);
21+
const [address] = useStorage<string>(STORAGE_KEY_ADDRESS);
2122
const [openInNewTab] = useStorage<boolean>(STORAGE_KEY_NEW_TAB, true);
2223
const [disableAutostart] = useStorage<boolean>(STORAGE_KEY_ALWAYS_OPTIONS, false);
2324
const [showDropdown, setShowDropdown] = useState(false);
@@ -37,6 +38,18 @@ export const GitpodButton = ({ application, additionalClassNames, urlTransformer
3738
};
3839
}, []);
3940

41+
// if the user has no address configured, set it to the default endpoint
42+
useEffect(() => {
43+
(async () => {
44+
// we don't use the useStorage hook because it does not offer a way see if the value is set (it could just be loading), meaning we could end up setting the default endpoint even if it's already set
45+
const storage = new Storage();
46+
const persistedAddress = await storage.get(STORAGE_KEY_ADDRESS);
47+
if (!persistedAddress) {
48+
await storage.set(STORAGE_KEY_ADDRESS, DEFAULT_GITPOD_ENDPOINT);
49+
}
50+
})();
51+
}, []);
52+
4053
const actions = useMemo(() => {
4154
const parsedHref = !urlTransformer ? currentHref : urlTransformer(currentHref);
4255
return [

src/contents/gitpod-dashboard.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import type { PlasmoCSConfig } from "plasmo";
2-
31
import { Storage } from "@plasmohq/storage";
4-
5-
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";
2+
import type { PlasmoCSConfig } from "plasmo";
63
import { STORAGE_AUTOMATICALLY_DETECT_GITPOD, STORAGE_KEY_ADDRESS } from "~storage";
74
import { parseEndpoint } from "~utils/parse-endpoint";
85

@@ -25,15 +22,10 @@ const automaticallyUpdateEndpoint = async () => {
2522
}
2623

2724
const currentHost = window.location.host;
28-
if (currentHost !== new URL(DEFAULT_GITPOD_ENDPOINT).host) {
29-
const currentlyStoredEndpoint = await storage.get<string>(STORAGE_KEY_ADDRESS);
30-
if (
31-
(currentlyStoredEndpoint && new URL(currentlyStoredEndpoint).host !== currentHost) ||
32-
!currentlyStoredEndpoint
33-
) {
34-
console.log(`Gitpod extension: switching default endpoint to ${currentHost}.`);
35-
await storage.set(STORAGE_KEY_ADDRESS, parseEndpoint(window.location.origin));
36-
}
25+
const currentlyStoredEndpoint = await storage.get<string>(STORAGE_KEY_ADDRESS);
26+
if (!currentlyStoredEndpoint || new URL(currentlyStoredEndpoint).host !== currentHost) {
27+
console.log(`Gitpod extension: switching default endpoint to ${currentHost}.`);
28+
await storage.set(STORAGE_KEY_ADDRESS, parseEndpoint(window.location.origin));
3729
}
3830
};
3931

0 commit comments

Comments
 (0)