Skip to content

Commit 5191e3e

Browse files
Automatic Dedicated switching (#93)
1 parent 47728aa commit 5191e3e

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

src/button/button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Logo from "react:./logo-mark.svg"
33
import type { SupportedApplication } from "./button-contributions";
44
import classNames from "classnames";
55
import { STORAGE_KEY_ADDRESS, STORAGE_KEY_NEW_TAB } from "~storage";
6+
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";
67
import { useStorage } from "@plasmohq/storage/hook";
78
import React from "react";
89

@@ -12,7 +13,7 @@ export interface GitpodButtonProps {
1213
}
1314

1415
export const GitpodButton = ({ application, additionalClassNames }: GitpodButtonProps) => {
15-
const [address] = useStorage<string>(STORAGE_KEY_ADDRESS, "https://gitpod.io");
16+
const [address] = useStorage<string>(STORAGE_KEY_ADDRESS, DEFAULT_GITPOD_ENDPOINT);
1617
const [openInNewTab] = useStorage<boolean>(STORAGE_KEY_NEW_TAB, false);
1718

1819
const [showDropdown, setShowDropdown] = useState(false);

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_GITPOD_ENDPOINT = "https://gitpod.io";

src/contents/gitpod-dashboard.ts

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

37
/**
48
* Checks if the current site is a Gitpod instance.
@@ -11,6 +15,23 @@ export const config: PlasmoCSConfig = {
1115
matches: ["https://*/*"]
1216
}
1317

18+
const storage = new Storage();
19+
20+
const automaticallyUpdateEndpoint = async () => {
21+
if (await storage.get<boolean>(STORAGE_AUTOMATICALLY_DETECT_GITPOD) === false) {
22+
return;
23+
}
24+
25+
const currentUserSetEndpoint = await storage.get(STORAGE_KEY_ADDRESS);
26+
if (!currentUserSetEndpoint || currentUserSetEndpoint === DEFAULT_GITPOD_ENDPOINT) {
27+
const currentHost = window.location.host;
28+
if (currentHost !== new URL(DEFAULT_GITPOD_ENDPOINT).host) {
29+
console.log(`Gitpod extension: switching default endpoint to ${currentHost}.`)
30+
await storage.set(STORAGE_KEY_ADDRESS, parseEndpoint(currentHost));
31+
}
32+
}
33+
}
1434
if (isSiteGitpod()) {
1535
sessionStorage.setItem("browser-extension-installed", "true");
36+
automaticallyUpdateEndpoint();
1637
}

src/popup.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useStorage } from "@plasmohq/storage/hook";
22
import { useCallback, useEffect, useState } from "react"
3-
import { STORAGE_KEY_ADDRESS, STORAGE_KEY_NEW_TAB } from "~storage";
3+
import { STORAGE_AUTOMATICALLY_DETECT_GITPOD, STORAGE_KEY_ADDRESS, STORAGE_KEY_NEW_TAB } from "~storage";
44
import { parseEndpoint } from "~utils/parse-endpoint";
55
import React from "react";
66

77
import "./popup.css"
88
import { InputField } from "~components/forms/InputField";
99
import { TextInput } from "~components/forms/TextInputField";
1010
import { CheckboxInputField } from "~components/forms/CheckboxInputField";
11+
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";
1112

1213
function IndexPopup() {
1314
const [error, setError] = useState<string>();
@@ -31,6 +32,7 @@ function IndexPopup() {
3132
}, [storedAddress])
3233

3334
const [openInNewTab, setOpenInNewTab] = useStorage<boolean>(STORAGE_KEY_NEW_TAB, false);
35+
const [automaticallyDetect, setAutomaticallyDetect] = useStorage<boolean>(STORAGE_AUTOMATICALLY_DETECT_GITPOD, true);
3436

3537
return (
3638
<div
@@ -45,7 +47,7 @@ function IndexPopup() {
4547
<form className="w-full">
4648
<InputField
4749
label="Gitpod URL"
48-
hint="Gitpod instance URL, e.g. https://gitpod.io."
50+
hint={`Gitpod instance URL, e.g. ${DEFAULT_GITPOD_ENDPOINT}.`}
4951
topMargin={false}
5052
>
5153
<div className="flex space-x-2">
@@ -60,7 +62,13 @@ function IndexPopup() {
6062
<CheckboxInputField
6163
label="Open Workspaces in a new tab"
6264
checked={openInNewTab}
63-
onChange={(checked) => setOpenInNewTab(checked)}
65+
onChange={setOpenInNewTab}
66+
/>
67+
<CheckboxInputField
68+
label="Automatically switch to Gitpod Dedicated"
69+
hint="Upon visiting a Gitpod Dedicated instance, switch to it"
70+
checked={automaticallyDetect}
71+
onChange={setAutomaticallyDetect}
6472
/>
6573
</form>
6674

src/storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
export const STORAGE_KEY_ADDRESS = "gitpod-installation-address";
3-
export const STORAGE_KEY_NEW_TAB = "gitpod-installation-new-tab";
3+
export const STORAGE_KEY_NEW_TAB = "gitpod-installation-new-tab";
4+
export const STORAGE_AUTOMATICALLY_DETECT_GITPOD = "gitpod-installation-automatically-detect-gitpod";

0 commit comments

Comments
 (0)