|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from "react"; |
| 4 | + |
| 5 | +interface DialogOptions { |
| 6 | + title?: string; |
| 7 | + confirmLabel?: string; |
| 8 | + /** Styles the confirm button red for irreversible actions such as deletes. */ |
| 9 | + destructive?: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +interface DialogApi { |
| 13 | + /** In-app replacement for `window.alert`; resolves when dismissed. */ |
| 14 | + alert(message: string, options?: DialogOptions): Promise<void>; |
| 15 | + /** In-app replacement for `window.confirm`; resolves true on confirm, false on cancel. */ |
| 16 | + confirm(message: string, options?: DialogOptions): Promise<boolean>; |
| 17 | +} |
| 18 | + |
| 19 | +interface OpenDialog extends DialogOptions { |
| 20 | + id: number; |
| 21 | + kind: "alert" | "confirm"; |
| 22 | + message: string; |
| 23 | + resolve(confirmed: boolean): void; |
| 24 | +} |
| 25 | + |
| 26 | +const DialogContext = createContext<DialogApi | null>(null); |
| 27 | + |
| 28 | +function Dialog({ dialog, onClose }: { dialog: OpenDialog; onClose(confirmed: boolean): void }) { |
| 29 | + const confirmRef = useRef<HTMLButtonElement>(null); |
| 30 | + const isConfirm = dialog.kind === "confirm"; |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + confirmRef.current?.focus(); |
| 34 | + // Capture phase so Esc/Enter reach the dialog before page shortcuts and inputs behind it. |
| 35 | + const onKeyDown = (event: KeyboardEvent) => { |
| 36 | + if (event.key !== "Escape" && event.key !== "Enter") return; |
| 37 | + event.preventDefault(); |
| 38 | + event.stopPropagation(); |
| 39 | + onClose(event.key === "Enter"); |
| 40 | + }; |
| 41 | + window.addEventListener("keydown", onKeyDown, true); |
| 42 | + return () => window.removeEventListener("keydown", onKeyDown, true); |
| 43 | + }, [onClose]); |
| 44 | + |
| 45 | + const confirmColor = dialog.destructive ? "bg-red-600 hover:bg-red-700" : "bg-blue-600 hover:bg-blue-700"; |
| 46 | + |
| 47 | + return ( |
| 48 | + <div |
| 49 | + className="fixed inset-0 z-[70] bg-black/60 flex items-center justify-center p-4 motion-safe:animate-[bft-fade-in_150ms_ease-out]" |
| 50 | + onMouseDown={(event) => { |
| 51 | + if (event.target === event.currentTarget) onClose(false); |
| 52 | + }} |
| 53 | + role="presentation" |
| 54 | + > |
| 55 | + <div |
| 56 | + role="alertdialog" |
| 57 | + aria-modal="true" |
| 58 | + aria-labelledby={dialog.title ? "app-dialog-title" : undefined} |
| 59 | + aria-describedby="app-dialog-message" |
| 60 | + className="bg-gray-800 rounded-2xl p-6 max-w-md w-full border border-gray-700 shadow-2xl motion-safe:animate-[bft-sheet-in_200ms_ease-out]" |
| 61 | + > |
| 62 | + {dialog.title ? ( |
| 63 | + <h2 id="app-dialog-title" className="text-lg font-bold text-white mb-3"> |
| 64 | + {dialog.title} |
| 65 | + </h2> |
| 66 | + ) : null} |
| 67 | + <p id="app-dialog-message" className="text-gray-300 mb-6 whitespace-pre-line"> |
| 68 | + {dialog.message} |
| 69 | + </p> |
| 70 | + <div className="flex gap-3 justify-end"> |
| 71 | + {isConfirm ? ( |
| 72 | + <button |
| 73 | + onClick={() => onClose(false)} |
| 74 | + className="px-4 py-2 bg-gray-700 hover:bg-gray-600 text-white rounded-lg font-medium transition-colors" |
| 75 | + > |
| 76 | + Cancel |
| 77 | + </button> |
| 78 | + ) : null} |
| 79 | + <button |
| 80 | + ref={confirmRef} |
| 81 | + onClick={() => onClose(true)} |
| 82 | + className={`px-4 py-2 ${confirmColor} text-white rounded-lg font-medium transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-400`} |
| 83 | + > |
| 84 | + {dialog.confirmLabel ?? (isConfirm ? "Confirm" : "OK")} |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export function DialogProvider({ children }: { children: ReactNode }) { |
| 93 | + // A queue, so a second dialog requested while one is open waits its turn instead of replacing it. |
| 94 | + const [queue, setQueue] = useState<OpenDialog[]>([]); |
| 95 | + const nextId = useRef(0); |
| 96 | + |
| 97 | + const open = useCallback((kind: OpenDialog["kind"], message: string, options?: DialogOptions) => { |
| 98 | + const id = nextId.current++; |
| 99 | + return new Promise<boolean>((resolve) => setQueue((current) => [...current, { ...options, id, kind, message, resolve }])); |
| 100 | + }, []); |
| 101 | + |
| 102 | + const api = useMemo<DialogApi>( |
| 103 | + () => ({ |
| 104 | + alert: async (message, options) => { |
| 105 | + await open("alert", message, options); |
| 106 | + }, |
| 107 | + confirm: (message, options) => open("confirm", message, options), |
| 108 | + }), |
| 109 | + [open], |
| 110 | + ); |
| 111 | + |
| 112 | + const current = queue[0]; |
| 113 | + const close = useCallback( |
| 114 | + (confirmed: boolean) => { |
| 115 | + current?.resolve(confirmed); |
| 116 | + setQueue((pending) => pending.filter((dialog) => dialog.id !== current?.id)); |
| 117 | + }, |
| 118 | + [current], |
| 119 | + ); |
| 120 | + |
| 121 | + return ( |
| 122 | + <DialogContext.Provider value={api}> |
| 123 | + {children} |
| 124 | + {current ? <Dialog key={current.id} dialog={current} onClose={close} /> : null} |
| 125 | + </DialogContext.Provider> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +export function useDialog(): DialogApi { |
| 130 | + const context = useContext(DialogContext); |
| 131 | + if (!context) throw new Error("useDialog must be used inside <DialogProvider>"); |
| 132 | + return context; |
| 133 | +} |
0 commit comments