import { createContext, useCallback, useContext, useState, type ReactNode, } from "react"; import { useI18n } from "../lib/i18n.js"; import { Modal } from "./feedback.js"; import { IconAction } from "./IconAction.js"; import { CloseIcon, TrashIcon, MoveIcon } from "./icons.js"; export interface ConfirmOptions { title?: string; message: ReactNode; confirmLabel?: string; cancelLabel?: string; tone?: "default" | "danger"; confirmIcon?: ReactNode; } type ConfirmFn = (options: ConfirmOptions) => Promise; interface PendingConfirm { options: ConfirmOptions; resolve: (value: boolean) => void; } const ConfirmContext = createContext(null); export const ConfirmProvider = ({ children }: { children: ReactNode }) => { const { t } = useI18n(); const [pending, setPending] = useState(null); const confirm = useCallback( (options) => new Promise((resolve) => setPending({ options, resolve })), [], ); const answer = (value: boolean) => { if (pending) pending.resolve(value); setPending(null); }; const tone = pending?.options.tone ?? "default"; return ( {children} answer(false)} >

{pending?.options.message}

} className="w-full" onClick={() => answer(false)} /> : ) } className="w-full bg-accent text-accent-foreground hover:bg-accent" onClick={() => answer(true)} />
); }; export const useConfirm = (): ConfirmFn => { const context = useContext(ConfirmContext); if (!context) throw new Error("useConfirm doit etre utilise sous ConfirmProvider"); return context; };