/** * DialogProvider 组件 * * 全局弹窗提供者组件 * 提供统一的 confirm、alert、prompt 方法 * 通过 Context 让所有子组件可以使用统一弹窗 * * @created 2025-11-22 * @component */ 'use client' import { createContext, useContext, ReactNode } from 'react' import ConfirmDialog from './ConfirmDialog' import AlertDialog from './AlertDialog' import PromptDialog from './PromptDialog' import { useConfirm, usePrompt } from '@/hooks/useConfirm' import { useAlert } from '@/hooks/useAlert' interface DialogContextType { confirm: (options: { title?: string message: string confirmText?: string cancelText?: string variant?: 'default' | 'danger' }) => Promise alert: (options: { title?: string message: string buttonText?: string variant?: 'default' | 'success' | 'warning' | 'error' } | string) => void prompt: (options: { title?: string message: string defaultValue?: string placeholder?: string confirmText?: string cancelText?: string }) => Promise } const DialogContext = createContext(null) export function useDialog() { const context = useContext(DialogContext) if (!context) { throw new Error('useDialog must be used within DialogProvider') } return context } interface DialogProviderProps { children: ReactNode } export function DialogProvider({ children }: DialogProviderProps) { const confirmHook = useConfirm() const alertHook = useAlert() const promptHook = usePrompt() const confirm = async (options: { title?: string message: string confirmText?: string cancelText?: string variant?: 'default' | 'danger' }): Promise => { return confirmHook.confirm(options) } const alert = (options: { title?: string message: string buttonText?: string variant?: 'default' | 'success' | 'warning' | 'error' } | string) => { alertHook.alert(options) } const prompt = async (options: { title?: string message: string defaultValue?: string placeholder?: string confirmText?: string cancelText?: string }): Promise => { return promptHook.prompt(options) } return ( {children} ) }