flnxi-Wu/app/admin/_hooks/useModalState.ts
2025-11-22 17:56:24 +08:00

41 lines
782 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* useModalState Hook
*
* 模态框状态管理Hook
* 统一管理模态框的打开/关闭状态
* 支持泛型,可以存储模态框相关的数据
*
* @created 2025-01-XX(从 page.tsx 提取)
* @hook
*/
'use client'
import { useCallback, useState } from 'react'
export function useModalState<T = void>() {
const [isOpen, setIsOpen] = useState(false)
const [data, setData] = useState<T | null>(null)
const open = useCallback((payload?: T) => {
setData((payload ?? null) as T | null)
setIsOpen(true)
}, [])
const close = useCallback(() => {
setIsOpen(false)
setData(null)
}, [])
return {
isOpen,
data,
open,
close,
setData,
}
}
export type UseModalStateReturn<T> = ReturnType<typeof useModalState<T>>