mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-28 13:31:32 +08:00
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:
parent
c85fad90b5
commit
c9c859731f
17
src/renderer/src/hooks/usePending.ts
Normal file
17
src/renderer/src/hooks/usePending.ts
Normal 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 }
|
||||
}
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user