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