From 5a6413f35660cecbae50230b4c4dcdb1aebd27ec Mon Sep 17 00:00:00 2001 From: icarus Date: Sun, 14 Sep 2025 00:12:50 +0800 Subject: [PATCH] feat(hooks): add useAgents hook for managing agent state Implement a custom hook to handle agent CRUD operations in the store --- src/renderer/src/hooks/useAgents.ts | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/renderer/src/hooks/useAgents.ts diff --git a/src/renderer/src/hooks/useAgents.ts b/src/renderer/src/hooks/useAgents.ts new file mode 100644 index 0000000000..69aeb94d08 --- /dev/null +++ b/src/renderer/src/hooks/useAgents.ts @@ -0,0 +1,63 @@ +import { useAppDispatch } from '@renderer/store' +import { addAgent, removeAgent, setAgents, updateAgent } from '@renderer/store/agents' +import { AgentEntity } from '@renderer/types' +import { uuid } from '@renderer/utils' +import { useCallback } from 'react' + +export const useAgents = () => { + const dispatch = useAppDispatch() + /** + * Adds a new agent to the store + * @param config - The configuration object for the new agent (without id) + */ + const addAgent_ = useCallback( + (config: Omit) => { + const entity = { + ...config, + id: uuid() + } as const + dispatch(addAgent(entity)) + }, + [dispatch] + ) + + /** + * Removes an agent from the store + * @param id - The ID of the agent to remove + */ + const removeAgent_ = useCallback( + (id: AgentEntity['id']) => { + dispatch(removeAgent({ id })) + }, + [dispatch] + ) + + /** + * Updates an existing agent in the store + * @param update - Partial agent data with required ID field + */ + const updateAgent_ = useCallback( + (update: Partial & { id: AgentEntity['id'] }) => { + dispatch(updateAgent(update)) + }, + [dispatch] + ) + + /** + * Sets the entire agents array in the store + * @param agents - Array of agent entities to set + */ + const setAgents_ = useCallback( + (agents: AgentEntity[]) => { + dispatch(setAgents(agents)) + }, + [dispatch] + ) + + return { + addAgent: addAgent_, + removeAgent: removeAgent_, + updateAgent: updateAgent_, + setAgents: setAgents_ + } +}