import React, { useMemo } from 'react'; import { Box, Button, Flex, useTheme, IconButton } from '@chakra-ui/react'; import { useSystem } from '@fastgpt/web/hooks/useSystem'; import { useEditTitle } from '@/web/common/hooks/useEditTitle'; import { useRouter } from 'next/router'; import Avatar from '@fastgpt/web/components/common/Avatar'; import MyTooltip from '@fastgpt/web/components/common/MyTooltip'; import MyIcon from '@fastgpt/web/components/common/Icon'; import { useTranslation } from 'next-i18next'; import { useConfirm } from '@fastgpt/web/hooks/useConfirm'; import { useUserStore } from '@/web/support/user/useUserStore'; import MyMenu from '@fastgpt/web/components/common/MyMenu'; import { useContextSelector } from 'use-context-selector'; import { ChatContext } from '@/web/core/chat/context/chatContext'; import MyBox from '@fastgpt/web/components/common/MyBox'; import { formatTimeToChatTime } from '@fastgpt/global/common/string/time'; import { ChatItemContext } from '@/web/core/chat/context/chatItemContext'; import { useChatStore } from '@/web/core/chat/context/useChatStore'; type HistoryItemType = { id: string; title: string; customTitle?: string; top?: boolean; updateTime: Date; }; const ChatHistorySlider = ({ confirmClearText }: { confirmClearText: string }) => { const theme = useTheme(); const router = useRouter(); const { t } = useTranslation(); const { isPc } = useSystem(); const { userInfo } = useUserStore(); const { appId, chatId: activeChatId } = useChatStore(); const onChangeChatId = useContextSelector(ChatContext, (v) => v.onChangeChatId); const isLoading = useContextSelector(ChatContext, (v) => v.isLoading); const ScrollData = useContextSelector(ChatContext, (v) => v.ScrollData); const histories = useContextSelector(ChatContext, (v) => v.histories); const onDelHistory = useContextSelector(ChatContext, (v) => v.onDelHistory); const onClearHistory = useContextSelector(ChatContext, (v) => v.onClearHistories); const onUpdateHistory = useContextSelector(ChatContext, (v) => v.onUpdateHistory); const appName = useContextSelector(ChatItemContext, (v) => v.chatBoxData?.app.name); const appAvatar = useContextSelector(ChatItemContext, (v) => v.chatBoxData?.app.avatar); const showRouteToAppDetail = useContextSelector(ChatItemContext, (v) => v.showRouteToAppDetail); const setQuoteData = useContextSelector(ChatItemContext, (v) => v.setQuoteData); const concatHistory = useMemo(() => { const formatHistories: HistoryItemType[] = histories.map((item) => { return { id: item.chatId, title: item.title, customTitle: item.customTitle, top: item.top, updateTime: item.updateTime }; }); const newChat: HistoryItemType = { id: activeChatId, title: t('common:core.chat.New Chat'), updateTime: new Date() }; const activeChat = histories.find((item) => item.chatId === activeChatId); return !activeChat ? [newChat].concat(formatHistories) : formatHistories; }, [activeChatId, histories, t]); // custom title edit const { onOpenModal, EditModal: EditTitleModal } = useEditTitle({ title: t('common:core.chat.Custom History Title'), placeholder: t('common:core.chat.Custom History Title Description') }); const { openConfirm, ConfirmModal } = useConfirm({ content: confirmClearText }); const canRouteToDetail = useMemo( () => appId && userInfo?.team.permission.hasWritePer && showRouteToAppDetail, [appId, userInfo?.team.permission.hasWritePer, showRouteToAppDetail] ); return ( {isPc && ( canRouteToDetail && router.push({ pathname: '/app/detail', query: { appId } }) } > {appName} )} {/* menu */} {!isPc && ( {t('common:core.chat.History')} )} {/* Clear */} {isPc && histories.length > 0 && ( } onClick={() => openConfirm(() => { onClearHistory(); })() } /> )} {/* chat history */} <> {concatHistory.map((item, i) => ( { onChangeChatId(item.id); setQuoteData(undefined); } })} {...(i !== concatHistory.length - 1 && { mb: '8px' })} > {item.customTitle || item.title} {!!item.id && ( {t(formatTimeToChatTime(item.updateTime) as any).replace('#', ':')} } aria-label={''} /> } menuList={[ { children: [ { label: item.top ? t('common:core.chat.Unpin') : t('common:core.chat.Pin'), icon: 'core/chat/setTopLight', onClick: () => { onUpdateHistory({ chatId: item.id, top: !item.top }); } }, { label: t('common:common.Custom Title'), icon: 'common/customTitleLight', onClick: () => { onOpenModal({ defaultVal: item.customTitle || item.title, onSuccess: (e) => onUpdateHistory({ chatId: item.id, customTitle: e }) }); } }, { label: t('common:common.Delete'), icon: 'delete', onClick: () => { onDelHistory(item.id); if (item.id === activeChatId) { onChangeChatId(); setQuoteData(undefined); } }, type: 'danger' } ] } ]} /> )} ))} {/* exec */} {!isPc && !!canRouteToDetail && ( router.push('/dashboard/apps')} > } bg={'white'} boxShadow={'1px 1px 9px rgba(0,0,0,0.15)'} size={'smSquare'} borderRadius={'50%'} aria-label={''} /> {t('common:core.chat.Exit Chat')} )} ); }; export default ChatHistorySlider;