fix(agents): correct agent update and retrieval logic

Fix mutation logic to use result.id instead of form.id for consistency
Make getAgent async and update cache with fetched agent data
This commit is contained in:
icarus 2025-09-18 22:51:02 +08:00
parent 02d2838424
commit d0b64dabc2

View File

@ -33,7 +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) => prev?.map((a) => (a.id === form.id ? result : a)) ?? []) mutate((prev) => prev?.map((a) => (a.id === result.id ? result : a)) ?? [])
} catch (error) { } catch (error) {
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.update.error.failed'))) window.toast.error(formatErrorMessageWithPrefix(error, t('agent.update.error.failed')))
} }
@ -54,10 +54,11 @@ export const useAgents = () => {
) )
const getAgent = useCallback( const getAgent = useCallback(
(id: string) => { async (id: string) => {
return data?.find((agent) => agent.id === id) const result = await client.getAgent(id)
mutate((prev) => prev?.map((a) => (a.id === result.id ? result : a)) ?? [])
}, },
[data] [client, mutate]
) )
return { return {