From c9c859731f5dae9d4c7fcbbd6093938b7b86f6a0 Mon Sep 17 00:00:00 2001 From: icarus Date: Mon, 13 Oct 2025 21:49:37 +0800 Subject: [PATCH] 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. --- src/renderer/src/hooks/usePending.ts | 17 +++++++++++++++++ src/renderer/src/store/runtime.ts | 14 +++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/renderer/src/hooks/usePending.ts 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,