import React, { useMemo, useState } from 'react'; import { Box, Button, Flex, useTheme, Menu, MenuButton, MenuList, MenuItem, IconButton } from '@chakra-ui/react'; import { useSystemStore } from '@/web/common/system/useSystemStore'; import { useEditTitle } from '@/web/common/hooks/useEditTitle'; import { useRouter } from 'next/router'; import Avatar from '@/components/Avatar'; import MyTooltip from '@/components/MyTooltip'; import MyIcon from '@/components/Icon'; import { useTranslation } from 'next-i18next'; import { useConfirm } from '@/web/common/hooks/useConfirm'; import Tabs from '@/components/Tabs'; import { useUserStore } from '@/web/support/user/useUserStore'; import { useQuery } from '@tanstack/react-query'; import { useAppStore } from '@/web/core/app/store/useAppStore'; import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant'; type HistoryItemType = { id: string; title: string; customTitle?: string; top?: boolean; }; enum TabEnum { 'app' = 'app', 'history' = 'history' } const ChatHistorySlider = ({ appId, appName, appAvatar, history, activeChatId, onChangeChat, onDelHistory, onClearHistory, onSetHistoryTop, onSetCustomTitle, onClose }: { appId?: string; appName: string; appAvatar: string; history: HistoryItemType[]; activeChatId: string; onChangeChat: (chatId?: string) => void; onDelHistory: (e: { chatId: string }) => void; onClearHistory: () => void; onSetHistoryTop?: (e: { chatId: string; top: boolean }) => void; onSetCustomTitle?: (e: { chatId: string; title: string }) => void; onClose: () => void; }) => { const theme = useTheme(); const router = useRouter(); const { t } = useTranslation(); const { isPc } = useSystemStore(); const { myApps, loadMyApps } = useAppStore(); const { userInfo } = useUserStore(); const [currentTab, setCurrentTab] = useState<`${TabEnum}`>(TabEnum.history); const isShare = useMemo(() => !appId || !userInfo, [appId, userInfo]); // custom title edit const { onOpenModal, EditModal: EditTitleModal } = useEditTitle({ title: '自定义历史记录标题', placeholder: '如果设置为空,会自动跟随聊天记录。' }); const { openConfirm, ConfirmModal } = useConfirm({ content: isShare ? t('chat.Confirm to clear share chat history') : t('chat.Confirm to clear history') }); const concatHistory = useMemo( () => !activeChatId ? [{ id: activeChatId, title: t('chat.New Chat') }].concat(history) : history, [activeChatId, history, t] ); useQuery(['init'], () => { if (isShare) { setCurrentTab(TabEnum.history); return null; } return loadMyApps(false); }); const canRouteToDetail = useMemo( () => appId && userInfo?.team.role !== TeamMemberRoleEnum.visitor, [appId, userInfo?.team.role] ); return ( {isPc && ( canRouteToDetail && router.replace({ pathname: '/app/detail', query: { appId } }) } > {appName} )} {/* menu */} {!isPc && !isShare && ( setCurrentTab(e as `${TabEnum}`)} /> )} {(isPc || isShare) && ( )} {/* chat history */} {(currentTab === TabEnum.history || isPc) && ( <> {concatHistory.map((item, i) => ( { onChangeChat(item.id); } })} > {item.customTitle || item.title} {!!item.id && ( { e.stopPropagation(); }} > {onSetHistoryTop && ( { e.stopPropagation(); onSetHistoryTop({ chatId: item.id, top: !item.top }); }} > {item.top ? '取消置顶' : '置顶'} )} {onSetCustomTitle && ( { e.stopPropagation(); onOpenModal({ defaultVal: item.customTitle || item.title, onSuccess: (e) => onSetCustomTitle({ chatId: item.id, title: e }) }); }} > {t('common.Custom Title')} )} { e.stopPropagation(); onDelHistory({ chatId: item.id }); if (item.id === activeChatId) { onChangeChat(); } }} > 删除 )} ))} )} {currentTab === TabEnum.app && !isPc && ( <> {myApps.map((item) => ( { router.replace({ query: { appId: item._id } }); onClose(); } })} > {item.name} ))} )} {!isPc && appId && ( router.push('/app/list')} > } bg={'white'} boxShadow={'1px 1px 9px rgba(0,0,0,0.15)'} h={'28px'} size={'sm'} borderRadius={'50%'} aria-label={''} /> {t('chat.Exit Chat')} )} ); }; export default ChatHistorySlider;