diff --git a/src/renderer/src/hooks/usePending.ts b/src/renderer/src/hooks/usePending.ts new file mode 100644 index 0000000000..5a97e49027 --- /dev/null +++ b/src/renderer/src/hooks/usePending.ts @@ -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 } +} diff --git a/src/renderer/src/store/runtime.ts b/src/renderer/src/store/runtime.ts index fe556b9a9e..26a8e6c879 100644 --- a/src/renderer/src/store/runtime.ts +++ b/src/renderer/src/store/runtime.ts @@ -56,6 +56,8 @@ export interface RuntimeState { chat: ChatState websearch: WebSearchState iknow: Record + /** To indicate something is pending. */ + pendingMap: Record } 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,