Open History to see the last commit
1import {
2 type AtomCommandResult,
3 isAtomCommandInterrupted,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6import { create } from "zustand";
7
8import { stackedThreadToast, toastManager } from "../components/ui/toast";
9import * as ThreadUndo from "./threadUndo";
10
11type UndoOptions = {
12 action: "Settled" | "Snoozed" | "Unpinned" | "Archived" | "Discarded";
13 undo: () => Promise<AtomCommandResult<unknown, unknown>>;
14 failureTitle: string;
15 claim: ReturnType<typeof ThreadUndo.begin>;
16 commit?: () => void;
17};
18
19type UndoNotice = {
20 action: UndoOptions["action"];
21 count: number;
22 undo: () => Promise<void>;
23};
24
25export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => ({ notice: null }));
26
27let liveUndos: UndoOptions[] = [];
28let expiry: ReturnType<typeof setTimeout> | undefined;
29
30function refreshNotice() {
31 const stale = liveUndos.filter(({ claim }) => !claim.isCurrent());
32 liveUndos = liveUndos.filter((entry) => !stale.includes(entry));
33 for (const entry of stale) entry.commit?.();
34 const latest = liveUndos.at(-1);
35 if (!latest) {
36 clearTimeout(expiry);
37 useThreadUndoNotice.setState({ notice: null });
38 return;
39 }
40 const group: UndoOptions[] = [];
41 for (let index = liveUndos.length - 1; index >= 0; index--) {
42 const entry = liveUndos[index]!;
43 if (entry.action !== latest.action) break;
44 group.push(entry);
45 }
46 useThreadUndoNotice.setState({
47 notice: {
48 action: latest.action,
49 count: group.length,
50 undo: async () => {
51 const current = group.filter(
52 (entry) => liveUndos.includes(entry) && entry.claim.isCurrent(),
53 );
54 liveUndos = liveUndos.filter((entry) => !group.includes(entry));
55 // Consume every claim before awaiting, so repeated clicks or shortcuts
56 // cannot restore the same group twice.
57 for (const entry of current) entry.claim.finish();
58 refreshNotice();
59 await Promise.all(
60 current.map(async ({ undo, failureTitle }) => {
61 const reportFailure = (error: unknown) => {
62 toastManager.add(
63 stackedThreadToast({
64 type: "error",
65 title: failureTitle,
66 description: error instanceof Error ? error.message : "An error occurred.",
67 }),
68 );
69 };
70 try {
71 const result = await undo();
72 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
73 reportFailure(squashAtomCommandFailure(result));
74 }
75 } catch (error) {
76 reportFailure(error);
77 }
78 }),
79 );
80 },
81 },
82 });
83}
84
85ThreadUndo.subscribe(refreshNotice);
86
87export function undoLatestThreadAction(): boolean {
88 const notice = useThreadUndoNotice.getState().notice;
89 if (!notice) return false;
90 void notice.undo();
91 return true;
92}
93
94export function showThreadUndoNotice(options: UndoOptions) {
95 if (!options.claim.isCurrent()) return;
96 liveUndos.push(options);
97 refreshNotice();
98 clearTimeout(expiry);
99 expiry = setTimeout(() => {
100 const expired = liveUndos;
101 liveUndos = [];
102 for (const { claim, commit } of expired) {
103 claim.finish();
104 commit?.();
105 }
106 refreshNotice();
107 }, 5_000);
108}