Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

confirmDialog.ts

128 lines3.4 KB

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 }
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 = [];
44​
45export function readConfirmDialogState(): ConfirmDialogState {
46 return state;
48​
49export function subscribeConfirmDialog(listener: () => void): () => void {
50 listeners.add(listener);
51 return () => {
52 listeners.delete(listener);
53 };
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 };
71​
72/** Undefined means no themed host is available. */
73export function requestConfirmDialog(
74 message: string,
75 options?: ConfirmDialogOptions,
76): Promise<boolean> | undefined {
77 if (registeredHostCount === 0) return undefined;
78​
79 const confirmation = new Promise<boolean>((resolve) => {
80 const pending = {
81 message,
82 variant: options?.variant ?? "default",
83 resolve,
84 } satisfies PendingConfirmation;
85 if (activeConfirmation || state.status === "closing") {
86 queuedConfirmations.push(pending);
87 return;
88 }
89​
90 activeConfirmation = pending;
91 publish({ status: "confirming", message, variant: pending.variant });
92 });
93​
94 return confirmation;
96​
97export function isConfirmDialogActive(): boolean {
98 return state.status !== "idle" || activeConfirmation !== null || queuedConfirmations.length > 0;
100​
101export function respondToConfirmDialog(confirmed: boolean): void {
102 if (state.status !== "confirming" || !activeConfirmation) return;
103​
104 const confirmation = activeConfirmation;
105 activeConfirmation = null;
106 confirmation.resolve(confirmed);
107 publish({ status: "closing", message: state.message, variant: state.variant });
109​
110export function completeConfirmDialogClose(): void {
111 if (state.status !== "closing") return;
112​
113 const next = queuedConfirmations.shift();
114 if (!next) {
115 publish(idleState);
116 return;
117 }
118​
119 activeConfirmation = next;
120 publish({ status: "confirming", message: next.message, variant: next.variant });
122​
123export function resetConfirmDialogForTests(): void {
124 resolvePendingConfirmations(false);
125 registeredHostCount = 0;
126 publish(idleState);
127 listeners.clear();