import { createContext, useCallback, useContext, useState, type ReactNode } from "react"; import { useTranslation } from "react-i18next"; import Modal from "./Modal"; // App-styled, promise-based replacement for window.confirm. Wrap the tree in // once, then anywhere: const confirm = useConfirm(); // if (await confirm({ message: "…", danger: true })) { … } export interface ConfirmOptions { title?: string; message: ReactNode; confirmLabel?: string; cancelLabel?: string; danger?: boolean; } type ConfirmFn = (opts: ConfirmOptions) => Promise; const ConfirmContext = createContext(() => Promise.resolve(false)); export function useConfirm(): ConfirmFn { return useContext(ConfirmContext); } export function ConfirmProvider({ children }: { children: ReactNode }) { const { t } = useTranslation(); const [state, setState] = useState<{ opts: ConfirmOptions; resolve: (ok: boolean) => void; } | null>(null); const confirm = useCallback( (opts) => new Promise((resolve) => setState({ opts, resolve })), [] ); function close(ok: boolean) { state?.resolve(ok); setState(null); } return ( {children} {state && ( close(false)} maxWidth="max-w-sm" >

{state.opts.message}

)}
); }