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

ConnectionsSettings.logic.ts

100 lines3.4 KB

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