mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-02 02:09:03 +08:00
refactor(useAgents): simplify agent data structure and mutations
Remove unnecessary nesting of agents array in SWR response and simplify mutation logic to work directly with the array
This commit is contained in:
parent
da61500e34
commit
934cc0dd33
@ -10,13 +10,17 @@ export const useAgents = () => {
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const client = useAgentClient()
|
const client = useAgentClient()
|
||||||
const key = client.agentPaths.base
|
const key = client.agentPaths.base
|
||||||
const { data, error, isLoading, mutate } = useSWR(key, () => client.listAgents())
|
const fetcher = useCallback(async () => {
|
||||||
|
const result = await client.listAgents()
|
||||||
|
return result.agents
|
||||||
|
}, [client])
|
||||||
|
const { data, error, isLoading, mutate } = useSWR(key, fetcher)
|
||||||
|
|
||||||
const addAgent = useCallback(
|
const addAgent = useCallback(
|
||||||
async (form: AddAgentForm) => {
|
async (form: AddAgentForm) => {
|
||||||
try {
|
try {
|
||||||
const result = await client.createAgent(form)
|
const result = await client.createAgent(form)
|
||||||
mutate((prev) => ({ agents: [...(prev?.agents ?? []), result], total: prev ? prev.total + 1 : 1 }))
|
mutate((prev) => [...(prev ?? []), result])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.add.error.failed')))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.add.error.failed')))
|
||||||
}
|
}
|
||||||
@ -29,10 +33,7 @@ export const useAgents = () => {
|
|||||||
try {
|
try {
|
||||||
// may change to optimistic update
|
// may change to optimistic update
|
||||||
const result = await client.updateAgent(form)
|
const result = await client.updateAgent(form)
|
||||||
mutate((prev) => ({
|
mutate((prev) => prev?.map((a) => (a.id === form.id ? result : a)) ?? [])
|
||||||
agents: prev?.agents.map((a) => (a.id === form.id ? result : a)) ?? [],
|
|
||||||
total: prev?.total ?? 0
|
|
||||||
}))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.update.error.failed')))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.update.error.failed')))
|
||||||
}
|
}
|
||||||
@ -44,10 +45,7 @@ export const useAgents = () => {
|
|||||||
async (id: string) => {
|
async (id: string) => {
|
||||||
try {
|
try {
|
||||||
await client.deleteAgent(id)
|
await client.deleteAgent(id)
|
||||||
mutate((prev) => ({
|
mutate((prev) => prev?.filter((a) => a.id !== id) ?? [])
|
||||||
agents: prev?.agents.filter((a) => a.id !== id) ?? [],
|
|
||||||
total: prev ? prev.total - 1 : 0
|
|
||||||
}))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.delete.error.failed')))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.delete.error.failed')))
|
||||||
}
|
}
|
||||||
@ -57,13 +55,13 @@ export const useAgents = () => {
|
|||||||
|
|
||||||
const getAgent = useCallback(
|
const getAgent = useCallback(
|
||||||
(id: string) => {
|
(id: string) => {
|
||||||
return data?.agents.find((agent) => agent.id === id)
|
return data?.find((agent) => agent.id === id)
|
||||||
},
|
},
|
||||||
[data?.agents]
|
[data]
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
agents: data?.agents ?? [],
|
agents: data ?? [],
|
||||||
error,
|
error,
|
||||||
isLoading,
|
isLoading,
|
||||||
addAgent,
|
addAgent,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user