The best way to control Claude Code, Codex and more from any computer or phone

main
ts 91 lines 3.24 KB
Raw
1 import type { EnvironmentId, SshDeviceHostConfig } from "@t3tools/contracts";
2 import { msg } from "gt-react";
3
4 import { m } from "../../i18n/messages";
5
6 export interface DeviceHostEnvironment {
7 readonly environmentId: EnvironmentId;
8 readonly label: string;
9 readonly hosts: ReadonlyArray<SshDeviceHostConfig>;
10 }
11
12 export interface DeviceHostGroup {
13 readonly host: SshDeviceHostConfig;
14 readonly members: Array<{
15 readonly environmentId: EnvironmentId;
16 readonly label: string;
17 readonly host: SshDeviceHostConfig;
18 }>;
19 }
20
21 /** Only combine matching configurations; ambiguous identities keep their machine's list. */
22 export function groupDeviceHosts(
23 environments: ReadonlyArray<DeviceHostEnvironment>,
24 ): ReadonlyArray<DeviceHostGroup> | null {
25 const groups = new Map<string, DeviceHostGroup>();
26 const identities = new Map<string, string>();
27 const configurations = new Map<string, string>();
28 for (const environment of environments) {
29 const destinations = new Set<string>();
30 for (const host of environment.hosts) {
31 const destination = JSON.stringify([host.target, host.port, host.identityFile]);
32 const configuration = JSON.stringify([host.label, host.target, host.port, host.identityFile]);
33 const previousConfiguration = identities.get(host.id);
34 const targetConfiguration = configurations.get(host.target);
35 const group = groups.get(destination);
36 if (
37 destinations.has(destination) ||
38 (previousConfiguration !== undefined && previousConfiguration !== configuration) ||
39 (targetConfiguration !== undefined && targetConfiguration !== configuration)
40 ) {
41 return null;
42 }
43 destinations.add(destination);
44 identities.set(host.id, configuration);
45 configurations.set(host.target, configuration);
46 const member = {
47 environmentId: environment.environmentId,
48 label: environment.label,
49 host,
50 };
51 if (group) group.members.push(member);
52 else groups.set(destination, { host, members: [member] });
53 }
54 }
55 return [...groups.values()];
56 }
57
58 export function updateDeviceHosts(
59 hosts: ReadonlyArray<SshDeviceHostConfig>,
60 host: SshDeviceHostConfig,
61 remove: boolean,
62 original = host,
63 ): ReadonlyArray<SshDeviceHostConfig> {
64 const sameDestination = (candidate: SshDeviceHostConfig, other: SshDeviceHostConfig) =>
65 candidate.target === other.target &&
66 candidate.port === other.port &&
67 candidate.identityFile === other.identityFile;
68 const findDestination = (destination: SshDeviceHostConfig) => {
69 const matches = hosts.filter((candidate) => sameDestination(candidate, destination));
70 if (matches.length > 1) {
71 throw new Error(
72 m(
73 msg(
74 "Multiple hosts match this SSH destination. Select the environment to edit its hosts.",
75 ),
76 ),
77 );
78 }
79 return matches[0];
80 };
81 const existing =
82 hosts.find((candidate) => candidate.id === original.id) ??
83 findDestination(original) ??
84 (remove ? undefined : findDestination(host));
85 if (remove) return hosts.filter((candidate) => candidate.id !== existing?.id);
86 return existing
87 ? hosts.map((candidate) =>
88 candidate.id === existing.id ? { ...host, id: existing.id } : candidate,
89 )
90 : [...hosts, host];
91 }