mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 06:49:02 +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
|
chat: ChatState
|
||||||
websearch: WebSearchState
|
websearch: WebSearchState
|
||||||
iknow: Record<string, boolean>
|
iknow: Record<string, boolean>
|
||||||
|
/** To indicate something is pending. */
|
||||||
|
pendingMap: Record<string, boolean | undefined>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ExportState {
|
export interface ExportState {
|
||||||
@ -98,7 +100,8 @@ const initialState: RuntimeState = {
|
|||||||
websearch: {
|
websearch: {
|
||||||
activeSearches: {}
|
activeSearches: {}
|
||||||
},
|
},
|
||||||
iknow: {}
|
iknow: {},
|
||||||
|
pendingMap: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const runtimeSlice = createSlice({
|
const runtimeSlice = createSlice({
|
||||||
@ -191,6 +194,14 @@ const runtimeSlice = createSlice({
|
|||||||
setSessionWaitingAction: (state, action: PayloadAction<{ id: string; value: boolean }>) => {
|
setSessionWaitingAction: (state, action: PayloadAction<{ id: string; value: boolean }>) => {
|
||||||
const { id, value } = action.payload
|
const { id, value } = action.payload
|
||||||
state.chat.sessionWaiting[id] = value
|
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,
|
setUpdateState,
|
||||||
setExportState,
|
setExportState,
|
||||||
addIknowAction,
|
addIknowAction,
|
||||||
|
setPendingAction,
|
||||||
// Chat related actions
|
// Chat related actions
|
||||||
toggleMultiSelectMode,
|
toggleMultiSelectMode,
|
||||||
setSelectedMessageIds,
|
setSelectedMessageIds,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user