From efefb11ee4cc309765b837b35ff0ac39618dd484 Mon Sep 17 00:00:00 2001 From: LHaghfoss Date: Tue, 24 Nov 2026 15:04:20 +0100 Subject: [PATCH] feat(desktop): say once at the top of Remote access whether a phone can reach this computer --- apps/desktop/src/state/remoteSummary.test.ts | 69 +++++++++++++++++ apps/desktop/src/state/remoteSummary.ts | 81 ++++++++++++++++++++ apps/desktop/src/ui/RemoteAccessPage.tsx | 32 ++------ apps/desktop/src/ui/RemoteStatusBanner.tsx | 39 ++++++++++ 4 files changed, 195 insertions(+), 26 deletions(-) create mode 100644 apps/desktop/src/state/remoteSummary.test.ts create mode 100644 apps/desktop/src/state/remoteSummary.ts create mode 100644 apps/desktop/src/ui/RemoteStatusBanner.tsx diff --git a/apps/desktop/src/state/remoteSummary.test.ts b/apps/desktop/src/state/remoteSummary.test.ts new file mode 100644 index 000000000..9eaf2856b --- /dev/null +++ b/apps/desktop/src/state/remoteSummary.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, test } from "bun:test"; +import type { RemoteGateway, RemoteStatus } from "../acp/remote"; +import type { ServerService } from "./machines"; +import { remoteSummary } from "./remoteSummary"; + +const gateway = (patch: Partial = {}): RemoteGateway => ({ + connected: true, + advertisedAddress: "192.168.1.24:17879", + loopbackOnly: false, + routes: [], + attachedDevices: [], + connectedDevices: ["Phone"], + ...patch, +}); + +const status = (patch: Partial = {}): RemoteStatus => ({ + available: true, + enabled: true, + shared: false, + gateway: gateway(), + pairedDevices: 2, + devices: [], + connectRequests: [], + endedConnectRequests: [], + refusedConnections: [], + ...patch, +}); + +const server = (running: boolean) => ({ installed: true, running }) as ServerService; + +describe("remoteSummary", () => { + test("reachable names the address, devices and pairings", () => { + const s = remoteSummary(status(), server(true)); + expect(s.tone).toBe("ok"); + expect(s.label).toBe("On"); + expect(s.title).toBe("Reachable on your network at 192.168.1.24:17879"); + expect(s.detail).toBe("1 device connected · 2 devices paired"); + expect(s.address).toBe("192.168.1.24:17879"); + }); + + test("without the server it says the app has to stay open", () => { + expect(remoteSummary(status(), server(false)).detail).toContain("Reachable while the app is open"); + }); + + test("off with the server running says network access is what is off", () => { + const s = remoteSummary(status({ enabled: false }), server(true)); + expect(s.label).toBe("Off"); + expect(s.detail).toContain("server is running, but network access is off"); + }); + + test("on but not connected yet is starting", () => { + expect(remoteSummary(status({ gateway: gateway({ connected: false }) }), null).label).toBe("Starting"); + }); + + test("a loopback gateway is local only", () => { + expect(remoteSummary(status({ gateway: gateway({ loopbackOnly: true }) }), null).label).toBe( + "Local only", + ); + }); + + test("an ended share, a missing gateway and a failed read each say so", () => { + expect(remoteSummary(status({ error: "The gateway stopped." }), null).detail).toBe( + "The gateway stopped.", + ); + expect(remoteSummary(status({ available: false }), null).label).toBe("Unavailable"); + expect(remoteSummary(null, null, "timed out").tone).toBe("bad"); + expect(remoteSummary(null, null).label).toBe("Checking"); + }); +}); diff --git a/apps/desktop/src/state/remoteSummary.ts b/apps/desktop/src/state/remoteSummary.ts new file mode 100644 index 000000000..de49ca6b7 --- /dev/null +++ b/apps/desktop/src/state/remoteSummary.ts @@ -0,0 +1,81 @@ +import type { RemoteStatus } from "../acp/remote"; +import type { ServerService } from "./machines"; + +export type RemoteSummaryTone = "ok" | "warn" | "bad" | "idle"; + +/** Settings › Remote access in one line: where this computer can be reached from, if anywhere. */ +export interface RemoteSummary { + tone: RemoteSummaryTone; + /** The short state, on the banner's chip: On, Off, Starting, Local only, Stopped. */ + label: string; + title: string; + detail: string; + /** What a phone dials, when there is one to copy. */ + address?: string; +} + +const plural = (n: number, one: string) => `${n} ${one}${n === 1 ? "" : "s"}`; + +/** + * Reads the gateway (`remote`), the background server and a failed read of the gateway into one + * state, so the page says once whether a phone can reach this computer. + */ +export function remoteSummary( + remote: RemoteStatus | null, + server: ServerService | null, + readError?: string | null, +): RemoteSummary { + if (readError) + return { tone: "bad", label: "Unknown", title: "Could not read remote access", detail: readError }; + if (remote === null) + return { tone: "idle", label: "Checking", title: "Checking remote access…", detail: "" }; + if (!remote.available) + return { + tone: "idle", + label: "Unavailable", + title: "Remote access is not available here", + detail: "This build of RustCode has no remote gateway.", + }; + if (remote.error) + return { tone: "warn", label: "Stopped", title: "Remote access stopped", detail: remote.error }; + if (!remote.enabled) + return { + tone: "idle", + label: "Off", + title: "No device can reach this computer", + detail: server?.running + ? "The RustCode server is running, but network access is off. Paired devices stay paired." + : "Turn on network access to open this computer's threads from a phone.", + }; + const gateway = remote.gateway; + if (!gateway?.connected) + return { + tone: "warn", + label: "Starting", + title: "Starting the gateway…", + detail: "Waiting for it to announce an address.", + }; + const connected = gateway.connectedDevices.length; + const parts = [ + connected ? `${plural(connected, "device")} connected` : "No device connected", + remote.pairedDevices ? `${plural(remote.pairedDevices, "device")} paired` : "", + server?.running ? "" : "Reachable while the app is open", + ].filter(Boolean); + if (gateway.loopbackOnly) + return { + tone: "warn", + label: "Local only", + title: "Reachable from this computer only", + detail: "The gateway listens on loopback, so a phone on the network cannot reach it.", + address: gateway.advertisedAddress, + }; + return { + tone: "ok", + label: "On", + title: gateway.advertisedAddress + ? `Reachable on your network at ${gateway.advertisedAddress}` + : "Reachable on your network", + detail: parts.join(" · "), + address: gateway.advertisedAddress, + }; +} diff --git a/apps/desktop/src/ui/RemoteAccessPage.tsx b/apps/desktop/src/ui/RemoteAccessPage.tsx index 0e4f2c66b..15f907afb 100644 --- a/apps/desktop/src/ui/RemoteAccessPage.tsx +++ b/apps/desktop/src/ui/RemoteAccessPage.tsx @@ -24,7 +24,6 @@ import { import { encodeQr } from "../lib/qr"; import { deviceLabel, lastSeenLabel } from "../state/connectRequests"; import { - gatewayLine, installServer, refreshMachines, refreshServerService, @@ -40,11 +39,13 @@ import { tickRemotePairing, } from "../state/remoteAccess"; import { remainingPairingSeconds } from "../state/remotePairing"; +import { remoteSummary } from "../state/remoteSummary"; import { askConfirm, errorText, toast, useApp } from "../state/store"; import { connectionDirection, peerOutbound } from "./connectionDirection"; import { Smartphone, WifiOff } from "./icons"; import { copyText, cx, Spinner, Switch } from "./primitives"; import { ConnectRequestsGroup, RevokeDeviceAction } from "./RemoteConnect"; +import { RemoteStatusBanner } from "./RemoteStatusBanner"; import { ServerProblems } from "./ServerProblems"; import { Action, Chip, Group, Row, SettingsHeaderActions, withCode } from "./settingsKit"; @@ -174,16 +175,14 @@ export function RemoteAccessPage() { Pair device - {remoteError && ( -

Could not read the gateway: {remoteError}

- )} + 0 ? status : undefined} > {server && !server.installed ? {serverLine(server)} : null} - {server?.gateway.remoteEnabled || server?.gateway.running ? ( - Gateway: {gatewayLine(server.gateway, server.installed)} - ) : null} } @@ -216,14 +212,7 @@ export function RemoteAccessPage() { )} - Gateway address - - {gateway?.connected ? "Connected" : enabled ? "Starting" : "Off"} - - - } + title="Gateway address" subtitle="What a phone on this network dials." hint={withCode("The terminal's `/remote status` shows the same address.")} status={ @@ -241,16 +230,7 @@ export function RemoteAccessPage() { "The gateway is not running." ) } - > - {gateway?.advertisedAddress && ( - void copyText(gateway.advertisedAddress!)} - > - Copy - - )} - + /> {(unavailable || expired) && ( + + + +
+
+ {summary.title} + {summary.label} +
+ {summary.detail &&
{summary.detail}
} +
+ {summary.address && ( + void copyText(summary.address!)}> + Copy address + + )} + + ); +}