perf: quote modal

This commit is contained in:
archer
2023-07-23 22:24:14 +08:00
parent 1ffe1be562
commit ea35ad2144
10 changed files with 188 additions and 209 deletions

View File

@@ -13,21 +13,17 @@ import MyIcon from '@/components/Icon';
import InputDataModal from '@/pages/kb/detail/components/InputDataModal';
import { getKbDataItemById } from '@/api/plugins/kb';
import { useLoading } from '@/hooks/useLoading';
import { useQuery } from '@tanstack/react-query';
import { getHistoryQuote, updateHistoryQuote } from '@/api/chat';
import { useToast } from '@/hooks/useToast';
import { getErrText } from '@/utils/tools';
import { QuoteItemType } from '@/types/chat';
const QuoteModal = ({
chatId,
contentId,
onUpdateQuote,
rawSearch = [],
onClose
}: {
chatId?: string;
contentId?: string;
rawSearch?: QuoteItemType[];
onUpdateQuote: (quoteId: string, sourceText: string) => Promise<void>;
rawSearch: QuoteItemType[];
onClose: () => void;
}) => {
const theme = useTheme();
@@ -40,47 +36,6 @@ const QuoteModal = ({
q: string;
}>();
const {
data: quote = [],
refetch,
isLoading
} = useQuery(['getHistoryQuote'], () => {
if (chatId && contentId) {
return getHistoryQuote({ chatId, contentId });
}
if (rawSearch.length > 0) {
return rawSearch;
}
return [];
});
/**
* update kbData, update mongo status and reload quotes
*/
const updateQuoteStatus = useCallback(
async (quoteId: string, sourceText: string) => {
if (!chatId || !contentId) return;
setIsLoading(true);
try {
await updateHistoryQuote({
contentId,
chatId,
quoteId,
sourceText
});
// reload quote
refetch();
} catch (err) {
toast({
status: 'warning',
title: getErrText(err)
});
}
setIsLoading(false);
},
[contentId, chatId, refetch, setIsLoading, toast]
);
/**
* click edit, get new kbDataItem
*/
@@ -91,7 +46,7 @@ const QuoteModal = ({
const data = (await getKbDataItemById(item.id)) as QuoteItemType;
if (!data) {
updateQuoteStatus(item.id, '已删除');
onUpdateQuote(item.id, '已删除');
throw new Error('该数据已被删除');
}
@@ -109,7 +64,7 @@ const QuoteModal = ({
}
setIsLoading(false);
},
[setIsLoading, toast, updateQuoteStatus]
[setIsLoading, toast, onUpdateQuote]
);
return (
@@ -123,14 +78,14 @@ const QuoteModal = ({
overflow={'overlay'}
>
<ModalHeader>
({quote.length})
({rawSearch.length})
<Box fontSize={'sm'} fontWeight={'normal'}>
注意: 修改知识库内容成功后
注意: 修改知识库内容成功后
</Box>
</ModalHeader>
<ModalCloseButton />
<ModalBody pt={0} whiteSpace={'pre-wrap'} textAlign={'justify'} fontSize={'sm'}>
{quote.map((item) => (
{rawSearch.map((item) => (
<Box
key={item.id}
flex={'1 0 0'}
@@ -172,14 +127,14 @@ const QuoteModal = ({
</Box>
))}
</ModalBody>
<Loading loading={isLoading} fixed={false} />
<Loading fixed={false} />
</ModalContent>
</Modal>
{editDataItem && (
<InputDataModal
onClose={() => setEditDataItem(undefined)}
onSuccess={() => updateQuoteStatus(editDataItem.dataId, '手动修改')}
onDelete={() => updateQuoteStatus(editDataItem.dataId, '已删除')}
onSuccess={() => onUpdateQuote(editDataItem.dataId, '手动修改')}
onDelete={() => onUpdateQuote(editDataItem.dataId, '已删除')}
kbId={editDataItem.kbId}
defaultValues={editDataItem}
/>