import React, { useCallback, useState } from 'react'; import { Modal, ModalOverlay, ModalContent, ModalBody, ModalCloseButton, ModalHeader, Box, useTheme } from '@chakra-ui/react'; 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'; const QuoteModal = ({ historyId, contentId, onClose }: { historyId: string; contentId: string; onClose: () => void; }) => { const theme = useTheme(); const { toast } = useToast(); const { setIsLoading, Loading } = useLoading(); const [editDataItem, setEditDataItem] = useState<{ dataId: string; a: string; q: string; }>(); const { data: quote = [], refetch, isLoading } = useQuery(['getHistoryQuote'], () => getHistoryQuote({ historyId, contentId })); /** * update kbData, update mongo status and reload quotes */ const updateQuoteStatus = useCallback( async (quoteId: string, sourceText: string) => { setIsLoading(true); try { await updateHistoryQuote({ contentId, historyId, quoteId, sourceText }); // reload quote refetch(); } catch (err) { toast({ status: 'warning', title: getErrText(err) }); } setIsLoading(false); }, [contentId, historyId, refetch, setIsLoading, toast] ); /** * click edit, get new kbDataItem */ const onclickEdit = useCallback( async (item: QuoteItemType) => { try { setIsLoading(true); const data = (await getKbDataItemById(item.id)) as QuoteItemType; if (!data) { updateQuoteStatus(item.id, '已删除'); throw new Error('该数据已被删除'); } setEditDataItem({ dataId: data.id, q: data.q, a: data.a }); } catch (err) { toast({ status: 'warning', title: getErrText(err) }); } setIsLoading(false); }, [setIsLoading, toast, updateQuoteStatus] ); return ( <> 知识库引用({quote.length}条) 注意: 修改知识库内容成功后,此处不会显示。点击编辑后,才是显示最新的内容。 {quote.map((item) => ( {item.source && ({item.source})} {item.q} {item.a} onclickEdit(item)} /> ))} {editDataItem && ( setEditDataItem(undefined)} onSuccess={() => updateQuoteStatus(editDataItem.dataId, '手动修改')} onDelete={() => updateQuoteStatus(editDataItem.dataId, '已删除')} kbId="" defaultValues={editDataItem} /> )} ); }; export default QuoteModal;