mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-08 14:29:15 +08:00
refactor(agents): migrate agent hooks to react-query and reorganize structure
- Replace redux-based agent management with react-query hooks - Move agent-related hooks to dedicated agents directory - Add useAddAgent, useRemoveAgent, and useUpdateAgent hooks - Update imports to reflect new hook locations - Keep redux store temporarily for backward compatibility
This commit is contained in:
parent
e5b43c8176
commit
532bad8eb7
@ -18,7 +18,7 @@ import {
|
|||||||
} from '@heroui/react'
|
} from '@heroui/react'
|
||||||
import { loggerService } from '@logger'
|
import { loggerService } from '@logger'
|
||||||
import ClaudeIcon from '@renderer/assets/images/models/claude.png'
|
import ClaudeIcon from '@renderer/assets/images/models/claude.png'
|
||||||
import { useAgents } from '@renderer/hooks/useAgents'
|
import { useAgents } from '@renderer/hooks/agents/useAgents'
|
||||||
import { useTimer } from '@renderer/hooks/useTimer'
|
import { useTimer } from '@renderer/hooks/useTimer'
|
||||||
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
|
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
|
||||||
import { AgentEntity, AgentType, isAgentType } from '@renderer/types'
|
import { AgentEntity, AgentType, isAgentType } from '@renderer/types'
|
||||||
|
|||||||
16
src/renderer/src/hooks/agents/useAddAgent.ts
Normal file
16
src/renderer/src/hooks/agents/useAddAgent.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { AgentEntity } from '@renderer/types'
|
||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
|
|
||||||
|
export const useAddAgent = () => {
|
||||||
|
const qc = useQueryClient()
|
||||||
|
|
||||||
|
// TODO: use api
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async (agent: AgentEntity) => {
|
||||||
|
return agent
|
||||||
|
},
|
||||||
|
onSuccess: (added: AgentEntity) => {
|
||||||
|
qc.setQueryData<AgentEntity[]>(['agents'], (old) => (old ? [...old, added] : [added]))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -1,9 +1,15 @@
|
|||||||
import { useAppDispatch, useAppSelector } from '@renderer/store'
|
import { useAppDispatch, useAppSelector } from '@renderer/store'
|
||||||
import { addAgent, removeAgent, setAgents, updateAgent } from '@renderer/store/agents'
|
import { addAgent, removeAgent, setAgents, updateAgent } from '@renderer/store/agents'
|
||||||
import { AgentEntity } from '@renderer/types'
|
import { AgentEntity } from '@renderer/types'
|
||||||
|
// import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
export const useAgents = () => {
|
export const useAgents = () => {
|
||||||
|
// const qc = useQueryClient()
|
||||||
|
// const { data, isLoading, error } = useQuery({
|
||||||
|
// queryKey: ['agents'],
|
||||||
|
// queryFn: async () => {}
|
||||||
|
// })
|
||||||
const agents = useAppSelector((state) => state.agents.agentsNew)
|
const agents = useAppSelector((state) => state.agents.agentsNew)
|
||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
/**
|
/**
|
||||||
16
src/renderer/src/hooks/agents/useRemoveAgent.ts
Normal file
16
src/renderer/src/hooks/agents/useRemoveAgent.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { AgentEntity } from '@renderer/types'
|
||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
|
|
||||||
|
export const useRemoveAgent = () => {
|
||||||
|
const qc = useQueryClient()
|
||||||
|
|
||||||
|
// TODO: use api
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async (id: string) => {
|
||||||
|
return id
|
||||||
|
},
|
||||||
|
onSuccess: (deletedId: string) => {
|
||||||
|
qc.setQueryData<AgentEntity[]>(['agents'], (old) => old?.filter((t) => t.id !== deletedId))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
18
src/renderer/src/hooks/agents/useUpdateAgent.ts
Normal file
18
src/renderer/src/hooks/agents/useUpdateAgent.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { AgentEntity } from '@renderer/types'
|
||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
|
|
||||||
|
export const useUpdateAgent = () => {
|
||||||
|
const qc = useQueryClient()
|
||||||
|
|
||||||
|
// TODO: use api
|
||||||
|
return useMutation({
|
||||||
|
// @ts-expect-error not-implemented
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
mutationFn: async ({ id, ...payload }: Partial<AgentEntity> & { id: string }) => {},
|
||||||
|
onSuccess: (updated: AgentEntity) => {
|
||||||
|
qc.setQueryData<AgentEntity[]>(['todos'], (old) =>
|
||||||
|
old ? old.map((t) => (t.id === updated.id ? updated : t)) : []
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -3,7 +3,7 @@ import { Button, Divider } from '@heroui/react'
|
|||||||
import { DraggableList } from '@renderer/components/DraggableList'
|
import { DraggableList } from '@renderer/components/DraggableList'
|
||||||
import { AgentModal } from '@renderer/components/Popups/AgentModal'
|
import { AgentModal } from '@renderer/components/Popups/AgentModal'
|
||||||
import Scrollbar from '@renderer/components/Scrollbar'
|
import Scrollbar from '@renderer/components/Scrollbar'
|
||||||
import { useAgents } from '@renderer/hooks/useAgents'
|
import { useAgents } from '@renderer/hooks/agents/useAgents'
|
||||||
import { useAssistants } from '@renderer/hooks/useAssistant'
|
import { useAssistants } from '@renderer/hooks/useAssistant'
|
||||||
import { useAssistantPresets } from '@renderer/hooks/useAssistantPresets'
|
import { useAssistantPresets } from '@renderer/hooks/useAssistantPresets'
|
||||||
import { useAssistantsTabSortType } from '@renderer/hooks/useStore'
|
import { useAssistantsTabSortType } from '@renderer/hooks/useStore'
|
||||||
|
|||||||
@ -10,7 +10,9 @@ export interface AgentsState {
|
|||||||
* They should not be in this slice. However, since redux will be removed
|
* They should not be in this slice. However, since redux will be removed
|
||||||
* in the future, I just don't care where should they are. */
|
* in the future, I just don't care where should they are. */
|
||||||
agents: AssistantPreset[]
|
agents: AssistantPreset[]
|
||||||
/** For new autonomous agent feature. They are actual agent entities. */
|
/** For new autonomous agent feature. They are actual agent entities.
|
||||||
|
* They won't be used anymore when sqlite api is ready.
|
||||||
|
*/
|
||||||
agentsNew: AgentEntity[]
|
agentsNew: AgentEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user