Open History to see the last commit
1import {
2 type AdvertisedEndpoint,
3 type AuthGrantScope,
4 AuthTerminalOperateScope,
5 AuthTerminalReadScope,
6 type DesktopBridge,
7 type DesktopServerExposureState,
8 type DesktopUpdateState,
9 type DesktopWslState,
10} from "@t3tools/contracts";
11import {
12 getDesktopUpdateDownloadedVersion,
13 resolveDesktopUpdateButtonAction,
14} from "../desktopUpdate.logic";
15
16/**
17 * Operating terminals without being able to list them leaves a client
18 * allocating a fresh shell for every script run it cannot see, so the pairing
19 * form keeps `terminal:read` alongside `terminal:operate`.
20 */
21export function togglePairingScopeSelection(
22 current: ReadonlyArray<AuthGrantScope>,
23 scope: AuthGrantScope,
24 checked: boolean,
25): ReadonlyArray<AuthGrantScope> {
26 const without = (scopes: ReadonlyArray<AuthGrantScope>) =>
27 scopes.filter((currentScope) => currentScope !== scope);
28 if (!checked) {
29 return scope === AuthTerminalReadScope
30 ? without(current).filter((currentScope) => currentScope !== AuthTerminalOperateScope)
31 : without(current);
32 }
33 const next = [...without(current), scope];
34 return scope === AuthTerminalOperateScope && !next.includes(AuthTerminalReadScope)
35 ? [...next, AuthTerminalReadScope]
36 : next;
37}
38
39export function canRevokeOtherClients(
40 clientSessions: ReadonlyArray<{ readonly current: boolean }> | null,
41): boolean {
42 return clientSessions === null || clientSessions.some((session) => !session.current);
43}
44
45type WslEnableBridge = Pick<DesktopBridge, "setWslBackendEnabled" | "setWslDistro" | "setWslOnly">;
46
47/**
48 * A QR code encoding a loopback URL makes the scanning device dial itself, so
49 * loopback endpoints stay copyable from the endpoint menu but are never
50 * offered as QR targets.
51 */
52export function isQrShareableEndpoint(endpoint: AdvertisedEndpoint): boolean {
53 return endpoint.status !== "unavailable" && endpoint.reachability !== "loopback";
54}
55
56/**
57 * Copy for the Tailscale HTTPS row when no Tailscale endpoint is advertised.
58 * The desktop skips the Tailscale probe until Network access or Tailscale
59 * Serve is on (it can trigger a macOS privacy prompt), so a missing endpoint
60 * then means "not checked", not "not running".
61 */
62export function missingTailscaleEndpointDescription(
63 state: Pick<DesktopServerExposureState, "mode" | "tailscaleServeEnabled"> | null,
64): string {
65 return state && state.mode !== "network-accessible" && !state.tailscaleServeEnabled
66 ? "Turn on Network access to detect Tailscale."
67 : "Start Tailscale to set up HTTPS access through MagicDNS.";
68}
69
70export function isWslSettingsRowVisible(input: {
71 readonly state: DesktopWslState | null;
72 readonly error: string | null;
73}): boolean {
74 const { state, error } = input;
75 return state ? state.available || state.enabled || state.wslOnly : error !== null;
76}
77
78/**
79 * Passive label for this machine's Version row once client and server versions
80 * match. The desktop bundles its server, so a pending app update is the only
81 * way it falls behind. Pending states follow the sidebar update control's
82 * action, including retryable failures, so the two never disagree; the sidebar
83 * is where to act. A recheck keeps a downloaded update while `checking`, so it
84 * keeps its label too. `state` is null outside the desktop app.
85 */
86export function resolveLocalVersionStatusLabel(state: DesktopUpdateState | null): string {
87 if (!state) return "Up to date";
88 if (state.status === "downloading") {
89 return state.availableVersion
90 ? `Downloading update ${state.availableVersion}`
91 : "Downloading update";
92 }
93 const action = resolveDesktopUpdateButtonAction(state);
94 if (
95 action === "install" ||
96 state.status === "downloaded" ||
97 (state.status === "checking" && state.downloadedVersion !== null)
98 ) {
99 const version = getDesktopUpdateDownloadedVersion(state);
100 return version ? `Update ${version} downloaded` : "Update downloaded";
101 }
102 if (action === "download") {
103 return state.availableVersion
104 ? `Update ${state.availableVersion} available`
105 : "Update available";
106 }
107 return "Up to date";
108}
109
110export type QrEndpointOption = {
111 readonly id: string;
112 /**
113 * Stable per endpoint *type* (endpointDefaultPreferenceKey). Multiple
114 * endpoints can share one, so it is only used to match the saved default.
115 */
116 readonly preferenceKey: string;
117 readonly qrShareable: boolean;
118};
119
120export function selectQrEndpointOption<T extends QrEndpointOption>(
121 options: ReadonlyArray<T>,
122 selectedId: string | null,
123 defaultPreferenceKey: string | null,
124): T | null {
125 return (
126 (selectedId !== null ? options.find((option) => option.id === selectedId) : undefined) ??
127 (defaultPreferenceKey !== null
128 ? options.find((option) => option.preferenceKey === defaultPreferenceKey)
129 : undefined) ??
130 options.find((option) => option.qrShareable) ??
131 options[0] ??
132 null
133 );
134}
135
136export async function applyWslEnableSelection(input: {
137 readonly bridge: WslEnableBridge;
138 readonly mode: "both" | "wsl-only";
139 readonly nextDistro: string | null;
140 readonly persistedDistro: string | null;
141}): Promise<DesktopWslState> {
142 const { bridge, mode, nextDistro, persistedDistro } = input;
143
144 // Stage every preference before enabling. The desktop only relaunches for
145 // mode/distro changes while WSL is active, so the final enable observes the
146 // complete selection and is the only call that may relaunch.
147 await bridge.setWslOnly(mode === "wsl-only");
148 if (persistedDistro !== nextDistro) {
149 await bridge.setWslDistro(nextDistro);
150 }
151 return await bridge.setWslBackendEnabled(true);
152}