fix: 全面优化安全、性能和代码质量
P0: 环境变量校验/事务保护/RBAC权限 P1: 花名册分页/密码重置验证码/ErrorBoundary/N+1查询优化 P2: 导出限制/自定义错误类/body大小限制/代码去重 P3: 自定义确认弹窗替换window.confirm
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { useState, useCallback, createContext, useContext, ReactNode } from 'react'
|
||||
import ConfirmDialog from '../components/ui/ConfirmDialog'
|
||||
|
||||
interface ConfirmOptions {
|
||||
title?: string
|
||||
message: string
|
||||
confirmLabel?: string
|
||||
variant?: 'danger' | 'primary'
|
||||
}
|
||||
|
||||
interface ConfirmContextValue {
|
||||
confirm: (options: ConfirmOptions) => Promise<boolean>
|
||||
}
|
||||
|
||||
const ConfirmContext = createContext<ConfirmContextValue | null>(null)
|
||||
|
||||
export function useConfirm(): (options: ConfirmOptions) => Promise<boolean> {
|
||||
const ctx = useContext(ConfirmContext)
|
||||
if (!ctx) {
|
||||
throw new Error('useConfirm must be used within ConfirmProvider')
|
||||
}
|
||||
return ctx.confirm
|
||||
}
|
||||
|
||||
export function useConfirmDialog() {
|
||||
return useConfirm()
|
||||
}
|
||||
|
||||
export function ConfirmProvider({ children }: { children: ReactNode }) {
|
||||
const [state, setState] = useState<{
|
||||
open: boolean
|
||||
options: ConfirmOptions
|
||||
resolve?: (value: boolean) => void
|
||||
}>({ open: false, options: { message: '' } })
|
||||
|
||||
const confirm = useCallback((options: ConfirmOptions) => {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
setState({ open: true, options, resolve })
|
||||
})
|
||||
}, [])
|
||||
|
||||
const handleConfirm = useCallback(() => {
|
||||
state.resolve?.(true)
|
||||
setState((s) => ({ ...s, open: false, resolve: undefined }))
|
||||
}, [state])
|
||||
|
||||
const handleCancel = useCallback(() => {
|
||||
state.resolve?.(false)
|
||||
setState((s) => ({ ...s, open: false, resolve: undefined }))
|
||||
}, [state])
|
||||
|
||||
return (
|
||||
<ConfirmContext.Provider value={{ confirm }}>
|
||||
{children}
|
||||
<ConfirmDialog
|
||||
open={state.open}
|
||||
title={state.options.title || '确认操作'}
|
||||
message={state.options.message}
|
||||
confirmLabel={state.options.confirmLabel || '确认'}
|
||||
variant={state.options.variant || 'danger'}
|
||||
onConfirm={handleConfirm}
|
||||
onCancel={handleCancel}
|
||||
/>
|
||||
</ConfirmContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user