From 136d343c1879a42685587c8f05a433f78c81c648 Mon Sep 17 00:00:00 2001 From: icarus Date: Thu, 18 Sep 2025 18:05:55 +0800 Subject: [PATCH] refactor(hooks): replace redux with swr in useAgents hook Simplify agents management by using SWR for data fetching instead of redux store operations --- src/renderer/src/hooks/agents/useAgents.ts | 67 +++------------------- 1 file changed, 8 insertions(+), 59 deletions(-) diff --git a/src/renderer/src/hooks/agents/useAgents.ts b/src/renderer/src/hooks/agents/useAgents.ts index 92673b207e..386c03eeb3 100644 --- a/src/renderer/src/hooks/agents/useAgents.ts +++ b/src/renderer/src/hooks/agents/useAgents.ts @@ -1,66 +1,15 @@ -import { useAppDispatch, useAppSelector } from '@renderer/store' -import { addAgent, removeAgent, setAgents, updateAgent } from '@renderer/store/agents' -import { AgentEntity } from '@renderer/types' -// import { useQuery, useQueryClient } from '@tanstack/react-query' -import { useCallback } from 'react' +import useSWR from 'swr' + +import { useAgentClient } from './useAgentClient' export const useAgents = () => { - // const qc = useQueryClient() - // const { data, isLoading, error } = useQuery({ - // queryKey: ['agents'], - // queryFn: async () => {} - // }) - const agents = useAppSelector((state) => state.agents.agentsNew) - const dispatch = useAppDispatch() - /** - * Adds a new agent to the store - * @param agent - The complete agent entity to add - */ - const addAgent_ = useCallback( - (agent: AgentEntity) => { - dispatch(addAgent(agent)) - }, - [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] - ) + const client = useAgentClient() + const key = client.agentPaths.base + const { data: agents, error, isLoading } = useSWR(key, () => client.listAgents()) return { agents, - addAgent: addAgent_, - removeAgent: removeAgent_, - updateAgent: updateAgent_, - setAgents: setAgents_ + error, + isLoading } }