Open History to see the last commit
1import type { BrowserImportFailureReason, BrowserImportSource } from "@t3tools/contracts";
2
3export interface WizardTargetProfile {
4 readonly id: string;
5 readonly name: string;
6}
7
8export type WizardTarget =
9 | { readonly kind: "new"; readonly profileId: string }
10 | { readonly kind: "existing"; readonly profileId: string; readonly name: string };
11
12export type WizardTargetSelection =
13 | { readonly kind: "new" }
14 | { readonly kind: "existing"; readonly profileId: string };
15
16export function initialTargetSelection(
17 canCreateProfile: boolean,
18 targetProfiles: ReadonlyArray<WizardTargetProfile>,
19): WizardTargetSelection {
20 if (canCreateProfile) return { kind: "new" };
21 const first = targetProfiles[0];
22 return first ? { kind: "existing", profileId: first.id } : { kind: "new" };
23}
24
25export function resolveWizardTarget(
26 selection: WizardTargetSelection,
27 newProfileId: string,
28 targetProfiles: ReadonlyArray<WizardTargetProfile>,
29): WizardTarget | undefined {
30 if (selection.kind === "new") return { kind: "new", profileId: newProfileId };
31 const profile = targetProfiles.find((candidate) => candidate.id === selection.profileId);
32 if (profile === undefined) return undefined;
33 return {
34 kind: "existing",
35 profileId: selection.profileId,
36 name: profile.name,
37 };
38}
39
40/**
41 * What the import wizard produces once it has actually tried to import. The
42 * parent runs the import and classifies the result; the wizard only reacts to
43 * it, which keeps the step transitions pure and testable.
44 */
45export type ImportOutcome =
46 | {
47 readonly kind: "imported";
48 readonly imported: number;
49 readonly skipped: number;
50 readonly skippedDomains: ReadonlyArray<string>;
51 readonly targetName: string;
52 }
53 | { readonly kind: "blocked"; readonly reason: BrowserImportFailureReason };
54
55/**
56 * The wizard's screens. Every one is a place the user can act from — there are
57 * no dead ends. `blocked` covers the reasons no local step recovers.
58 */
59export type WizardStep =
60 | { readonly step: "quit" }
61 | { readonly step: "configure" }
62 | { readonly step: "checking" }
63 | { readonly step: "importing" }
64 | {
65 readonly step: "done";
66 readonly imported: number;
67 readonly skipped: number;
68 readonly skippedDomains: ReadonlyArray<string>;
69 readonly targetName: string;
70 }
71 | { readonly step: "blocked"; readonly reason: BrowserImportFailureReason };
72
73/** The import owns its target partition until the write finishes. */
74export function canCloseWizard(step: WizardStep): boolean {
75 return step.step !== "importing";
76}
77
78/**
79 * Where the wizard opens for a source. A running browser is the one thing we
80 * know up front, from the source listing; everything else is discovered by
81 * trying, so the wizard starts by letting the user choose what to import.
82 */
83export function initialWizardStep(source: BrowserImportSource): WizardStep {
84 if (source.unavailable === "browserRunning") return { step: "quit" };
85 if (source.unavailable !== undefined) return { step: "blocked", reason: source.unavailable };
86 if (source.profiles.length === 0) return { step: "blocked", reason: "unknownSourceProfile" };
87 return { step: "configure" };
88}
89
90/** Where an attempted import lands the wizard, by how it turned out. */
91export function outcomeToStep(outcome: ImportOutcome): WizardStep {
92 if (outcome.kind === "imported") {
93 return {
94 step: "done",
95 imported: outcome.imported,
96 skipped: outcome.skipped,
97 skippedDomains: outcome.skippedDomains,
98 targetName: outcome.targetName,
99 };
100 }
101 // A browser that reopened mid-import routes back to the quit screen; every
102 // other failure surfaces on the blocked screen, which offers a retry when
103 // one could help.
104 if (outcome.reason === "browserRunning") return { step: "quit" };
105 return { step: "blocked", reason: outcome.reason };
106}
107
108/** Where a fresh availability check lands the wizard after the user quits. */
109export function refreshedSourceStep(source: BrowserImportSource | undefined): WizardStep {
110 if (source === undefined) return { step: "blocked", reason: "unknownSource" };
111 return initialWizardStep(source);
112}
113
114/** Preserve the chosen source profile when a post-quit refresh still lists it. */
115export function refreshedSourceProfileDirectory(
116 currentDirectory: string,
117 source: BrowserImportSource,
118): string {
119 if (source.profiles.some((profile) => profile.directory === currentDirectory)) {
120 return currentDirectory;
121 }
122 return source.profiles[0]?.directory ?? "";
123}
124
125/**
126 * Whether retrying could clear a failure. The keychain prompt can be approved
127 * on a second try, a missing key appears once the user signs in to the
128 * browser (which is what its copy asks for), and a read or session error may
129 * be transient; an unsupported browser will not change, so it gets no retry
130 * button.
131 */
132export function isRetryableReason(reason: BrowserImportFailureReason): boolean {
133 switch (reason) {
134 case "needsKeychainApproval":
135 case "keychainItemMissing":
136 case "keychainUnavailable":
137 case "readFailed":
138 case "sessionUnavailable":
139 case "profileNotSaved":
140 return true;
141 default:
142 return false;
143 }
144}
145
146/**
147 * Names the sites whose cookies were skipped: "example.com and google.com",
148 * or "a, b, c and 4 more" past a few, so the line stays short.
149 */
150export function formatSkippedDomains(domains: ReadonlyArray<string>): string {
151 if (domains.length === 0) return "";
152 if (domains.length === 1) return domains[0]!;
153 if (domains.length <= 3) return `${domains.slice(0, -1).join(", ")} and ${domains.at(-1)}`;
154 return `${domains.slice(0, 3).join(", ")} and ${domains.length - 3} more`;
155}