From a04b6618648e58ebd4e5f0b2d432bd86442c16da Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Thu, 6 Jul 2023 10:49:51 +0800 Subject: [PATCH] fix: kb un refresh --- .../pages/api/openapi/v1/chat/completions.ts | 4 ++ client/src/pages/kb/components/DataCard.tsx | 14 +++--- client/src/pages/kb/components/Detail.tsx | 44 +++++++++++-------- client/src/pages/kb/index.tsx | 8 ++-- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/client/src/pages/api/openapi/v1/chat/completions.ts b/client/src/pages/api/openapi/v1/chat/completions.ts index 17491f5a3..b0f7a4f85 100644 --- a/client/src/pages/api/openapi/v1/chat/completions.ts +++ b/client/src/pages/api/openapi/v1/chat/completions.ts @@ -167,6 +167,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex return res.json({ id: chatId || '', model: model.chat.chatModel, + object: 'chat.completion', + created: 1688608930, usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }, choices: [ { message: { role: 'assistant', content: response }, finish_reason: 'stop', index: 0 } @@ -296,6 +298,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex : {}), newChatId, id: chatId || '', + object: 'chat.completion', + created: 1688608930, model: model.chat.chatModel, usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: tokens }, choices: [ diff --git a/client/src/pages/kb/components/DataCard.tsx b/client/src/pages/kb/components/DataCard.tsx index 1f6a2e57b..e44ba8e06 100644 --- a/client/src/pages/kb/components/DataCard.tsx +++ b/client/src/pages/kb/components/DataCard.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useState, useRef } from 'react'; +import React, { useCallback, useState, useRef, useEffect } from 'react'; import { Box, Card, @@ -50,11 +50,11 @@ const DataCard = ({ kbId }: { kbId: string }) => { } = usePagination({ api: getKbDataList, pageSize: 24, + defaultRequest: false, params: { kbId, searchText - }, - defaultRequest: false + } }); const [editInputData, setEditInputData] = useState(); @@ -133,11 +133,11 @@ const DataCard = ({ kbId }: { kbId: string }) => { refetchInterval: 5000, enabled: qaListLen > 0 || vectorListLen > 0 }); - useQuery(['getKbData', kbId], () => { + + useEffect(() => { setSearchText(''); getData(1); - return null; - }); + }, [kbId]); return ( @@ -314,4 +314,4 @@ const DataCard = ({ kbId }: { kbId: string }) => { ); }; -export default DataCard; +export default React.memo(DataCard); diff --git a/client/src/pages/kb/components/Detail.tsx b/client/src/pages/kb/components/Detail.tsx index 1ea22f1a1..5012a86ef 100644 --- a/client/src/pages/kb/components/Detail.tsx +++ b/client/src/pages/kb/components/Detail.tsx @@ -8,7 +8,7 @@ import { useUserStore } from '@/store/user'; import { KbItemType } from '@/types/plugin'; import { useScreen } from '@/hooks/useScreen'; import { getErrText } from '@/utils/tools'; -import Info, { type ComponentRef } from './Info'; +import { type ComponentRef } from './Info'; import Tabs from '@/components/Tabs'; import dynamic from 'next/dynamic'; import DataCard from './DataCard'; @@ -16,6 +16,9 @@ import DataCard from './DataCard'; const Test = dynamic(() => import('./Test'), { ssr: false }); +const Info = dynamic(() => import('./Info'), { + ssr: false +}); enum TabEnum { data = 'data', @@ -28,7 +31,7 @@ const Detail = ({ kbId }: { kbId: string }) => { const router = useRouter(); const { isPc } = useScreen(); const BasicInfo = useRef(null); - const { setLastKbId, kbDetail, getKbDetail, loadKbList, myKbList } = useUserStore(); + const { setLastKbId, kbDetail, getKbDetail, loadKbList } = useUserStore(); const [currentTab, setCurrentTab] = useState(TabEnum.data); const form = useForm({ @@ -36,25 +39,30 @@ const Detail = ({ kbId }: { kbId: string }) => { }); const { reset } = form; - useQuery([kbId], () => getKbDetail(kbId), { - onSuccess(res) { - kbId && setLastKbId(kbId); - if (res) { - setCurrentTab(TabEnum.data); + useQuery( + [kbId], + () => { + setCurrentTab(TabEnum.data); + return getKbDetail(kbId); + }, + { + onSuccess(res) { + if (!res) return; + kbId && setLastKbId(kbId); reset(res); BasicInfo.current?.initInput?.(res.tags); + }, + onError(err: any) { + loadKbList(true); + setLastKbId(''); + router.replace(`/kb`); + toast({ + title: getErrText(err, '获取知识库异常'), + status: 'error' + }); } - }, - onError(err: any) { - loadKbList(true); - setLastKbId(''); - router.replace(`/kb`); - toast({ - title: getErrText(err, '获取知识库异常'), - status: 'error' - }); } - }); + ); return ( { ); }; -export default Detail; +export default React.memo(Detail); diff --git a/client/src/pages/kb/index.tsx b/client/src/pages/kb/index.tsx index d42f16763..9924c0c7f 100644 --- a/client/src/pages/kb/index.tsx +++ b/client/src/pages/kb/index.tsx @@ -28,9 +28,11 @@ const Kb = () => { )} - - {kbId && } - + {!!kbId && ( + + + + )} ); };