Open History to see the last commit
1// Shared across hook instances so sidebar, header and menu actions invalidate each other.
2const currentActions = new Map<string, symbol>();
3const listeners = new Set<() => void>();
4
5export function subscribe(listener: () => void) {
6 listeners.add(listener);
7 return () => {
8 listeners.delete(listener);
9 };
10}
11
12function notify() {
13 for (const listener of listeners) listener();
14}
15
16export function begin(kind: string, threadKey: string) {
17 const key = JSON.stringify([kind, threadKey]);
18 const token = Symbol();
19 currentActions.set(key, token);
20 notify();
21 const isCurrent = () => currentActions.get(key) === token;
22 return {
23 isCurrent,
24 finish: () => {
25 if (isCurrent()) {
26 currentActions.delete(key);
27 notify();
28 }
29 },
30 };
31}
32
33export function invalidate(kind: string, threadKey: string) {
34 currentActions.delete(JSON.stringify([kind, threadKey]));
35 notify();
36}
37
38export type ThreadUndoStep =
39 | { readonly action: "unsettle"; readonly restoreUnsettledAt?: string | null }
40 | { readonly action: "unsnooze" }
41 /** `orderKey: null` restores a pin saved without a manual order; omitted pins at the top. */
42 | { readonly action: "pin"; readonly orderKey?: string | null }
43 | { readonly action: "reorderActive"; readonly orderKey: string }
44 | { readonly action: "snooze"; readonly snoozedUntil: string };
45
46export type ThreadUndoPlan = readonly [ThreadUndoStep, ...ThreadUndoStep[]];
47
48export function planThreadUndo(
49 action: "settle" | "snooze" | "unpin",
50 thread?: {
51 readonly pinnedAt?: string | null;
52 readonly pinOrderKey?: string | null;
53 readonly activeOrderKey?: string | null;
54 readonly unsettledAt?: string | null;
55 readonly snoozedUntil?: string | null;
56 } | null,
57): ThreadUndoPlan {
58 if (action === "snooze") return [{ action: "unsnooze" }];
59 const pin: ThreadUndoStep = {
60 action: "pin",
61 ...(thread != null ? { orderKey: thread.pinOrderKey ?? null } : {}),
62 };
63 // Pinning clears snooze on the server; restore it after restoring pin placement.
64 const snooze =
65 thread?.snoozedUntil != null
66 ? [{ action: "snooze" as const, snoozedUntil: thread.snoozedUntil }]
67 : [];
68 if (action === "unpin") return [pin, ...snooze];
69 // Settling clears a manual active position; pinned threads are placed by their pin instead.
70 const placement: ThreadUndoStep[] =
71 thread?.pinnedAt != null
72 ? [pin]
73 : thread?.activeOrderKey != null
74 ? [{ action: "reorderActive", orderKey: thread.activeOrderKey }]
75 : [];
76 const unsettle: ThreadUndoStep =
77 thread == null
78 ? { action: "unsettle" }
79 : { action: "unsettle", restoreUnsettledAt: thread.unsettledAt ?? null };
80 return [unsettle, ...placement, ...snooze];
81}
82
83/**
84 * True when a step cancelled a change that was still waiting to sync. The server never applied
85 * it, so the rest of the plan has nothing to restore; see `restoresAfterDeviceCancel`.
86 */
87export function cancelledOnDevice(value: unknown): boolean {
88 return typeof value === "object" && value !== null && "cancelledOnDevice" in value;
89}
90
91/**
92 * Whether a step still runs after the change it undoes was cancelled on the device. Only a
93 * snooze can: the cancelled settle replaced a snooze queued on the device, which went with it.
94 * `current` is the thread as shown now; a snooze it still holds needs nothing.
95 */
96export function restoresAfterDeviceCancel(
97 step: ThreadUndoStep,
98 current: { readonly snoozedUntil?: string | null } | null | undefined,
99): boolean {
100 return step.action === "snooze" && current?.snoozedUntil !== step.snoozedUntil;
101}
102
103export async function executeThreadUndo<Result>(
104 plan: ThreadUndoPlan,
105 execute: (step: ThreadUndoStep) => Promise<Result>,
106 succeeded: (result: Result) => boolean,
107): Promise<Result> {
108 let result = await execute(plan[0]);
109 for (const step of plan.slice(1)) {
110 if (!succeeded(result)) return result;
111 // A snooze can expire before Undo runs, and the server rejects an expired restore.
112 // @effect-diagnostics-next-line globalDate:off
113 if (step.action === "snooze" && Date.parse(step.snoozedUntil) <= Date.now()) continue;
114 result = await execute(step);
115 }
116 return result;
117}