import React, { useCallback, useRef, useState } from 'react'; import type { MouseEvent } from 'react'; import { AddIcon } from '@chakra-ui/icons'; import { Box, Button, Flex, useTheme, Menu, MenuList, MenuItem, useOutsideClick } from '@chakra-ui/react'; import { ChatIcon } from '@chakra-ui/icons'; import { useRouter } from 'next/router'; import { formatTimeToChatTime } from '@/utils/tools'; import MyIcon from '@/components/Icon'; import type { ShareChatHistoryItemType, ExportChatType } from '@/types/chat'; import { useChatStore } from '@/store/chat'; import { useGlobalStore } from '@/store/global'; import styles from '../index.module.scss'; const PcSliderBar = ({ onclickDelHistory, onclickExportChat, onCloseSlider }: { onclickDelHistory: (historyId: string) => void; onclickExportChat: (type: ExportChatType) => void; onCloseSlider: () => void; }) => { const router = useRouter(); const { shareId = '', historyId = '' } = router.query as { shareId: string; historyId: string; }; const theme = useTheme(); const { isPc } = useGlobalStore(); const ContextMenuRef = useRef(null); const onclickContext = useRef(false); const [contextMenuData, setContextMenuData] = useState<{ left: number; top: number; history: ShareChatHistoryItemType; }>(); const { shareChatHistory } = useChatStore(); // close contextMenu useOutsideClick({ ref: ContextMenuRef, handler: () => { setTimeout(() => { if (contextMenuData && !onclickContext.current) { setContextMenuData(undefined); } }, 10); setTimeout(() => { onclickContext.current = false; }, 10); } }); const onclickContextMenu = useCallback( (e: MouseEvent, history: ShareChatHistoryItemType) => { e.preventDefault(); // 阻止默认右键菜单 if (!isPc) return; onclickContext.current = true; setContextMenuData({ left: e.clientX, top: e.clientY, history }); }, [isPc] ); const replaceChatPage = useCallback( ({ hId = '', shareId }: { hId?: string; shareId: string }) => { if (hId === historyId) return; router.replace(`/chat/share?shareId=${shareId}&historyId=${hId}`); !isPc && onCloseSlider(); }, [historyId, isPc, onCloseSlider, router] ); return ( {/* 新对话 */} {/* chat history */} {shareChatHistory.map((item) => ( replaceChatPage({ hId: item._id, shareId: item.shareId })} onContextMenu={(e) => onclickContextMenu(e, item)} > {item.title} {formatTimeToChatTime(item.updateTime)} {item.latestChat || '……'} {/* phone quick delete */} {!isPc && ( { e.stopPropagation(); onclickDelHistory(item._id); item._id === historyId && replaceChatPage({ shareId: item.shareId }); }} /> )} ))} {shareChatHistory.length === 0 && ( 还没有聊天记录 )} {/* context menu */} {contextMenuData && ( { onclickDelHistory(contextMenuData.history._id); contextMenuData.history._id === historyId && replaceChatPage({ shareId }); }} > 删除记录 onclickExportChat('html')}>导出HTML格式 onclickExportChat('pdf')}>导出PDF格式 onclickExportChat('md')}>导出Markdown格式 )} ); }; export default PcSliderBar;