import ResouceModal from "@/pages/ChatAppPage/components/ResouceModal"; import ThumbsMessage from "@/pages/ChatAppPage/components/ThumbsMessage"; import { useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import FileBs from "./FileBs"; import MessageBs from "./MessageBs"; import MessageSystem from "./MessageSystem"; import MessageUser from "./MessageUser"; import RunLog from "./RunLog"; import Separator from "./Separator"; import { useMessageStore } from "./messageStore"; export default function MessagePanne({ useName, guideWord, loadMore, flow_type }) { const { t } = useTranslation() const { chatId, messages } = useMessageStore() // 反馈 const thumbRef = useRef(null) // 溯源 const sourceRef = useRef(null) // 自动滚动 const messagesRef = useRef(null) const scrollLockRef = useRef(false) useEffect(() => { scrollLockRef.current = false queryLockRef.current = false }, [chatId]) useEffect(() => { if (scrollLockRef.current) return messagesRef.current.scrollTop = messagesRef.current.scrollHeight; }, [messages]) // 消息滚动加载 const queryLockRef = useRef(false) useEffect(() => { function handleScroll() { if (queryLockRef.current) return const { scrollTop, clientHeight, scrollHeight } = messagesRef.current // 距离底部 600px内,开启自动滚动 scrollLockRef.current = (scrollHeight - scrollTop - clientHeight) > 600 if (messagesRef.current.scrollTop <= 90) { console.log('请求 :>> ', 1); queryLockRef.current = true loadMore() // TODO 翻页定位 // 临时处理防抖 setTimeout(() => { queryLockRef.current = false }, 1000); } } messagesRef.current?.addEventListener('scroll', handleScroll); return () => messagesRef.current?.removeEventListener('scroll', handleScroll) }, [messagesRef.current, messages, chatId]); return
{guideWord && } { messages.map(msg => { // 工厂 let type = 'llm' if (msg.isSend) { type = 'user' } else if (msg.category === 'divider') { type = 'separator' } else if (msg.files?.length) { type = 'file' } else if (['tool', 'flow', 'knowledge'].includes(msg.category)) { type = 'runLog' } else if (msg.thought) { type = 'system' } switch (type) { case 'user': return ; case 'llm': return { thumbRef.current?.openModal(chatId) }} onSource={(data) => { sourceRef.current?.openModal(data) }} />; case 'system': return ; case 'separator': return ; case 'file': return ; case 'runLog': return ; default: return
未知消息类型
; } }) } {/* 踩 反馈 */} {/* 源文件类型 */}
};