From 0482f23d978694142665da7558e2af18c0488e52 Mon Sep 17 00:00:00 2001 From: Junhui Chen Date: Sat, 9 Aug 2025 10:28:23 +0800 Subject: [PATCH] f --- app/(tabs)/ask.tsx | 3 +- components/ask/hello.tsx | 84 ++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/app/(tabs)/ask.tsx b/app/(tabs)/ask.tsx index 5575d5c..ac9bb8e 100644 --- a/app/(tabs)/ask.tsx +++ b/app/(tabs)/ask.tsx @@ -1,4 +1,5 @@ import ReturnArrow from "@/assets/icons/svg/returnArrow.svg"; +import AskHello from "@/components/ask/hello"; import { ThemedText } from "@/components/ThemedText"; import { fetchApi } from "@/lib/server-api-util"; import { Message } from "@/types/ask"; @@ -271,7 +272,7 @@ export default function AskScreen() { } ]} > - {/* */} + {/* 聊天页面 */} diff --git a/components/ask/hello.tsx b/components/ask/hello.tsx index f72ef93..a291afd 100644 --- a/components/ask/hello.tsx +++ b/components/ask/hello.tsx @@ -1,7 +1,7 @@ import { ThemedText } from "@/components/ThemedText"; import { webSocketManager } from "@/lib/websocket-util"; import { Message } from "@/types/ask"; -import { Dispatch, SetStateAction } from "react"; +import { Dispatch, SetStateAction, useCallback, useRef } from "react"; import { useTranslation } from "react-i18next"; import { Dimensions, Image, StyleSheet, TouchableOpacity, View } from 'react-native'; import { createNewConversation } from "./utils"; @@ -16,36 +16,63 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell const width = Dimensions.get('window').width; const height = Dimensions.get('window').height; - const handleCase = async (text: string) => { - setIsHello(false); - setUserMessages([ - { - id: Math.random().toString(36).substring(2, 9), - content: text, - role: 'user', - timestamp: new Date().toISOString() - }, - { - id: Math.random().toString(36).substring(2, 9), - content: "keepSearchIng", - role: 'assistant', - timestamp: new Date().toISOString() - } - ]); + // + const inFlightRef = useRef(false); + + const handleCase = useCallback(async (text: string) => { + if (inFlightRef.current) return; + inFlightRef.current = true; + try { + // UI + setIsHello(false); + setUserMessages([ + { + id: Math.random().toString(36).substring(2, 9), + content: text, + role: 'user', + timestamp: new Date().toISOString() + }, + { + id: Math.random().toString(36).substring(2, 9), + content: "keepSearchIng", + role: 'assistant', + timestamp: new Date().toISOString() + } + ]); + + const sessionId = await createNewConversation(text); + if (!sessionId) { + console.error("Failed to create a new conversation."); + // + setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng')); + return; + } - const sessionId = await createNewConversation(text); - if (sessionId) { setConversationId(sessionId); - webSocketManager.send({ - type: 'Chat', - session_id: sessionId, - message: text - }); - } else { - console.error("Failed to create a new conversation."); + try { + if (webSocketManager && typeof (webSocketManager as any).send === 'function') { + (webSocketManager as any).send({ + type: 'Chat', + session_id: sessionId, + message: text + }); + } else { + throw new Error('WebSocket manager is not ready'); + } + } catch (wsErr) { + console.error('WebSocket send failed:', wsErr); + // + setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng')); + } + } catch (err) { + console.error('handleCase failed:', err); + // setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng')); + } finally { + inFlightRef.current = false; } - } + }, [setConversationId, setIsHello, setUserMessages]); + return ( {/*