Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions apps/desktop/src/state/remoteSummary.test.ts
Original file line number Diff line number Diff line change
@@ -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> = {}): RemoteGateway => ({
connected: true,
advertisedAddress: "192.168.1.24:17879",
loopbackOnly: false,
routes: [],
attachedDevices: [],
connectedDevices: ["Phone"],
...patch,
});

const status = (patch: Partial<RemoteStatus> = {}): 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");
});
});
81 changes: 81 additions & 0 deletions apps/desktop/src/state/remoteSummary.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
32 changes: 6 additions & 26 deletions apps/desktop/src/ui/RemoteAccessPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
import { encodeQr } from "../lib/qr";
import { deviceLabel, lastSeenLabel } from "../state/connectRequests";
import {
gatewayLine,
installServer,
refreshMachines,
refreshServerService,
Expand All @@ -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";

Expand Down Expand Up @@ -174,16 +175,14 @@ export function RemoteAccessPage() {
Pair device
</Action>
</SettingsHeaderActions>
{remoteError && (
<p className="mb-6 text-callout text-warning">Could not read the gateway: {remoteError}</p>
)}
<RemoteStatusBanner summary={remoteSummary(remote, server, remoteError)} />

<Group>
<Row
title="Network access"
subtitle="Let paired devices reach every thread on this computer."
hint="Starts the gateway, across all projects, not only the ones open here. Off stops remote access for this computer, including a share a terminal started; paired devices stay paired."
status={status}
status={enabled && sharedCount > 0 ? status : undefined}
>
<Switch
label="Network access"
Expand All @@ -199,9 +198,6 @@ export function RemoteAccessPage() {
status={
<>
{server && !server.installed ? <span className="tabular">{serverLine(server)}</span> : null}
{server?.gateway.remoteEnabled || server?.gateway.running ? (
<span className="block">Gateway: {gatewayLine(server.gateway, server.installed)}</span>
) : null}
<ServerProblems problems={server?.problems} />
</>
}
Expand All @@ -216,14 +212,7 @@ export function RemoteAccessPage() {
)}
</Row>
<Row
title={
<>
Gateway address
<Chip tone={gateway?.connected ? "ok" : enabled ? "warn" : "idle"}>
{gateway?.connected ? "Connected" : enabled ? "Starting" : "Off"}
</Chip>
</>
}
title="Gateway address"
subtitle="What a phone on this network dials."
hint={withCode("The terminal's `/remote status` shows the same address.")}
status={
Expand All @@ -241,16 +230,7 @@ export function RemoteAccessPage() {
"The gateway is not running."
)
}
>
{gateway?.advertisedAddress && (
<Action
aria-label="Copy the gateway address"
onClick={() => void copyText(gateway.advertisedAddress!)}
>
Copy
</Action>
)}
</Row>
/>
{(unavailable || expired) && (
<Row
title="Pair a device"
Expand Down
39 changes: 39 additions & 0 deletions apps/desktop/src/ui/RemoteStatusBanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { RemoteSummary } from "../state/remoteSummary";
import { Globe, Smartphone, WifiOff } from "./icons";
import { copyText, cx } from "./primitives";
import { Action, Chip } from "./settingsKit";

/** Settings › Remote access, top of the page: whether a phone can reach this computer, said once. */
export function RemoteStatusBanner({ summary }: { summary: RemoteSummary }) {
const Icon = summary.tone === "ok" ? Globe : summary.label === "Local only" ? Smartphone : WifiOff;
return (
<section
aria-label="Remote access status"
aria-live="polite"
className="mb-8 flex items-center gap-4 rounded-xl border border-border bg-surface px-4 py-4"
>
<span
aria-hidden
className={cx(
"flex size-10 shrink-0 items-center justify-center rounded-full bg-fill",
summary.tone === "ok" ? "text-success" : summary.tone === "idle" ? "text-fg-faint" : "text-warning",
summary.tone === "bad" && "text-danger",
)}
>
<Icon size={18} />
</span>
<div className="min-w-0 flex-1">
<div className="flex min-w-0 items-center gap-2 font-medium text-body text-fg">
<span className="min-w-0 truncate">{summary.title}</span>
<Chip tone={summary.tone}>{summary.label}</Chip>
</div>
{summary.detail && <div className="mt-0.5 text-callout text-fg-muted">{summary.detail}</div>}
</div>
{summary.address && (
<Action aria-label="Copy the gateway address" onClick={() => void copyText(summary.address!)}>
Copy address
</Action>
)}
</section>
);
}
Loading