'use client'; import { useCallback, useState } from 'react'; /** * 确认对话框配置 */ export interface ConfirmOptions { title?: string; message: string; confirmText?: string; cancelText?: string; type?: 'info' | 'warning' | 'danger'; } /** * 确认对话框状态 */ interface ConfirmState { isOpen: boolean; options: ConfirmOptions | null; resolve: ((value: boolean) => void) | null; } /** * 确认对话框 Hook * 用于显示确认对话框并等待用户响应 */ export function useConfirm() { const [state, setState] = useState({ isOpen: false, options: null, resolve: null, }); const confirm = useCallback((options: ConfirmOptions): Promise => { return new Promise((resolve) => { setState({ isOpen: true, options: { title: '确认', confirmText: '确定', cancelText: '取消', type: 'info', ...options, }, resolve, }); }); }, []); const handleConfirm = useCallback(() => { if (state.resolve) { state.resolve(true); } setState({ isOpen: false, options: null, resolve: null }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [state.resolve]); const handleCancel = useCallback(() => { if (state.resolve) { state.resolve(false); } setState({ isOpen: false, options: null, resolve: null }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [state.resolve]); return { confirm, isOpen: state.isOpen, options: state.options, handleConfirm, handleCancel, }; } export default useConfirm;