use base ui toast

This commit is contained in:
yyh
2026-03-25 20:38:44 +08:00
parent a7178b4d5c
commit 20dea1faa2
274 changed files with 3597 additions and 8129 deletions

View File

@ -5,10 +5,8 @@ import { useTextGeneration } from '../hooks'
const mockNotify = vi.fn()
const mockSsePost = vi.fn<(url: string, fetchOptions: { body: Record<string, unknown> }, otherOptions: IOtherOptions) => void>()
vi.mock('@/app/components/base/toast/context', () => ({
useToastContext: () => ({
notify: mockNotify,
}),
vi.mock('@/app/components/base/ui/toast', () => ({
}))
vi.mock('@/service/base', () => ({

View File

@ -1,57 +1,46 @@
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useToastContext } from '@/app/components/base/toast/context'
import { toast } from '@/app/components/base/ui/toast'
import { ssePost } from '@/service/base'
export const useTextGeneration = () => {
const { t } = useTranslation()
const { notify } = useToastContext()
const [isResponding, setIsResponding] = useState(false)
const [completion, setCompletion] = useState('')
const [messageId, setMessageId] = useState<string | null>(null)
const handleSend = async (
url: string,
data: any,
) => {
const handleSend = async (url: string, data: any) => {
if (isResponding) {
notify({ type: 'info', message: t('errorMessage.waitForResponse', { ns: 'appDebug' }) })
toast.info(t('errorMessage.waitForResponse', { ns: 'appDebug' }))
return false
}
setIsResponding(true)
setCompletion('')
setMessageId('')
let res: string[] = []
ssePost(
url,
{
body: {
response_mode: 'streaming',
...data,
},
ssePost(url, {
body: {
response_mode: 'streaming',
...data,
},
{
onData: (data: string, _isFirstMessage: boolean, { messageId }) => {
res.push(data)
setCompletion(res.join(''))
setMessageId(messageId)
},
onMessageReplace: (messageReplace) => {
res = [messageReplace.answer]
setCompletion(res.join(''))
},
onCompleted() {
setIsResponding(false)
},
onError() {
setIsResponding(false)
},
}, {
onData: (data: string, _isFirstMessage: boolean, { messageId }) => {
res.push(data)
setCompletion(res.join(''))
setMessageId(messageId)
},
)
onMessageReplace: (messageReplace) => {
res = [messageReplace.answer]
setCompletion(res.join(''))
},
onCompleted() {
setIsResponding(false)
},
onError() {
setIsResponding(false)
},
})
return true
}
return {
completion,
isResponding,