feat(hooks): add usePending hook to manage pending state

Add a new custom hook to track pending states with a map in the runtime store. The hook provides a way to set and clear pending flags by id.
This commit is contained in:
icarus 2025-10-13 21:49:37 +08:00
parent c85fad90b5
commit c9c859731f
2 changed files with 30 additions and 1 deletions

View File

@ -0,0 +1,17 @@
import { useAppDispatch } from '@renderer/store'
import { setPendingAction } from '@renderer/store/runtime'
import { useCallback } from 'react'
import { useRuntime } from './useRuntime'
export const usePending = () => {
const { pendingMap } = useRuntime()
const dispatch = useAppDispatch()
const setPending = useCallback(
(id: string, value: boolean | undefined) => {
dispatch(setPendingAction({ id, value }))
},
[dispatch]
)
return { pendingMap, setPending }
}

View File

@ -56,6 +56,8 @@ export interface RuntimeState {
chat: ChatState
websearch: WebSearchState
iknow: Record<string, boolean>
/** To indicate something is pending. */
pendingMap: Record<string, boolean | undefined>
}
export interface ExportState {
@ -98,7 +100,8 @@ const initialState: RuntimeState = {
websearch: {
activeSearches: {}
},
iknow: {}
iknow: {},
pendingMap: {}
}
const runtimeSlice = createSlice({
@ -191,6 +194,14 @@ const runtimeSlice = createSlice({
setSessionWaitingAction: (state, action: PayloadAction<{ id: string; value: boolean }>) => {
const { id, value } = action.payload
state.chat.sessionWaiting[id] = value
},
setPendingAction: (state, action: PayloadAction<{ id: string; value: boolean | undefined }>) => {
const { id, value } = action.payload
if (value) {
state.pendingMap[id] = value
} else {
delete state.pendingMap[id]
}
}
}
})
@ -210,6 +221,7 @@ export const {
setUpdateState,
setExportState,
addIknowAction,
setPendingAction,
// Chat related actions
toggleMultiSelectMode,
setSelectedMessageIds,