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[] = [];
26const listeners = new Set<() => void>();
27
28function publish(next: ConfirmDialogState): void {
29 state = next;
30 for (const listener of listeners) {
31 listener();
32 }
33}
34
35function resolvePendingConfirmations(confirmed: boolean): void {
36 activeConfirmation?.resolve(confirmed);
37 for (const confirmation of queuedConfirmations) {
38 confirmation.resolve(confirmed);
39 }
40 activeConfirmation = null;
41 queuedConfirmations = [];
42}
43
44export function readConfirmDialogState(): ConfirmDialogState {
45 return state;
46}
47
48/**
49 * Only themed hosts subscribe, so a subscriber is a mounted host. When the last
50 * one leaves, pending confirmations resolve as cancelled.
51 */
52export function subscribeConfirmDialogHost(listener: () => void): () => void {
53 listeners.add(listener);
54 return () => {
55 if (!listeners.delete(listener) || listeners.size > 0) return;
56 resolvePendingConfirmations(false);
57 publish(idleState);
58 };
59}
60
61/** Undefined means no themed host is available. */
62export function requestConfirmDialog(
63 message: string,
64 options?: ConfirmDialogOptions,
65): Promise<boolean> | undefined {
66 if (listeners.size === 0) return undefined;
67
68 const confirmation = new Promise<boolean>((resolve) => {
69 const pending = {
70 message,
71 variant: options?.variant ?? "default",
72 resolve,
73 } satisfies PendingConfirmation;
74 if (activeConfirmation || state.status === "closing") {
75 queuedConfirmations.push(pending);
76 return;
77 }
78
79 activeConfirmation = pending;
80 publish({ status: "confirming", message, variant: pending.variant });
81 });
82
83 return confirmation;
84}
85
86export function isConfirmDialogActive(): boolean {
87 return state.status !== "idle" || activeConfirmation !== null || queuedConfirmations.length > 0;
88}
89
90export function respondToConfirmDialog(confirmed: boolean): void {
91 if (state.status !== "confirming" || !activeConfirmation) return;
92
93 const confirmation = activeConfirmation;
94 activeConfirmation = null;
95 confirmation.resolve(confirmed);
96 publish({ status: "closing", message: state.message, variant: state.variant });
97}
98
99export function completeConfirmDialogClose(): void {
100 if (state.status !== "closing") return;
101
102 const next = queuedConfirmations.shift();
103 if (!next) {
104 publish(idleState);
105 return;
106 }
107
108 activeConfirmation = next;
109 publish({ status: "confirming", message: next.message, variant: next.variant });
110}
111
112export function resetConfirmDialogForTests(): void {
113 listeners.clear();
114 resolvePendingConfirmations(false);
115 publish(idleState);
116}