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:
@@ -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 }
|
||||
}
|
||||
Reference in New Issue
Block a user