Some checks failed
CI Check / check (push) Has been cancelled
- 替换所有浏览器原生弹窗(confirm/alert/prompt) - 创建 ConfirmDialog、AlertDialog、PromptDialog 组件 - 创建 DialogProvider 和 useDialog Hook - 替换所有 admin 组件、首页、分享页面中的原生弹窗 - 素材预览面板统一 - 创建 MaterialPreviewModal 组件 - 使用统一弹窗组件,统一退出逻辑 - 首页UI统一到三层架构 - 迁移主要按钮和输入框样式 - 迁移筛选弹窗中的所有按钮样式 - 使用 useThemeStyles() Hook - 更新文档 - 更新优化记录、优化计划、后续修改、更新日志
119 lines
3.2 KiB
TypeScript
119 lines
3.2 KiB
TypeScript
'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<boolean>
|
|
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<string | null>
|
|
}
|
|
|
|
const DialogContext = createContext<DialogContextType | null>(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<boolean> => {
|
|
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<string | null> => {
|
|
return promptHook.prompt(options)
|
|
}
|
|
|
|
return (
|
|
<DialogContext.Provider value={{ confirm, alert, prompt }}>
|
|
{children}
|
|
<ConfirmDialog
|
|
isOpen={confirmHook.isOpen}
|
|
onClose={confirmHook.handleCancel}
|
|
onConfirm={confirmHook.handleConfirm}
|
|
title={confirmHook.options.title || '确认'}
|
|
message={confirmHook.options.message}
|
|
confirmText={confirmHook.options.confirmText}
|
|
cancelText={confirmHook.options.cancelText}
|
|
variant={confirmHook.options.variant}
|
|
/>
|
|
<AlertDialog
|
|
isOpen={alertHook.isOpen}
|
|
onClose={alertHook.close}
|
|
title={alertHook.options.title || '提示'}
|
|
message={alertHook.options.message}
|
|
buttonText={alertHook.options.buttonText}
|
|
variant={alertHook.options.variant}
|
|
/>
|
|
<PromptDialog
|
|
isOpen={promptHook.isOpen}
|
|
onClose={promptHook.handleCancel}
|
|
onConfirm={promptHook.handleConfirm}
|
|
title={promptHook.options.title || '输入'}
|
|
message={promptHook.options.message}
|
|
defaultValue={promptHook.options.defaultValue}
|
|
placeholder={promptHook.options.placeholder}
|
|
confirmText={promptHook.options.confirmText}
|
|
cancelText={promptHook.options.cancelText}
|
|
/>
|
|
</DialogContext.Provider>
|
|
)
|
|
}
|
|
|