diff --git a/package.json b/package.json index 44b6b7d..18bf232 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,5 @@ "overrides": { "sharp": "0.33.4" } - }, - "packageManager": "pnpm@9.14.2" + } } diff --git a/src/button/button.tsx b/src/button/button.tsx index 2c28108..f41d6c9 100644 --- a/src/button/button.tsx +++ b/src/button/button.tsx @@ -4,6 +4,7 @@ import { useHotkeys } from "react-hotkeys-hook"; import Logo from "react:./logo-mark.svg"; import { useStorage } from "@plasmohq/storage/hook"; +import { Storage } from "@plasmohq/storage"; import { DEFAULT_GITPOD_ENDPOINT, EVENT_CURRENT_URL_CHANGED } from "~constants"; import { STORAGE_KEY_ADDRESS, STORAGE_KEY_ALWAYS_OPTIONS, STORAGE_KEY_NEW_TAB } from "~storage"; @@ -17,7 +18,7 @@ type Props = { urlTransformer?: (url: string) => string; }; export const GitpodButton = ({ application, additionalClassNames, urlTransformer }: Props) => { - const [address] = useStorage(STORAGE_KEY_ADDRESS, DEFAULT_GITPOD_ENDPOINT); + const [address] = useStorage(STORAGE_KEY_ADDRESS); const [openInNewTab] = useStorage(STORAGE_KEY_NEW_TAB, true); const [disableAutostart] = useStorage(STORAGE_KEY_ALWAYS_OPTIONS, false); const [showDropdown, setShowDropdown] = useState(false); @@ -37,6 +38,18 @@ export const GitpodButton = ({ application, additionalClassNames, urlTransformer }; }, []); + // if the user has no address configured, set it to the default endpoint + useEffect(() => { + (async () => { + // 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 + const storage = new Storage(); + const persistedAddress = await storage.get(STORAGE_KEY_ADDRESS); + if (!persistedAddress) { + await storage.set(STORAGE_KEY_ADDRESS, DEFAULT_GITPOD_ENDPOINT); + } + })(); + }, []); + const actions = useMemo(() => { const parsedHref = !urlTransformer ? currentHref : urlTransformer(currentHref); return [ diff --git a/src/contents/gitpod-dashboard.ts b/src/contents/gitpod-dashboard.ts index 523768b..6384ef3 100644 --- a/src/contents/gitpod-dashboard.ts +++ b/src/contents/gitpod-dashboard.ts @@ -1,8 +1,5 @@ -import type { PlasmoCSConfig } from "plasmo"; - import { Storage } from "@plasmohq/storage"; - -import { DEFAULT_GITPOD_ENDPOINT } from "~constants"; +import type { PlasmoCSConfig } from "plasmo"; import { STORAGE_AUTOMATICALLY_DETECT_GITPOD, STORAGE_KEY_ADDRESS } from "~storage"; import { parseEndpoint } from "~utils/parse-endpoint"; @@ -25,15 +22,10 @@ const automaticallyUpdateEndpoint = async () => { } const currentHost = window.location.host; - if (currentHost !== new URL(DEFAULT_GITPOD_ENDPOINT).host) { - const currentlyStoredEndpoint = await storage.get(STORAGE_KEY_ADDRESS); - if ( - (currentlyStoredEndpoint && new URL(currentlyStoredEndpoint).host !== currentHost) || - !currentlyStoredEndpoint - ) { - console.log(`Gitpod extension: switching default endpoint to ${currentHost}.`); - await storage.set(STORAGE_KEY_ADDRESS, parseEndpoint(window.location.origin)); - } + const currentlyStoredEndpoint = await storage.get(STORAGE_KEY_ADDRESS); + if (!currentlyStoredEndpoint || new URL(currentlyStoredEndpoint).host !== currentHost) { + console.log(`Gitpod extension: switching default endpoint to ${currentHost}.`); + await storage.set(STORAGE_KEY_ADDRESS, parseEndpoint(window.location.origin)); } };