mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 12:51:26 +08:00
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:
parent
2f3e634880
commit
a0445a307a
31
src/renderer/src/hooks/usePendingMap.ts
Normal file
31
src/renderer/src/hooks/usePendingMap.ts
Normal 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 }
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user