import { createContext, useCallback, useContext, useRef, useState, type ReactNode } from 'react' import { Modal } from './Modal' export type ConfirmOptions = { title?: string message: string confirmLabel?: string cancelLabel?: string danger?: boolean } type ConfirmFn = (opts: ConfirmOptions) => Promise const ConfirmContext = createContext(null) // Drop-in async replacement for window.confirm(): `await confirm({ message })`. export function useConfirm(): ConfirmFn { const ctx = useContext(ConfirmContext) if (!ctx) throw new Error('useConfirm must be used within ') return ctx } export function ConfirmProvider({ children }: { children: ReactNode }) { const [opts, setOpts] = useState(null) const resolver = useRef<(v: boolean) => void>(() => {}) const confirm = useCallback((o) => { setOpts(o) return new Promise((resolve) => { resolver.current = resolve }) }, []) const finish = useCallback((result: boolean) => { resolver.current(result) resolver.current = () => {} setOpts(null) }, []) return ( {children} finish(false)}> {opts && (
{ if (e.key === 'Enter') { e.preventDefault() finish(true) } }} >

{opts.message}

)}
) }