import React, { useMemo, useRef, useState } from 'react'; import { Flex, Box, TableContainer, Table, Thead, Tr, Th, Td, Tbody, useTheme, useDisclosure, ModalBody } from '@chakra-ui/react'; import MyIcon from '@/components/Icon'; import { useTranslation } from 'next-i18next'; import { usePagination } from '@/hooks/usePagination'; import { getAppChatLogs } from '@/api/app'; import dayjs from 'dayjs'; import { ChatSourceMap, HUMAN_ICON } from '@/constants/chat'; import { AppLogsListItemType } from '@/types/app'; import { useGlobalStore } from '@/store/global'; import ChatBox, { type ComponentRef } from '@/components/ChatBox'; import { useQuery } from '@tanstack/react-query'; import { getInitChatSiteInfo } from '@/api/chat'; import Tag from '@/components/Tag'; import MyModal from '@/components/MyModal'; import DateRangePicker, { type DateRangeType } from '@/components/DateRangePicker'; import { addDays } from 'date-fns'; const Logs = ({ appId }: { appId: string }) => { const { t } = useTranslation(); const { isPc } = useGlobalStore(); const [dateRange, setDateRange] = useState({ from: addDays(new Date(), -7), to: new Date() }); const { isOpen: isOpenMarkDesc, onOpen: onOpenMarkDesc, onClose: onCloseMarkDesc } = useDisclosure(); const { data: logs, isLoading, Pagination, getData, pageNum } = usePagination({ api: getAppChatLogs, pageSize: 20, params: { appId, dateStart: dateRange.from || new Date(), dateEnd: addDays(dateRange.to || new Date(), 1) } }); const [detailLogsId, setDetailLogsId] = useState(); return ( {isPc && ( <> {t('app.Chat logs')} {t('app.Chat Logs Tips')},{' '} {t('chat.Read Mark Description')} )} {/* table */} {logs.map((item) => ( setDetailLogsId(item.id)} > ))}
{t('app.Logs Source')} {t('app.Logs Time')} {t('app.Logs Title')} {t('app.Logs Message Total')} {t('app.Feedback Count')} {t('app.Mark Count')}
{t(ChatSourceMap[item.source]?.name || 'UnKnow')} {dayjs(item.time).format('YYYY/MM/DD HH:mm')} {item.title} {item.messageCount} {!!item?.feedbackCount ? ( {item.feedbackCount} ) : ( <>- )} {item.markCount}
{logs.length === 0 && !isLoading && ( {t('app.Logs Empty')} )} getData(1)} /> {!!detailLogsId && ( setDetailLogsId(undefined)} /> )} {t('chat.Mark Description')}
); }; export default Logs; function DetailLogsModal({ appId, chatId, onClose }: { appId: string; chatId: string; onClose: () => void; }) { const ChatBoxRef = useRef(null); const { isPc } = useGlobalStore(); const theme = useTheme(); const { data: chat } = useQuery( ['getChatDetail', chatId], () => getInitChatSiteInfo({ appId, chatId }), { onSuccess(res) { const history = res.history.map((item) => ({ ...item, status: 'finish' as any })); ChatBoxRef.current?.resetHistory(history); ChatBoxRef.current?.resetVariables(res.variables); if (res.history.length > 0) { setTimeout(() => { ChatBoxRef.current?.scrollToBottom('auto'); }, 500); } } } ); const history = useMemo(() => (chat?.history ? chat.history : []), [chat]); const title = useMemo(() => { return history[history.length - 2]?.value?.slice(0, 8); }, [history]); return ( <> {isPc ? ( <> {title} {`${history.length}条记录`} {!!chat?.app?.chatModels && ( {chat.app.chatModels.join(',')} )} ) : ( <> {title} )} {}} /> ); }