Open History to see the last commit
1import type { ConfirmDialogOptions, ConfirmDialogVariant } from "@t3tools/contracts";
2
3export type ConfirmDialogState =
4 | { readonly status: "idle" }
5 | {
6 readonly status: "confirming";
7 readonly message: string;
8 readonly variant: ConfirmDialogVariant;
9 }
10 | {
11 readonly status: "closing";
12 readonly message: string;
13 readonly variant: ConfirmDialogVariant;
14 };
15
16type PendingConfirmation = {
17 readonly message: string;
18 readonly variant: ConfirmDialogVariant;
19 readonly resolve: (confirmed: boolean) => void;
20};
21
22const idleState: ConfirmDialogState = { status: "idle" };
23let state: ConfirmDialogState = idleState;
24let activeConfirmation: PendingConfirmation | null = null;
25let queuedConfirmations: PendingConfirmation[] = [];
26let registeredHostCount = 0;
27const listeners = new Set<() => void>();
28
29function publish(next: ConfirmDialogState): void {
30 state = next;
31 for (const listener of listeners) {
32 listener();
33 }
34}
35
36function resolvePendingConfirmations(confirmed: boolean): void {
37 activeConfirmation?.resolve(confirmed);
38 for (const confirmation of queuedConfirmations) {
39 confirmation.resolve(confirmed);
40 }
41 activeConfirmation = null;
42 queuedConfirmations = [];
43}
44
45export function readConfirmDialogState(): ConfirmDialogState {
46 return state;
47}
48
49export function subscribeConfirmDialog(listener: () => void): () => void {
50 listeners.add(listener);
51 return () => {
52 listeners.delete(listener);
53 };
54}
55
56export function registerConfirmDialogHost(): () => void {
57 registeredHostCount += 1;
58 let registered = true;
59
60 return () => {
61 if (!registered) return;
62 registered = false;
63 registeredHostCount = Math.max(0, registeredHostCount - 1);
64
65 if (registeredHostCount === 0) {
66 resolvePendingConfirmations(false);
67 publish(idleState);
68 }
69 };
70}
71
72/**
73 * An undefined result means no themed host is currently available.
74 */
75export function requestConfirmDialog(
76 message: string,
77 options?: ConfirmDialogOptions,
78): Promise<boolean> | undefined {
79 if (registeredHostCount === 0) return undefined;
80
81 const confirmation = new Promise<boolean>((resolve) => {
82 const pending = {
83 message,
84 variant: options?.variant ?? "default",
85 resolve,
86 } satisfies PendingConfirmation;
87 if (activeConfirmation || state.status === "closing") {
88 queuedConfirmations.push(pending);
89 return;
90 }
91
92 activeConfirmation = pending;
93 publish({ status: "confirming", message, variant: pending.variant });
94 });
95
96 return confirmation;
97}
98
99export function isConfirmDialogActive(): boolean {
100 return state.status !== "idle" || activeConfirmation !== null || queuedConfirmations.length > 0;
101}
102
103export function respondToConfirmDialog(confirmed: boolean): void {
104 if (state.status !== "confirming" || !activeConfirmation) return;
105
106 const confirmation = activeConfirmation;
107 activeConfirmation = null;
108 confirmation.resolve(confirmed);
109 publish({ status: "closing", message: state.message, variant: state.variant });
110}
111
112export function completeConfirmDialogClose(): void {
113 if (state.status !== "closing") return;
114
115 const next = queuedConfirmations.shift();
116 if (!next) {
117 publish(idleState);
118 return;
119 }
120
121 activeConfirmation = next;
122 publish({ status: "confirming", message: next.message, variant: next.variant });
123}
124
125export function resetConfirmDialogForTests(): void {
126 resolvePendingConfirmations(false);
127 registeredHostCount = 0;
128 publish(idleState);
129 listeners.clear();
130}