"use client"; import { createContext, useCallback, useContext, useState, type ReactNode, } from "react"; import { Modal } from "@/components/ui/modal"; import { Button } from "@/components/ui/button"; type ConfirmOpts = { title: string; message?: string; confirmLabel?: string; destructive?: boolean; }; type ConfirmFn = (opts: ConfirmOpts) => Promise; const ConfirmContext = createContext(null); /** Imperative confirm — `if (await confirm({...})) { … }`. Renders a styled * modal instead of the browser's window.confirm. Provided by ConfirmProvider. */ export function useConfirm(): ConfirmFn { const fn = useContext(ConfirmContext); if (!fn) throw new Error("useConfirm must be used within a ConfirmProvider"); return fn; } export function ConfirmProvider({ children }: { children: ReactNode }) { const [pending, setPending] = useState<{ opts: ConfirmOpts; resolve: (v: boolean) => void; } | null>(null); const confirm = useCallback( (opts) => new Promise((resolve) => setPending({ opts, resolve })), [], ); function settle(value: boolean) { pending?.resolve(value); setPending(null); } return ( {children} settle(false)} title={pending?.opts.title ?? ""} > {pending ? (
{pending.opts.message ? (

{pending.opts.message}

) : null}
) : null}
); }