Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -17,7 +18,7 @@ type Props = {
urlTransformer?: (url: string) => string;
};
export const GitpodButton = ({ application, additionalClassNames, urlTransformer }: Props) => {
const [address] = useStorage<string>(STORAGE_KEY_ADDRESS, DEFAULT_GITPOD_ENDPOINT);
const [address] = useStorage<string>(STORAGE_KEY_ADDRESS);
const [openInNewTab] = useStorage<boolean>(STORAGE_KEY_NEW_TAB, true);
const [disableAutostart] = useStorage<boolean>(STORAGE_KEY_ALWAYS_OPTIONS, false);
const [showDropdown, setShowDropdown] = useState(false);
Expand All @@ -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);
}
})();
}, [address]);

const actions = useMemo(() => {
const parsedHref = !urlTransformer ? currentHref : urlTransformer(currentHref);
return [
Expand Down
18 changes: 5 additions & 13 deletions src/contents/gitpod-dashboard.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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<string>(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<string>(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));
}
};

Expand Down
Loading