Skip to content

Commit

Permalink
Automatic Dedicated switching (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek authored Sep 27, 2023
1 parent 47728aa commit 5191e3e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Logo from "react:./logo-mark.svg"
import type { SupportedApplication } from "./button-contributions";
import classNames from "classnames";
import { STORAGE_KEY_ADDRESS, STORAGE_KEY_NEW_TAB } from "~storage";
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";
import { useStorage } from "@plasmohq/storage/hook";
import React from "react";

Expand All @@ -12,7 +13,7 @@ export interface GitpodButtonProps {
}

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

const [showDropdown, setShowDropdown] = useState(false);
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_GITPOD_ENDPOINT = "https://gitpod.io";
21 changes: 21 additions & 0 deletions src/contents/gitpod-dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { PlasmoCSConfig } from "plasmo";
import { Storage } from "@plasmohq/storage";
import { STORAGE_AUTOMATICALLY_DETECT_GITPOD, STORAGE_KEY_ADDRESS } from "~storage";
import { parseEndpoint } from "~utils/parse-endpoint";
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";

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

const storage = new Storage();

const automaticallyUpdateEndpoint = async () => {
if (await storage.get<boolean>(STORAGE_AUTOMATICALLY_DETECT_GITPOD) === false) {
return;
}

const currentUserSetEndpoint = await storage.get(STORAGE_KEY_ADDRESS);
if (!currentUserSetEndpoint || currentUserSetEndpoint === DEFAULT_GITPOD_ENDPOINT) {
const currentHost = window.location.host;
if (currentHost !== new URL(DEFAULT_GITPOD_ENDPOINT).host) {
console.log(`Gitpod extension: switching default endpoint to ${currentHost}.`)
await storage.set(STORAGE_KEY_ADDRESS, parseEndpoint(currentHost));
}
}
}
if (isSiteGitpod()) {
sessionStorage.setItem("browser-extension-installed", "true");
automaticallyUpdateEndpoint();
}
14 changes: 11 additions & 3 deletions src/popup.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useStorage } from "@plasmohq/storage/hook";
import { useCallback, useEffect, useState } from "react"
import { STORAGE_KEY_ADDRESS, STORAGE_KEY_NEW_TAB } from "~storage";
import { STORAGE_AUTOMATICALLY_DETECT_GITPOD, STORAGE_KEY_ADDRESS, STORAGE_KEY_NEW_TAB } from "~storage";
import { parseEndpoint } from "~utils/parse-endpoint";
import React from "react";

import "./popup.css"
import { InputField } from "~components/forms/InputField";
import { TextInput } from "~components/forms/TextInputField";
import { CheckboxInputField } from "~components/forms/CheckboxInputField";
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";

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

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

return (
<div
Expand All @@ -45,7 +47,7 @@ function IndexPopup() {
<form className="w-full">
<InputField
label="Gitpod URL"
hint="Gitpod instance URL, e.g. https://gitpod.io."
hint={`Gitpod instance URL, e.g. ${DEFAULT_GITPOD_ENDPOINT}.`}
topMargin={false}
>
<div className="flex space-x-2">
Expand All @@ -60,7 +62,13 @@ function IndexPopup() {
<CheckboxInputField
label="Open Workspaces in a new tab"
checked={openInNewTab}
onChange={(checked) => setOpenInNewTab(checked)}
onChange={setOpenInNewTab}
/>
<CheckboxInputField
label="Automatically switch to Gitpod Dedicated"
hint="Upon visiting a Gitpod Dedicated instance, switch to it"
checked={automaticallyDetect}
onChange={setAutomaticallyDetect}
/>
</form>

Expand Down
3 changes: 2 additions & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

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

0 comments on commit 5191e3e

Please sign in to comment.