import { View, ScrollView } from "@tarojs/components"; import NavBarNormal from "@/components/NavBarNormal/index"; import PageCustom from "@/components/page-custom/index"; import { useDidShow, useRouter, useUnload } from "@tarojs/taro"; import ChatMessage from "@/components/chat-message"; import InputBar from "./components/InputBar"; import { useEffect, useState } from "react"; import { useTextChat } from "@/store/textChatStore"; import { formatMessageTime } from "@/utils/timeUtils"; import ChatGreeting from "./components/ChatGreeting"; import PersonalCard from "./components/PersonalCard"; import ButtonEnableStreamVoice from "./components/OptionButtons/ButtonEnableStreamVoice"; import RecommendQuestions from "./components/RecommendQuestions"; import { usePersistentState } from "@/hooks/usePersistentState"; import style from './index.module.less' // 导入我们抽离的 hooks 和常量 import { useChatMessages, useChatScrollManager, useChatUI, useChatAgent, } from "./hooks"; import { useChatInput } from "./components/InputBar/useChatInput"; export default function Index() { const router = useRouter(); const { agentId, isVisitor } = router.params; if (!agentId) { return 没有相应的智能体; } // 使用抽离的 hooks const { agent } = useChatAgent(agentId, isVisitor); const { historyList, groupedMessages, messagesLength, loadMore, pageIndex, mutate, resetAndLoadFirstPage, resetAndClearData } = useChatMessages(agentId, isVisitor); // const loadMoreCallback = isNewChat ? null : loadMore // 获取原始历史消息列表长度(用于UI状态判断) const rawHistoryListLength = historyList.length; const { scrollViewRef, scrollTop, keyboardHeight, marginTopOffset, handleScrollToUpper, handleTouchMove, handleScrollToLower, } = useChatScrollManager(messagesLength, pageIndex, loadMore); // InputBar 相关状态 const [isVoice, setIsVoice] = useState(false); const [disabled, setDisabled] = useState(false); const [contextMenuVisible, setContextMenuVisible] = useState(false); // 获取当前消息列表长度 const currentMessageListLength = useTextChat((state) => state.list.length); const isNewChat = useTextChat((state) => state.isNewChat); const messageStopHandle = useTextChat((state) => state.messageStopHandle); const { destroy, genSessionId, clearList } = useTextChat(); const { showWelcome, haveBg, inputContainerHeight, inputContainerBottomOffset, setShowWelcome, getBgContent, createNavLeftRenderer, } = useChatUI(agent, rawHistoryListLength, currentMessageListLength, isNewChat); const [streamVoiceEnable, setStreamVoiceEnable] = usePersistentState( "streamVoiceEnable", false ); // 统一的聊天输入逻辑 - 只在主组件中实例化一次 const chatInputActions = useChatInput({ agent, enableOutputAudioStream: streamVoiceEnable, setShowWelcome, setIsVoice, setDisabled, historyList, }); // 首次进入聊天生成 session id useEffect(() => { genSessionId(); // 重新拉取第一页数据, resetAndLoadFirstPage() }, [genSessionId]); // 页面卸载时清理 useUnload(() => { destroy(); }); // 加载更多的处理函数(已经在 useChatScrollManager 中处理) const onScrollToUpper = handleScrollToUpper; // const PersonalCardWrap = ()=> // 使用工厂函数创建导航栏左侧渲染器 const renderNavLeft = createNavLeftRenderer(PersonalCard, contextMenuVisible, ()=> { console.log('clear chat') messageStopHandle?.(); clearList() resetAndClearData() }, (menuVisible: boolean) => { setContextMenuVisible(menuVisible) }); // 页面点击时关闭清除上下文菜单 const handlePageClick = () => { setContextMenuVisible(false) } return ( {/* <>{`${isNewChat ? 'isnew': 'notnew'}`}-- */} {isNewChat && 聊聊新话题} {showWelcome && } {groupedMessages.map((group, groupIndex) => { return ( {formatMessageTime(group.dt)} {group.list.map((message) => { const reasoningContent = (message as any).reasoningContent || ""; return ( ); })} ); })} {agent && ( )} {/* ${keyboardHeight <= 0 ? 'transition-[bottom] delay-300' : ''} */} {agent && ( )} 0 ? 'opacity-0': ''} `}>内容由AI生成,仅供参考 ); }