From e284c95891fb6dc71d241eaeecf7e690cab4953e Mon Sep 17 00:00:00 2001 From: LukeGus Date: Mon, 19 Oct 2026 17:07:07 -0500 Subject: [PATCH] fix: add a neutral linked desktop hook for plugins (#1580) --- packages/plugin-sdk/ui.api.txt | 1 + scripts/check-sdk-ui-exports.cjs | 1 + src/ui/lib/linked-server.ts | 20 ++++++++++++++++++++ src/ui/plugin-host/sdk-ui.ts | 2 +- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/plugin-sdk/ui.api.txt b/packages/plugin-sdk/ui.api.txt index 501cb2113..62fec6f18 100644 --- a/packages/plugin-sdk/ui.api.txt +++ b/packages/plugin-sdk/ui.api.txt @@ -584,6 +584,7 @@ useChooseTheme (value): () => (themeId: string) => void useConfirm (value): () => (options: ConfirmOptions) => Promise useConnectionGate (value): () => ConnectionGateController useConnectionLog (value): () => ConnectionLogContextType +useDesktopLinked (value): () => boolean useDesktopUpdate (value): () => DesktopUpdateState useEditorDefaultKeys (value): () => Map useHostActions (value): () => HostActionDef[] diff --git a/scripts/check-sdk-ui-exports.cjs b/scripts/check-sdk-ui-exports.cjs index c7ecb49c4..bd2a540cb 100644 --- a/scripts/check-sdk-ui-exports.cjs +++ b/scripts/check-sdk-ui-exports.cjs @@ -605,6 +605,7 @@ const ALLOWED = [ "useConfirm", "useConnectionGate", "useConnectionLog", + "useDesktopLinked", "useDesktopUpdate", "useEditorDefaultKeys", "useHostActions", diff --git a/src/ui/lib/linked-server.ts b/src/ui/lib/linked-server.ts index 6435c8775..4634105bd 100644 --- a/src/ui/lib/linked-server.ts +++ b/src/ui/lib/linked-server.ts @@ -1,3 +1,4 @@ +import { useEffect, useState } from "react"; import { isElectron } from "@/lib/electron"; import { authApi } from "@/main-axios"; import { SYNC_CHANGED_EVENT } from "@/lib/sync-events"; @@ -48,3 +49,22 @@ export function notifySyncChanged(): void { cached = null; window.dispatchEvent(new CustomEvent(SYNC_CHANGED_EVENT)); } + +/** Whether this desktop is linked to a server, kept current as it changes. */ +export function useDesktopLinked(): boolean { + const [linked, setLinked] = useState(false); + useEffect(() => { + let live = true; + const read = () => + void getLinkedSession().then((session) => { + if (live) setLinked(!!session); + }); + read(); + window.addEventListener(SYNC_CHANGED_EVENT, read); + return () => { + live = false; + window.removeEventListener(SYNC_CHANGED_EVENT, read); + }; + }, []); + return linked; +} diff --git a/src/ui/plugin-host/sdk-ui.ts b/src/ui/plugin-host/sdk-ui.ts index 8245fcf7c..c6a9db34b 100644 --- a/src/ui/plugin-host/sdk-ui.ts +++ b/src/ui/plugin-host/sdk-ui.ts @@ -574,7 +574,7 @@ export { invalidateServerStatusCache, } from "@/lib/hosts-request-cache"; export { reportBetaFeedbackUrl, reportCoreIssueUrl } from "@/lib/issue-url"; -export { notifySyncChanged } from "@/lib/linked-server"; +export { notifySyncChanged, useDesktopLinked } from "@/lib/linked-server"; export { clearLocalAdaptivePreferences, getPreferredHostAction,