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";
13 undo: () => Promise<AtomCommandResult<unknown, unknown>>;
14 failureTitle: string;
15 claim: ReturnType<typeof ThreadUndo.begin>;
16};
17
18type UndoNotice = {
19 action: UndoOptions["action"];
20 count: number;
21 undo: () => Promise<void>;
22};
23
24export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => ({ notice: null }));
25
26// Shared across sidebar, header and menu actions. Consecutive actions of the
27// same kind share one notice and can be restored together.
28let liveUndos: UndoOptions[] = [];
29let expiry: ReturnType<typeof setTimeout> | undefined;
30
31function refreshNotice() {
32 liveUndos = liveUndos.filter(({ claim }) => claim.isCurrent());
33 const latest = liveUndos.at(-1);
34 if (!latest) {
35 clearTimeout(expiry);
36 useThreadUndoNotice.setState({ notice: null });
37 return;
38 }
39 const group: UndoOptions[] = [];
40 for (let index = liveUndos.length - 1; index >= 0; index--) {
41 const entry = liveUndos[index]!;
42 if (entry.action !== latest.action) break;
43 group.push(entry);
44 }
45 useThreadUndoNotice.setState({
46 notice: {
47 action: latest.action,
48 count: group.length,
49 undo: async () => {
50 const current = group.filter(
51 (entry) => liveUndos.includes(entry) && entry.claim.isCurrent(),
52 );
53 liveUndos = liveUndos.filter((entry) => !group.includes(entry));
54 // Consume every claim before awaiting, so repeated clicks or shortcuts
55 // cannot restore the same group twice.
56 for (const entry of current) entry.claim.finish();
57 refreshNotice();
58 await Promise.all(
59 current.map(async ({ undo, failureTitle }) => {
60 const reportFailure = (error: unknown) => {
61 toastManager.add(
62 stackedThreadToast({
63 type: "error",
64 title: failureTitle,
65 description: error instanceof Error ? error.message : "An error occurred.",
66 }),
67 );
68 };
69 try {
70 const result = await undo();
71 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
72 reportFailure(squashAtomCommandFailure(result));
73 }
74 } catch (error) {
75 reportFailure(error);
76 }
77 }),
78 );
79 },
80 },
81 });
82}
83
84ThreadUndo.subscribe(refreshNotice);
85
86/** Runs the group displayed in the sidebar; false when nothing is left to undo. */
87export function undoLatestThreadAction(): boolean {
88 const notice = useThreadUndoNotice.getState().notice;
89 if (!notice) return false;
90 void notice.undo();
91 return true;
92}
93
94/** Shows one compact confirmation for the currently undoable thread actions. */
95export function showThreadUndoNotice(options: UndoOptions) {
96 if (!options.claim.isCurrent()) return;
97 liveUndos.push(options);
98 refreshNotice();
99 clearTimeout(expiry);
100 expiry = setTimeout(() => {
101 const expired = liveUndos;
102 liveUndos = [];
103 for (const { claim } of expired) claim.finish();
104 refreshNotice();
105 }, 5_000);
106}