mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-10 07:19:02 +08:00
refactor(hooks): improve error handling in useSessions hook
- Remove redundant agentId checks as they're handled by the API client - Add consistent error formatting using formatErrorMessageWithPrefix - Update error messages for all session operations
This commit is contained in:
parent
f5acddbfeb
commit
77c64cf868
@ -1,4 +1,5 @@
|
|||||||
import { CreateSessionForm, UpdateSessionForm } from '@renderer/types'
|
import { CreateSessionForm, UpdateSessionForm } from '@renderer/types'
|
||||||
|
import { formatErrorMessageWithPrefix } from '@renderer/utils/error'
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
@ -11,9 +12,6 @@ export const useSessions = (agentId: string) => {
|
|||||||
const key = client.getSessionPaths(agentId).base
|
const key = client.getSessionPaths(agentId).base
|
||||||
|
|
||||||
const fetcher = async () => {
|
const fetcher = async () => {
|
||||||
if (!agentId) {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
const data = await client.listSessions(agentId)
|
const data = await client.listSessions(agentId)
|
||||||
return data.data
|
return data.data
|
||||||
}
|
}
|
||||||
@ -21,12 +19,11 @@ export const useSessions = (agentId: string) => {
|
|||||||
|
|
||||||
const createSession = useCallback(
|
const createSession = useCallback(
|
||||||
async (form: CreateSessionForm) => {
|
async (form: CreateSessionForm) => {
|
||||||
if (!agentId) return
|
|
||||||
try {
|
try {
|
||||||
const result = await client.createSession(agentId, form)
|
const result = await client.createSession(agentId, form)
|
||||||
mutate((prev) => [...(prev ?? []), result])
|
mutate((prev) => [...(prev ?? []), result])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(t('agent.session.create.error.failed'))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.create.error.failed')))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[agentId, client, mutate, t]
|
[agentId, client, mutate, t]
|
||||||
@ -35,12 +32,14 @@ export const useSessions = (agentId: string) => {
|
|||||||
// TODO: including messages field
|
// TODO: including messages field
|
||||||
const getSession = useCallback(
|
const getSession = useCallback(
|
||||||
async (id: string) => {
|
async (id: string) => {
|
||||||
if (!agentId) return
|
try {
|
||||||
const result = await client.getSession(agentId, id)
|
const result = await client.getSession(agentId, id)
|
||||||
mutate((prev) => prev?.map((session) => (session.id === result.id ? result : session)))
|
mutate((prev) => prev?.map((session) => (session.id === result.id ? result : session)))
|
||||||
return result
|
} catch (error) {
|
||||||
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.get.error.failed')))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[agentId, client, mutate]
|
[agentId, client, mutate, t]
|
||||||
)
|
)
|
||||||
|
|
||||||
const deleteSession = useCallback(
|
const deleteSession = useCallback(
|
||||||
@ -50,7 +49,7 @@ export const useSessions = (agentId: string) => {
|
|||||||
await client.deleteSession(agentId, id)
|
await client.deleteSession(agentId, id)
|
||||||
mutate((prev) => prev?.filter((session) => session.id !== id))
|
mutate((prev) => prev?.filter((session) => session.id !== id))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(t('agent.session.delete.error.failed'))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.delete.error.failed')))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[agentId, client, mutate, t]
|
[agentId, client, mutate, t]
|
||||||
@ -63,7 +62,7 @@ export const useSessions = (agentId: string) => {
|
|||||||
const result = await client.updateSession(agentId, form)
|
const result = await client.updateSession(agentId, form)
|
||||||
mutate((prev) => prev?.map((session) => (session.id === form.id ? result : session)))
|
mutate((prev) => prev?.map((session) => (session.id === form.id ? result : session)))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(t('agent.session.update.error.failed'))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.update.error.failed')))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[agentId, client, mutate, t]
|
[agentId, client, mutate, t]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user