diff --git a/apps/web/src/components/ProviderUpdateLaunchNotification.environments.ts b/apps/web/src/components/ProviderUpdateLaunchNotification.environments.ts index 1b7ec85e23..b6b4bdd7f6 100644 --- a/apps/web/src/components/ProviderUpdateLaunchNotification.environments.ts +++ b/apps/web/src/components/ProviderUpdateLaunchNotification.environments.ts @@ -3,10 +3,10 @@ import type { ServerConfig } from "@t3tools/contracts"; import { useMemo } from "react"; import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments"; +import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; import { buildLocalEnvironmentUpdateGroups, deriveEnvironmentDisplayLabel, - parseWslDistroFromInstanceId, type EnvironmentUpdateConnectionState, type LocalEnvironmentProvidersInput, type LocalEnvironmentUpdateGroup, @@ -19,10 +19,7 @@ import { * connection id. SSH, relay, and other remote targets are excluded. */ function isLocalConnectionTarget(target: ConnectionCatalogEntry["target"]): boolean { - return ( - target._tag === "PrimaryConnectionTarget" || - (target._tag === "BearerConnectionTarget" && target.connectionId.startsWith("local:")) - ); + return target._tag === "PrimaryConnectionTarget" || isDesktopLocalConnectionTarget(target); } function normalizeConnectionState( @@ -46,25 +43,6 @@ function normalizeConnectionState( } } -/** - * The stable backend instance id of a desktop-local secondary (e.g. - * "wsl:ubuntu"), recovered from the desktop bootstrap topology by matching the - * environment's loopback URL. Used only to derive the WSL distro label; absence - * is fine because labeling falls back to the reported platform OS. - */ -function localBackendInstanceId(displayUrl: string | null): string | undefined { - if (displayUrl === null) { - return undefined; - } - try { - const bootstraps = window.desktopBridge?.getLocalEnvironmentBootstraps() ?? []; - const match = bootstraps.find((entry) => entry.httpBaseUrl === displayUrl); - return match?.id; - } catch { - return undefined; - } -} - /** * Reactively enumerate the enabled local environments (the primary plus any * desktop-local secondary such as WSL) with each one's full provider list and a @@ -88,20 +66,20 @@ export function useLocalEnvironmentUpdateGroups(): { const isPrimary = environment.environmentId === primaryEnvironmentId; const serverConfig: ServerConfig | null = environment.serverConfig; - const instanceId = isPrimary - ? undefined - : localBackendInstanceId(environment.displayUrl); - const wslDistro = parseWslDistroFromInstanceId(instanceId); inputs.push({ environmentId: environment.environmentId, - // Label by platform so the row reads "Windows"/"WSL", not the account name. - label: deriveEnvironmentDisplayLabel({ - isWsl: instanceId?.startsWith("wsl:") === true, - wslDistro, - platformOs: serverConfig?.environment.platform.os, - fallbackLabel: environment.label, - }), + // Secondaries carry a meaningful label straight from the platform source + // (e.g. "WSL (Ubuntu)"). The primary's catalog label can be the account + // name, so fall back to its platform OS so the row reads "Windows"/"Linux". + label: isPrimary + ? deriveEnvironmentDisplayLabel({ + isWsl: false, + wslDistro: null, + platformOs: serverConfig?.environment.platform.os, + fallbackLabel: environment.label, + }) + : environment.label, isPrimary, // The primary is the backend serving this renderer, so it is ready // whenever its providers are available; secondaries report their live diff --git a/apps/web/src/components/ProviderUpdateLaunchNotification.tsx b/apps/web/src/components/ProviderUpdateLaunchNotification.tsx index 9af9d2c3a4..23c02b571d 100644 --- a/apps/web/src/components/ProviderUpdateLaunchNotification.tsx +++ b/apps/web/src/components/ProviderUpdateLaunchNotification.tsx @@ -3,6 +3,7 @@ import { DownloadIcon } from "lucide-react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useEnvironments } from "~/state/environments"; +import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; import { useDismissedProviderUpdateNotificationKeys } from "../providerUpdateDismissal"; import { ProviderUpdateEnvironmentRows } from "./ProviderUpdateEnvironmentRows"; import { useLocalEnvironmentUpdateGroups } from "./ProviderUpdateLaunchNotification.environments"; @@ -26,10 +27,8 @@ function useHasLocalSecondaryEnvironment(): boolean { const { environments } = useEnvironments(); return useMemo( () => - environments.some( - (environment) => - environment.entry.target._tag === "BearerConnectionTarget" && - environment.entry.target.connectionId.startsWith("local:"), + environments.some((environment) => + isDesktopLocalConnectionTarget(environment.entry.target), ), [environments], ); diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 9163dc7a08..196d97ca60 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -69,7 +69,8 @@ import { type SidebarThreadPreviewCount, type SidebarThreadSortOrder, } from "@t3tools/contracts/settings"; -import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts"; +import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal"; +import { useDesktopLocalBootstraps } from "../connection/useDesktopLocalBootstraps"; import { isElectron } from "../env"; import { APP_STAGE_LABEL, APP_VERSION } from "../branding"; import { isTerminalFocused } from "../lib/terminalFocus"; @@ -402,8 +403,7 @@ export const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThr // suppress the cloud icon (the project header already shows a container icon // for desktop-local projects, see sidebarProjectGrouping). const isDesktopLocalThread = - environment?.entry.target._tag === "BearerConnectionTarget" && - environment.entry.target.connectionId.startsWith("local:"); + environment !== null && isDesktopLocalConnectionTarget(environment.entry.target); const threadEnvironmentLabel = isRemoteThread ? (remoteEnvLabel ?? (isDesktopLocalThread ? "Local" : "Remote")) : null; @@ -2487,49 +2487,21 @@ const SidebarProjectListRow = memo(function SidebarProjectListRow(props: Sidebar ); }); -const PRIMARY_BOOTSTRAP_ID = "primary"; - -function readDesktopSecondaryBootstraps(): ReadonlyArray { - const bridge = window.desktopBridge; - if (bridge === undefined) { - return []; - } - try { - return bridge - .getLocalEnvironmentBootstraps() - .filter((entry) => entry.id !== PRIMARY_BOOTSTRAP_ID); - } catch { - return []; - } -} - function LocalSecondaryStatus() { const { environments } = useEnvironments(); // The desktop reports which local secondary backends (e.g. the WSL backend) - // exist; we poll because the bridge has no change event. A backend that is - // still cold-booting has no httpBaseUrl yet and isn't in the catalog, so we + // exist; the hook polls because the bridge has no change event. A backend that + // is still cold-booting has no httpBaseUrl yet and isn't in the catalog, so we // surface "Connecting" straight from the bootstrap list and clear it once the // matching environment reports a connected phase. - const [secondaries, setSecondaries] = useState>( - readDesktopSecondaryBootstraps, - ); - useEffect(() => { - const read = () => setSecondaries(readDesktopSecondaryBootstraps()); - read(); - const interval = setInterval(read, 2000); - return () => clearInterval(interval); - }, []); + const secondaries = useDesktopLocalBootstraps(); // Connected desktop-local environments keyed by their backend URL so we can // match a bootstrap (which only knows the URL) to its connection phase. const localEnvByUrl = useMemo(() => { const map = new Map(); for (const environment of environments) { - if ( - environment.entry.target._tag === "BearerConnectionTarget" && - environment.entry.target.connectionId.startsWith("local:") && - environment.displayUrl !== null - ) { + if (isDesktopLocalConnectionTarget(environment.entry.target) && environment.displayUrl !== null) { map.set(environment.displayUrl, { phase: environment.connection.phase, error: environment.connection.error, @@ -3192,11 +3164,7 @@ export default function Sidebar() { () => new Set( environments - .filter( - (environment) => - environment.entry.target._tag === "BearerConnectionTarget" && - environment.entry.target.connectionId.startsWith("local:"), - ) + .filter((environment) => isDesktopLocalConnectionTarget(environment.entry.target)) .map((environment) => environment.environmentId), ), [environments], diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 20f0c3ee8f..b2329bc878 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -113,6 +113,7 @@ import { type ServerClientSessionRecord, type ServerPairingLinkRecord, } from "~/environments/primary"; +import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; import { useUiStateStore } from "~/uiStateStore"; import { resolveServerConfigVersionMismatch } from "~/versionSkew"; import { usePrimaryCloudLinkState } from "~/cloud/primaryCloudLinkState"; @@ -1447,9 +1448,7 @@ function SavedBackendListRow({ // environment whose connection id is prefixed "local:"), not a remote // environment you connect to or remove here — its lifecycle is driven by the // WSL on/off + distro picker on this page. - const isWslEnvironment = - environment.entry.target._tag === "BearerConnectionTarget" && - environment.entry.target.connectionId.startsWith("local:"); + const isWslEnvironment = isDesktopLocalConnectionTarget(environment.entry.target); return (
@@ -2809,10 +2808,8 @@ export function ConnectionsSettings() { // If WSL never connected (fresh install, toggled on then immediately off, // etc.) there's no local environment, so we skip the confirmation dialog. const hasWslRegistrationToLose = useMemo(() => { - return environments.some( - (environment) => - environment.entry.target._tag === "BearerConnectionTarget" && - environment.entry.target.connectionId.startsWith("local:"), + return environments.some((environment) => + isDesktopLocalConnectionTarget(environment.entry.target), ); }, [environments]); diff --git a/apps/web/src/connection/desktopLocal.ts b/apps/web/src/connection/desktopLocal.ts new file mode 100644 index 0000000000..f12436ee30 --- /dev/null +++ b/apps/web/src/connection/desktopLocal.ts @@ -0,0 +1,47 @@ +import type { ConnectionTarget } from "@t3tools/client-runtime/connection"; +import { PRIMARY_LOCAL_ENVIRONMENT_ID, type DesktopEnvironmentBootstrap } from "@t3tools/contracts"; + +/** + * Desktop-local secondary backends (e.g. a parallel WSL backend) are registered + * by the connection platform source as bearer connections whose id carries this + * prefix. It is the renderer's single signal that an environment is a + * host-managed local backend rather than a user-saved remote, SSH, or relay + * environment. + * + * Keep this the one source of truth: the producer (`connection/platform.ts`) + * mints ids via {@link desktopLocalConnectionId} and every consumer classifies + * via {@link isDesktopLocalConnectionTarget}, so the convention can never drift + * between the two. + */ +export const DESKTOP_LOCAL_CONNECTION_ID_PREFIX = "local:"; + +export function desktopLocalConnectionId(environmentId: string): string { + return `${DESKTOP_LOCAL_CONNECTION_ID_PREFIX}${environmentId}`; +} + +export function isDesktopLocalConnectionTarget(target: ConnectionTarget): boolean { + return ( + target._tag === "BearerConnectionTarget" && + target.connectionId.startsWith(DESKTOP_LOCAL_CONNECTION_ID_PREFIX) + ); +} + +/** + * Read the desktop's secondary local backends (everything except the primary) + * from the bridge. Returns an empty list off-desktop or if the bridge throws. + * Shared by the connection platform source and the renderer's poller so both + * read the same host topology from one place. + */ +export function readDesktopSecondaryBootstraps(): ReadonlyArray { + const bridge = window.desktopBridge; + if (bridge === undefined) { + return []; + } + try { + return bridge + .getLocalEnvironmentBootstraps() + .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID); + } catch { + return []; + } +} diff --git a/apps/web/src/connection/platform.ts b/apps/web/src/connection/platform.ts index c1c3dc6e39..bf5d8b109a 100644 --- a/apps/web/src/connection/platform.ts +++ b/apps/web/src/connection/platform.ts @@ -46,6 +46,7 @@ import { clearComposerDraftsEnvironment } from "../composerDraftStore"; import { isHostedStaticApp } from "../hostedPairing"; import { appAtomRegistry } from "../rpc/atomRegistry"; import { acknowledgeRpcRequest, trackRpcRequestSent } from "../rpc/requestLatencyState"; +import { desktopLocalConnectionId, readDesktopSecondaryBootstraps } from "./desktopLocal"; import { connectionStorageLayer } from "./storage"; let nextObservedRpcRequestId = 0; @@ -320,17 +321,21 @@ const loadSecondaryConnectionRegistration = Effect.fn( scopes: AuthStandardClientScopes, clientMetadata: clientMetadata(), }).pipe(Effect.mapError(mapRemoteEnvironmentError)); - const connectionId = `local:${descriptor.environmentId}`; + const connectionId = desktopLocalConnectionId(descriptor.environmentId); + // Prefer the desktop's bootstrap label (it identifies the backend and distro, + // e.g. "WSL: Ubuntu") over the generic descriptor label, so consumers can show + // a meaningful name without recovering it from the bootstrap list later. + const label = entry.label || descriptor.label; return new BearerConnectionRegistration({ target: new BearerConnectionTarget({ environmentId: descriptor.environmentId, - label: descriptor.label, + label, connectionId, }), profile: new BearerConnectionProfile({ connectionId, environmentId: descriptor.environmentId, - label: descriptor.label, + label, httpBaseUrl, wsBaseUrl, }), @@ -348,20 +353,6 @@ interface CachedPlatformRegistration { readonly registration: PlatformConnectionRegistration; } -function desktopSecondaryBootstraps(): ReadonlyArray { - const bridge = window.desktopBridge; - if (bridge === undefined) { - return []; - } - try { - return bridge - .getLocalEnvironmentBootstraps() - .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID); - } catch { - return []; - } -} - const platformConnectionSourceLayer = Layer.effect( PlatformConnectionSource, Effect.gen(function* () { @@ -404,7 +395,7 @@ const platformConnectionSourceLayer = Layer.effect( } } - for (const bootstrap of desktopSecondaryBootstraps()) { + for (const bootstrap of readDesktopSecondaryBootstraps()) { const signature = `${bootstrap.httpBaseUrl}|${bootstrap.wsBaseUrl}|${bootstrap.bootstrapToken ?? ""}`; const cached = previous.get(bootstrap.id); if (cached !== undefined && cached.signature === signature) { diff --git a/apps/web/src/connection/useDesktopLocalBootstraps.ts b/apps/web/src/connection/useDesktopLocalBootstraps.ts new file mode 100644 index 0000000000..f1c7ab6ced --- /dev/null +++ b/apps/web/src/connection/useDesktopLocalBootstraps.ts @@ -0,0 +1,27 @@ +import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts"; +import { useEffect, useState } from "react"; + +import { readDesktopSecondaryBootstraps } from "./desktopLocal"; + +const DESKTOP_LOCAL_BOOTSTRAP_POLL_MS = 2_000; + +/** + * Reactively track the desktop's secondary local backends (e.g. a parallel WSL + * backend). The bridge exposes no change event, so we re-read on an interval; + * off-desktop this stays an empty list. Use this instead of polling the bridge + * ad hoc so every renderer consumer reads the same topology. + */ +export function useDesktopLocalBootstraps(): ReadonlyArray { + const [bootstraps, setBootstraps] = useState>( + readDesktopSecondaryBootstraps, + ); + + useEffect(() => { + const read = () => setBootstraps(readDesktopSecondaryBootstraps()); + read(); + const interval = setInterval(read, DESKTOP_LOCAL_BOOTSTRAP_POLL_MS); + return () => clearInterval(interval); + }, []); + + return bootstraps; +}