"use client" import { useState, createContext, useContext, ReactNode } from "react" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" type DialogType = "alert" | "confirm" | "prompt" interface DialogConfig { type: DialogType title?: string message: string defaultValue?: string confirmText?: string cancelText?: string variant?: "default" | "destructive" onConfirm?: (value?: string) => void onCancel?: () => void } interface DialogContextType { showAlert: (message: string, title?: string) => Promise showConfirm: (message: string, title?: string, variant?: "default" | "destructive") => Promise showPrompt: (message: string, defaultValue?: string, title?: string) => Promise } const DialogContext = createContext(undefined) export function DialogProvider({ children }: { children: ReactNode }) { const [open, setOpen] = useState(false) const [config, setConfig] = useState(null) const [inputValue, setInputValue] = useState("") const showAlert = (message: string, title?: string): Promise => { return new Promise((resolve) => { setConfig({ type: "alert", title: title || "提示", message, confirmText: "确定", onConfirm: () => { setOpen(false) // 等待模态框动画完成后再 resolve setTimeout(() => resolve(), 150) }, }) setOpen(true) }) } const showConfirm = ( message: string, title?: string, variant: "default" | "destructive" = "default" ): Promise => { return new Promise((resolve) => { setConfig({ type: "confirm", title: title || "确认", message, confirmText: "确定", cancelText: "取消", variant, onConfirm: () => { setOpen(false) // 等待模态框动画完成后再 resolve setTimeout(() => resolve(true), 150) }, onCancel: () => { setOpen(false) // 等待模态框动画完成后再 resolve setTimeout(() => resolve(false), 150) }, }) setOpen(true) }) } const showPrompt = ( message: string, defaultValue: string = "", title?: string ): Promise => { return new Promise((resolve) => { setInputValue(defaultValue) setConfig({ type: "prompt", title: title || "输入", message, defaultValue, confirmText: "确定", cancelText: "取消", onConfirm: (value) => { setOpen(false) // 等待模态框动画完成后再 resolve setTimeout(() => resolve(value || null), 150) }, onCancel: () => { setOpen(false) // 等待模态框动画完成后再 resolve setTimeout(() => resolve(null), 150) }, }) setOpen(true) }) } const handleConfirm = () => { if (config?.type === "prompt") { config.onConfirm?.(inputValue) } else { config?.onConfirm?.() } } const handleCancel = () => { config?.onCancel?.() } return ( {children} {config?.title} {config?.message} {config?.type === "prompt" && (
setInputValue(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleConfirm() } }} autoFocus />
)} {config?.type !== "alert" && ( {config?.cancelText || "取消"} )} {config?.confirmText || "确定"}
) } export function useDialog() { const context = useContext(DialogContext) if (!context) { throw new Error("useDialog must be used within a DialogProvider") } return context }