@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 167 lines 5.28 KB
Raw
1 import { msg } from "gt-react";
2
3 import { m, type MessageResolver } from "../../i18n/messages";
4 import type { BrowserImportFailureReason, BrowserImportSource } from "@t3tools/contracts";
5
6 export interface WizardTargetProfile {
7 readonly id: string;
8 readonly name: string;
9 }
10
11 export type WizardTarget =
12 | { readonly kind: "new"; readonly profileId: string }
13 | { readonly kind: "existing"; readonly profileId: string; readonly name: string };
14
15 export type WizardTargetSelection =
16 | { readonly kind: "new" }
17 | { readonly kind: "existing"; readonly profileId: string };
18
19 export function initialTargetSelection(
20 canCreateProfile: boolean,
21 targetProfiles: ReadonlyArray<WizardTargetProfile>,
22 ): WizardTargetSelection {
23 if (canCreateProfile) return { kind: "new" };
24 const first = targetProfiles[0];
25 return first ? { kind: "existing", profileId: first.id } : { kind: "new" };
26 }
27
28 export function resolveWizardTarget(
29 selection: WizardTargetSelection,
30 newProfileId: string,
31 targetProfiles: ReadonlyArray<WizardTargetProfile>,
32 ): WizardTarget | undefined {
33 if (selection.kind === "new") return { kind: "new", profileId: newProfileId };
34 const profile = targetProfiles.find((candidate) => candidate.id === selection.profileId);
35 if (profile === undefined) return undefined;
36 return {
37 kind: "existing",
38 profileId: selection.profileId,
39 name: profile.name,
40 };
41 }
42
43 export type ImportOutcome =
44 | {
45 readonly kind: "imported";
46 readonly imported: number;
47 readonly skipped: number;
48 readonly skippedDomains: ReadonlyArray<string>;
49 readonly targetName: string;
50 }
51 | {
52 readonly kind: "blocked";
53 readonly reason: BrowserImportFailureReason;
54 /** Shown instead of the reason's copy, such as a managed policy's refusal. */
55 readonly message?: string;
56 };
57
58 export type WizardStep =
59 | { readonly step: "quit" }
60 | {
61 readonly step: "fullDiskAccess";
62 readonly resume: "configure" | "import";
63 readonly checked?: boolean;
64 }
65 | { readonly step: "configure" }
66 | { readonly step: "checking"; readonly check: "browser" | "fullDiskAccess" }
67 | { readonly step: "importing" }
68 | {
69 readonly step: "done";
70 readonly imported: number;
71 readonly skipped: number;
72 readonly skippedDomains: ReadonlyArray<string>;
73 readonly targetName: string;
74 }
75 | {
76 readonly step: "blocked";
77 readonly reason: BrowserImportFailureReason;
78 readonly message?: string;
79 };
80
81 export function canCloseWizard(step: WizardStep): boolean {
82 return step.step !== "importing";
83 }
84
85 export function initialWizardStep(source: BrowserImportSource): WizardStep {
86 if (source.unavailable === "browserRunning") return { step: "quit" };
87 if (source.unavailable === "needsFullDiskAccess") {
88 return { step: "fullDiskAccess", resume: "configure" };
89 }
90 if (source.unavailable !== undefined) return { step: "blocked", reason: source.unavailable };
91 if (source.profiles.length === 0) return { step: "blocked", reason: "unknownSourceProfile" };
92 return { step: "configure" };
93 }
94
95 export function outcomeToStep(outcome: ImportOutcome): WizardStep {
96 if (outcome.kind === "imported") {
97 return {
98 step: "done",
99 imported: outcome.imported,
100 skipped: outcome.skipped,
101 skippedDomains: outcome.skippedDomains,
102 targetName: outcome.targetName,
103 };
104 }
105 if (outcome.reason === "browserRunning") return { step: "quit" };
106 if (outcome.reason === "needsFullDiskAccess") {
107 return { step: "fullDiskAccess", resume: "import", checked: true };
108 }
109 return outcome.message === undefined
110 ? { step: "blocked", reason: outcome.reason }
111 : { step: "blocked", reason: outcome.reason, message: outcome.message };
112 }
113
114 export function refreshedSourceStep(source: BrowserImportSource | undefined): WizardStep {
115 if (source === undefined) return { step: "blocked", reason: "unknownSource" };
116 return initialWizardStep(source);
117 }
118
119 export function fullDiskAccessRecheckStep(source: BrowserImportSource | undefined): WizardStep {
120 const next = refreshedSourceStep(source);
121 return next.step === "fullDiskAccess" ? { ...next, checked: true } : next;
122 }
123
124 export function refreshedSourceProfileDirectory(
125 currentDirectory: string,
126 source: BrowserImportSource,
127 ): string {
128 if (source.profiles.some((profile) => profile.directory === currentDirectory)) {
129 return currentDirectory;
130 }
131 return source.profiles[0]?.directory ?? "";
132 }
133
134 export function isRetryableReason(reason: BrowserImportFailureReason): boolean {
135 switch (reason) {
136 case "needsKeychainApproval":
137 case "keychainItemMissing":
138 case "keychainUnavailable":
139 case "readFailed":
140 case "sessionUnavailable":
141 case "profileNotSaved":
142 return true;
143 default:
144 return false;
145 }
146 }
147
148 export function formatSkippedDomains(
149 domains: ReadonlyArray<string>,
150 resolve: MessageResolver = m,
151 ): string {
152 if (domains.length === 0) return "";
153 if (domains.length === 1) return domains[0]!;
154 if (domains.length <= 3)
155 return resolve(
156 msg("{domains} and {last}", {
157 domains: domains.slice(0, -1).join(", "),
158 last: domains.at(-1),
159 }),
160 );
161 return resolve(
162 msg("{count, plural, one {{domains} and {count} more} other {{domains} and {count} more}}", {
163 domains: domains.slice(0, 3).join(", "),
164 count: domains.length - 3,
165 }),
166 );
167 }