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 IconArrowLeftWhite24 from "@/components/icon/IconArrowLeftWhite24"; import IconArrowLeft from "@/components/icon/icon-arrow-left"; import PersonalCard from "./components/PersonalCard"; import ButtonEnableStreamVoice from "./components/OptionButtons/ButtonEnableStreamVoice"; import RecommendQuestions from "./components/RecommendQuestions"; import { usePersistentState } from "@/hooks/usePersistentState"; // 导入我们抽离的 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, } = useChatMessages(agentId, isVisitor); // 获取原始历史消息列表长度(用于UI状态判断) const rawHistoryListLength = historyList.length; const { scrollViewRef, scrollTop, keyboardHeight, marginTopOffset, handleScrollToUpper, handleTouchMove, handleScrollToLower, } = useChatScrollManager(messagesLength, pageIndex, loadMore); // 获取当前消息列表长度 const currentMessageListLength = useTextChat((state) => state.list.length); const { showWelcome, haveBg, inputContainerHeight, inputContainerBottomOffset, setShowWelcome, getBgContent, createNavLeftRenderer, } = useChatUI(agent, rawHistoryListLength, currentMessageListLength); const [streamVoiceEnable, setStreamVoiceEnable] = usePersistentState( "streamVoiceEnable", false ); // InputBar 相关状态 const [isVoice, setIsVoice] = useState(false); const [disabled, setDisabled] = useState(false); const { destroy, genSessionId, clearList } = useTextChat(); // 统一的聊天输入逻辑 - 只在主组件中实例化一次 const chatInputActions = useChatInput({ agent, enableOutputAudioStream: streamVoiceEnable, setShowWelcome, setIsVoice, setDisabled, historyList, }); // 页面显示时刷新数据 useDidShow(() => { // 需要清掉当前已经进行的聊天 // clearList(); }); // 首次进入聊天生成 session id useEffect(() => { genSessionId(); // 重新拉取第一页数据, resetAndLoadFirstPage() }, [genSessionId]); // 页面卸载时清理 useUnload(() => { destroy(); }); // 加载更多的处理函数(已经在 useChatScrollManager 中处理) const onScrollToUpper = handleScrollToUpper; // 使用工厂函数创建导航栏左侧渲染器 const renderNavLeft = createNavLeftRenderer(PersonalCard, IconArrowLeftWhite24, IconArrowLeft); // console.log('----scrollTop: ', scrollTop, '----') return ( {/* <>{`${scrollTop}`}-- */} {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 && ( )} ); }