This commit is contained in:
Junhui Chen 2025-08-09 11:43:36 +08:00
parent 384e607fe1
commit 76f2e6ed48

View File

@ -1,9 +1,8 @@
import { ThemedText } from "@/components/ThemedText"; import { ThemedText } from "@/components/ThemedText";
import { Message } from "@/types/ask"; import { Message } from "@/types/ask";
import { Dispatch, SetStateAction, useCallback, useRef } from "react"; import { Dispatch, SetStateAction, useRef } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { StyleSheet, View } from 'react-native'; import { View } from 'react-native';
import { createNewConversation } from "./utils";
interface AskHelloProps { interface AskHelloProps {
setUserMessages: Dispatch<SetStateAction<Message[]>>; setUserMessages: Dispatch<SetStateAction<Message[]>>;
@ -17,59 +16,59 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell
// //
const inFlightRef = useRef(false); const inFlightRef = useRef(false);
const handleCase = useCallback(async (text: string) => { // const handleCase = useCallback(async (text: string) => {
if (inFlightRef.current) return; // if (inFlightRef.current) return;
inFlightRef.current = true; // inFlightRef.current = true;
try { // try {
// UI // // UI
setIsHello(false); // setIsHello(false);
// setUserMessages([ // // setUserMessages([
// { // // {
// id: Math.random().toString(36).substring(2, 9), // // id: Math.random().toString(36).substring(2, 9),
// content: text, // // content: text,
// role: 'user', // // role: 'user',
// timestamp: new Date().toISOString() // // timestamp: new Date().toISOString()
// }, // // },
// { // // {
// id: Math.random().toString(36).substring(2, 9), // // id: Math.random().toString(36).substring(2, 9),
// content: "keepSearchIng", // // content: "keepSearchIng",
// role: 'assistant', // // role: 'assistant',
// timestamp: new Date().toISOString() // // timestamp: new Date().toISOString()
// } // // }
// ]); // // ]);
const sessionId = await createNewConversation(text); // const sessionId = await createNewConversation(text);
if (!sessionId) { // if (!sessionId) {
console.error("Failed to create a new conversation."); // console.error("Failed to create a new conversation.");
// // //
// setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng')); // // setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
return; // return;
} // }
setConversationId(sessionId); // setConversationId(sessionId);
// try { // // try {
// if (webSocketManager && typeof (webSocketManager as any).send === 'function') { // // if (webSocketManager && typeof (webSocketManager as any).send === 'function') {
// (webSocketManager as any).send({ // // (webSocketManager as any).send({
// type: 'Chat', // // type: 'Chat',
// session_id: sessionId, // // session_id: sessionId,
// message: text // // message: text
// }); // // });
// } else { // // } else {
// throw new Error('WebSocket manager is not ready'); // // throw new Error('WebSocket manager is not ready');
// } // // }
// } catch (wsErr) { // // } catch (wsErr) {
// console.error('WebSocket send failed:', wsErr); // // console.error('WebSocket send failed:', wsErr);
// // // // //
// setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng')); // // setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
// } // // }
} catch (err) { // } catch (err) {
console.error('handleCase failed:', err); // console.error('handleCase failed:', err);
// // //
// setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng')); // // setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
} finally { // } finally {
inFlightRef.current = false; // inFlightRef.current = false;
} // }
}, [setConversationId, setIsHello, setUserMessages]); // }, [setConversationId, setIsHello, setUserMessages]);
return ( return (
<View className="flex-1 bg-white w-full"> <View className="flex-1 bg-white w-full">
@ -126,23 +125,23 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell
} }
const styles = StyleSheet.create({ // const styles = StyleSheet.create({
caseContainer: { // caseContainer: {
flexDirection: 'row', // flexDirection: 'row',
flexWrap: 'wrap', // flexWrap: 'wrap',
justifyContent: 'center', // justifyContent: 'center',
width: '100%', // width: '100%',
marginTop: 16 // marginTop: 16
}, // },
case: { // case: {
borderWidth: 1, // borderWidth: 1,
borderColor: "#AC7E35", // borderColor: "#AC7E35",
borderRadius: 10, // borderRadius: 10,
paddingHorizontal: 8, // paddingHorizontal: 8,
marginHorizontal: 4, // marginHorizontal: 4,
marginVertical: 4, // marginVertical: 4,
width: 'auto', // width: 'auto',
fontSize: 14, // fontSize: 14,
color: "#4C320C" // color: "#4C320C"
} // }
}) // })