Skip to content

Commit 6800526

Browse files
Fix outdated button link on popState (#105)
1 parent 2fb3ba0 commit 6800526

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/button/button.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useEffect, useState, useRef } from "react";
1+
import { useEffect, useState, useRef, useMemo } from "react";
22
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_ALWAYS_OPTIONS, STORAGE_KEY_NEW_TAB } from "~storage";
6-
import { DEFAULT_GITPOD_ENDPOINT } from "~constants";
6+
import { DEFAULT_GITPOD_ENDPOINT, EVENT_CURRENT_URL_CHANGED } from "~constants";
77
import { useStorage } from "@plasmohq/storage/hook";
88
import React from "react";
99

@@ -17,17 +17,30 @@ export const GitpodButton = ({ application, additionalClassNames }: GitpodButton
1717
const [openInNewTab] = useStorage<boolean>(STORAGE_KEY_NEW_TAB, true);
1818
const [disableAutostart] = useStorage<boolean>(STORAGE_KEY_ALWAYS_OPTIONS, false);
1919
const [showDropdown, setShowDropdown] = useState(false);
20+
const [currentHref, setCurrentHref] = useState(window.location.href);
2021

21-
const actions = [
22+
useEffect(() => {
23+
const handleUrlChange = () => {
24+
setCurrentHref(window.location.href)
25+
};
26+
27+
document.addEventListener(EVENT_CURRENT_URL_CHANGED, handleUrlChange);
28+
29+
return () => {
30+
document.removeEventListener(EVENT_CURRENT_URL_CHANGED, handleUrlChange);
31+
};
32+
}, []);
33+
34+
const actions = useMemo(() => [
2235
{
23-
href: `${address}/?autostart=${!disableAutostart}#${window.location.toString()}`,
36+
href: `${address}/?autostart=${!disableAutostart}#${currentHref}`,
2437
label: "Open",
2538
},
2639
{
27-
href: `${address}/?autostart=false#${window.location.toString()}`,
40+
href: `${address}/?autostart=false#${currentHref}`,
2841
label: "Open with options...",
2942
},
30-
];
43+
], [address, disableAutostart, currentHref]);
3144
const dropdownRef = useRef<HTMLDivElement | null>(null);
3245
const firstActionRef = useRef<HTMLAnchorElement | null>(null);
3346

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const DEFAULT_GITPOD_ENDPOINT = "https://gitpod.io";
2-
export const ALL_ORIGINS_WILDCARD = "*://*/*";
2+
export const ALL_ORIGINS_WILDCARD = "*://*/*";
3+
export const EVENT_CURRENT_URL_CHANGED = "current-url-changed";

src/contents/button.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PlasmoCSConfig, PlasmoGetInlineAnchor } from "plasmo";
33
import React, { type ReactElement } from "react";
44
import { GitpodButton } from "../button/button";
55
import { buttonContributions, isSiteSuitable, type ButtonContributionParams } from "../button/button-contributions";
6+
import { EVENT_CURRENT_URL_CHANGED } from "~constants";
67

78
export const config: PlasmoCSConfig = {
89
matches: [
@@ -26,6 +27,8 @@ class ButtonContributionManager {
2627
anchor: HTMLElement,
2728
}
2829

30+
private currentHref = window.location.href;
31+
2932
_disabled = false;
3033

3134
constructor(private contributions: ButtonContributionParams[]) {
@@ -45,7 +48,7 @@ class ButtonContributionManager {
4548
}
4649

4750
private getContainerId(contribution: ButtonContributionParams) {
48-
return "gp-btn-cnt-" + contribution.application + contribution.additionalClassNames?.map(c => "-" + c)?.join("");
51+
return `gp-btn-cnt-${contribution.application}${contribution.additionalClassNames?.map(c => "-" + c)?.join("")}`;
4952
}
5053

5154
private updateActive(active?: { contribution: ButtonContributionParams, anchor: HTMLElement }) {
@@ -63,6 +66,12 @@ class ButtonContributionManager {
6366
return null;
6467
}
6568

69+
if (this.currentHref !== window.location.href) {
70+
this.currentHref = window.location.href;
71+
const event = new CustomEvent(EVENT_CURRENT_URL_CHANGED);
72+
document.dispatchEvent(event);
73+
}
74+
6675
for (const contribution of this.contributions) {
6776
const isActive = this.isActive(contribution);
6877
if (isActive) {

0 commit comments

Comments
 (0)