feat(hooks): add usePendingMap hook for managing pending state

Implement a custom hook to track pending states with cache persistence. The hook provides methods to set and check pending status for given IDs, with automatic cleanup of undefined values.
This commit is contained in:
icarus 2025-10-22 04:24:38 +08:00
parent 2f3e634880
commit a0445a307a

View File

@ -0,0 +1,31 @@
import { useCache } from '@data/hooks/useCache'
import { useCallback } from 'react'
export const usePendingMap = () => {
const [pendingMap, setPendingMap] = useCache('app.pending_map')
const setPending = useCallback(
(id: string, value: boolean | undefined) => {
if (value !== undefined) {
setPendingMap({
...pendingMap,
[id]: value
})
} else {
const newMap = { ...pendingMap }
delete newMap[id]
setPendingMap(newMap)
}
},
[pendingMap, setPendingMap]
)
const isPending = useCallback(
(id: string) => {
return pendingMap[id]
},
[pendingMap]
)
return { pendingMap, setPending, isPending }
}