Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ConnectionsSettings.logic.ts

115 lines4.1 KB

1import {
2 type AdvertisedEndpoint,
3 type AuthGrantScope,
4 AuthTerminalOperateScope,
5 AuthTerminalReadScope,
6 type DesktopBridge,
7 type DesktopServerExposureState,
8 type DesktopWslState,
9} from "@t3tools/contracts";
10​
11/**
12 * Operating terminals without being able to list them leaves a client
13 * allocating a fresh shell for every script run it cannot see, so the pairing
14 * form keeps `terminal:read` alongside `terminal:operate`.
15 */
16export function togglePairingScopeSelection(
17 current: ReadonlyArray<AuthGrantScope>,
18 scope: AuthGrantScope,
19 checked: boolean,
20): ReadonlyArray<AuthGrantScope> {
21 const without = (scopes: ReadonlyArray<AuthGrantScope>) =>
22 scopes.filter((currentScope) => currentScope !== scope);
23 if (!checked) {
24 return scope === AuthTerminalReadScope
25 ? without(current).filter((currentScope) => currentScope !== AuthTerminalOperateScope)
26 : without(current);
27 }
28 const next = [...without(current), scope];
29 return scope === AuthTerminalOperateScope && !next.includes(AuthTerminalReadScope)
30 ? [...next, AuthTerminalReadScope]
31 : next;
33​
34export function canRevokeOtherClients(
35 clientSessions: ReadonlyArray<{ readonly current: boolean }> | null,
36): boolean {
37 return clientSessions === null || clientSessions.some((session) => !session.current);
39​
40type WslEnableBridge = Pick<DesktopBridge, "setWslBackendEnabled" | "setWslDistro" | "setWslOnly">;
41​
42/**
43 * A QR code encoding a loopback URL makes the scanning device dial itself, so
44 * loopback endpoints stay copyable from the endpoint menu but are never
45 * offered as QR targets.
46 */
47export function isQrShareableEndpoint(endpoint: AdvertisedEndpoint): boolean {
48 return endpoint.status !== "unavailable" && endpoint.reachability !== "loopback";
50​
51/**
52 * Copy for the Tailscale HTTPS row when no Tailscale endpoint is advertised.
53 * The desktop skips the Tailscale probe until Network access or Tailscale
54 * Serve is on (it can trigger a macOS privacy prompt), so a missing endpoint
55 * then means "not checked", not "not running".
56 */
57export function missingTailscaleEndpointDescription(
58 state: Pick<DesktopServerExposureState, "mode" | "tailscaleServeEnabled"> | null,
59): string {
60 return state && state.mode !== "network-accessible" && !state.tailscaleServeEnabled
61 ? "Turn on Network access to detect Tailscale."
62 : "Start Tailscale to set up HTTPS access through MagicDNS.";
64​
65export function isWslSettingsRowVisible(input: {
66 readonly state: DesktopWslState | null;
67 readonly error: string | null;
68}): boolean {
69 const { state, error } = input;
70 return state ? state.available || state.enabled || state.wslOnly : error !== null;
72​
73export type QrEndpointOption = {
74 readonly id: string;
75 /**
76 * Stable per endpoint *type* (endpointDefaultPreferenceKey). Multiple
77 * endpoints can share one, so it is only used to match the saved default.
78 */
79 readonly preferenceKey: string;
80 readonly qrShareable: boolean;
81};
82​
83export function selectQrEndpointOption<T extends QrEndpointOption>(
84 options: ReadonlyArray<T>,
85 selectedId: string | null,
86 defaultPreferenceKey: string | null,
87): T | null {
88 return (
89 (selectedId !== null ? options.find((option) => option.id === selectedId) : undefined) ??
90 (defaultPreferenceKey !== null
91 ? options.find((option) => option.preferenceKey === defaultPreferenceKey)
92 : undefined) ??
93 options.find((option) => option.qrShareable) ??
94 options[0] ??
95 null
96 );
98​
99export async function applyWslEnableSelection(input: {
100 readonly bridge: WslEnableBridge;
101 readonly mode: "both" | "wsl-only";
102 readonly nextDistro: string | null;
103 readonly persistedDistro: string | null;
104}): Promise<DesktopWslState> {
105 const { bridge, mode, nextDistro, persistedDistro } = input;
106​
107 // Stage every preference before enabling. The desktop only relaunches for
108 // mode/distro changes while WSL is active, so the final enable observes the
109 // complete selection and is the only call that may relaunch.
110 await bridge.setWslOnly(mode === "wsl-only");
111 if (persistedDistro !== nextDistro) {
112 await bridge.setWslDistro(nextDistro);
113 }
114 return await bridge.setWslBackendEnabled(true);