"use client" import { create } from "zustand" import { Trash2 } from "lucide-react" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogMedia, AlertDialogTitle, } from "@/shared/components/ui/alert-dialog" // ── Types ── export type ConfirmOptions = { title?: string description?: string confirmLabel?: string cancelLabel?: string variant?: "destructive" | "default" } type ConfirmStore = { open: boolean options: ConfirmOptions resolve: ((value: boolean) => void) | null _show: (options: ConfirmOptions) => Promise _close: (confirmed: boolean) => void } // ── Store ── const useConfirmStore = create((set, get) => ({ open: false, options: {}, resolve: null, _show: (options) => new Promise((resolve) => { set({ open: true, options, resolve }) }), _close: (confirmed) => { const { resolve } = get() resolve?.(confirmed) set({ open: false, resolve: null }) }, })) // ── Imperative API (usage: `await confirm({ ... })`) ── export function confirm(options: ConfirmOptions = {}) { if (process.env.NODE_ENV === "development") { const state = useConfirmStore.getState() if (state.open) { console.warn("[ConfirmDialog] A confirm dialog is already open. Nested confirms are not supported.") } // Detect missing mount: if `resolve` is never set after a tick, the dialog component is not mounted. const result = state._show(options) setTimeout(() => { const current = useConfirmStore.getState() if (current.open && current.resolve === null) { console.warn( "[ConfirmDialog] confirm() was called but does not appear to be mounted. " + "Make sure is rendered in your root layout.", ) } }, 100) return result } return useConfirmStore.getState()._show(options) } // ── Dialog component (mount once in the root layout) ── export function ConfirmDialog() { const { open, options, _close } = useConfirmStore() const isDestructive = options.variant === "destructive" return ( { if (!v) _close(false) }} > {isDestructive && ( )} {options.title ?? "Are you sure?"} {options.description && ( {options.description} )} _close(false)}> {options.cancelLabel ?? "Cancel"} _close(true)} > {options.confirmLabel ?? "Confirm"} ) }