Open History to see the last commit
1import {
2 SimlockDeviceHostConfig,
3 type EnvironmentId,
4 type SimlockDeviceHostConfig as SimlockDeviceHostConfigType,
5} from "@t3tools/contracts";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8
9const parseSimlockDeviceHostDraft = Schema.decodeUnknownOption(SimlockDeviceHostConfig);
10
11export const newSimlockDeviceHost = (id: string): SimlockDeviceHostConfigType => ({
12 kind: "simlock",
13 id,
14 label: "Simlock",
15 gatewayUrl: "",
16 agentToken: "",
17 maxLeases: 2,
18 maxLeaseMinutes: 60,
19});
20
21/** Adds, replaces, or removes one Simlock host. */
22export function updateSimlockDeviceHosts(
23 hosts: ReadonlyArray<SimlockDeviceHostConfigType>,
24 host: SimlockDeviceHostConfigType,
25 remove: boolean,
26): ReadonlyArray<SimlockDeviceHostConfigType> {
27 if (remove) return hosts.filter((candidate) => candidate.id !== host.id);
28 return hosts.some((candidate) => candidate.id === host.id)
29 ? hosts.map((candidate) => (candidate.id === host.id ? host : candidate))
30 : [...hosts, host];
31}
32
33/** Why the shared fields keep a draft from saving, or null; tokens are checked per environment. */
34export function simlockDeviceHostProblem(host: SimlockDeviceHostConfigType): string | null {
35 const withToken = { ...host, agentToken: "token" };
36 if (Option.isSome(parseSimlockDeviceHostDraft(withToken))) return null;
37 if (!host.label.trim()) return "Give the host a label.";
38 // With in-range limits, a draft that still fails has a bad gateway URL.
39 return Option.isNone(
40 parseSimlockDeviceHostDraft({ ...withToken, maxLeases: 1, maxLeaseMinutes: 60 }),
41 )
42 ? "Enter the gateway's origin, such as https://gateway.example.ts.net:4700."
43 : "Keep the limits within the ranges shown.";
44}
45
46export function simlockLeaseLimits(host: SimlockDeviceHostConfigType) {
47 const devices = host.maxLeases === 1 ? "1 device" : `${host.maxLeases} devices`;
48 return `Up to ${devices} at once, each released after ${host.maxLeaseMinutes} min`;
49}
50
51export interface SimlockHostEnvironment {
52 readonly environmentId: EnvironmentId;
53 readonly label: string;
54 readonly hosts: ReadonlyArray<SimlockDeviceHostConfigType>;
55}
56
57/** A token field: the environment's saved token, a replacement, or none. */
58export type SimlockTokenEntry =
59 | { kind: "saved" }
60 | { kind: "new"; value: string }
61 | { kind: "cleared" };
62
63/**
64 * The host list each environment saves: the shared fields everywhere, and each environment's own
65 * token, which may stay blank until later. Saved tokens arrive redacted; sending one back keeps it.
66 */
67export function planSimlockDeviceHostSave(
68 environments: ReadonlyArray<SimlockHostEnvironment>,
69 draft: SimlockDeviceHostConfigType,
70 tokens: ReadonlyMap<EnvironmentId, SimlockTokenEntry>,
71):
72 | { readonly problem: string }
73 | {
74 readonly patches: ReadonlyArray<{
75 environmentId: EnvironmentId;
76 hosts: ReadonlyArray<SimlockDeviceHostConfigType>;
77 }>;
78 } {
79 const shared = simlockDeviceHostProblem(draft);
80 if (shared) return { problem: shared };
81 const patches = [];
82 for (const environment of environments) {
83 const saved = environment.hosts.find((host) => host.id === draft.id)?.agentToken ?? "";
84 const entry = tokens.get(environment.environmentId) ?? { kind: "cleared" };
85 const agentToken =
86 entry.kind === "saved" ? saved : entry.kind === "new" ? entry.value.trim() : "";
87 patches.push({
88 environmentId: environment.environmentId,
89 hosts: updateSimlockDeviceHosts(environment.hosts, { ...draft, agentToken }, false),
90 });
91 }
92 return { patches };
93}