This commit is contained in:
Junhui Chen 2025-08-09 14:58:54 +08:00
parent 60152a64f0
commit c6be061130

View File

@ -1,8 +1,9 @@
import { ThemedText } from "@/components/ThemedText"; import { ThemedText } from "@/components/ThemedText";
import { webSocketManager } from "@/lib/websocket-util";
import { Message } from "@/types/ask"; import { Message } from "@/types/ask";
import { Dispatch, SetStateAction, useCallback, useRef } from "react"; import { Dispatch, SetStateAction, useCallback, useRef } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { View } from 'react-native'; import { Image, ScrollView, StyleSheet, TouchableOpacity, useWindowDimensions, View } from 'react-native';
import { createNewConversation } from "./utils"; import { createNewConversation } from "./utils";
interface AskHelloProps { interface AskHelloProps {
@ -12,68 +13,66 @@ interface AskHelloProps {
} }
export default function AskHello({ setUserMessages, setConversationId, setIsHello }: AskHelloProps) { export default function AskHello({ setUserMessages, setConversationId, setIsHello }: AskHelloProps) {
const { t } = useTranslation(); const { t } = useTranslation();
// const { width, height } = useWindowDimensions(); const { width, height } = useWindowDimensions();
//
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">
{/* <ScrollView <ScrollView
contentContainerStyle={{ contentContainerStyle={{
flexGrow: 1, flexGrow: 1,
paddingHorizontal: 8, paddingHorizontal: 8,
@ -81,22 +80,22 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell
}} }}
keyboardDismissMode="interactive" keyboardDismissMode="interactive"
keyboardShouldPersistTaps="handled" keyboardShouldPersistTaps="handled"
> */} >
<View className="items-center"> <View className="items-center">
<ThemedText style={{ fontSize: 32, fontWeight: 'bold', textAlign: 'center', lineHeight: 40, }}> <ThemedText style={{ fontSize: 32, fontWeight: 'bold', textAlign: 'center', lineHeight: 40, }}>
{t('ask.hi', { ns: 'ask' })} {t('ask.hi', { ns: 'ask' })}
{"\n"} {"\n"}
{t('ask.iAmMemo', { ns: 'ask' })} {t('ask.iAmMemo', { ns: 'ask' })}
</ThemedText> </ThemedText>
{/* <View> <View>
<Image source={require('@/assets/images/png/icon/ip.png')} style={{ width: width * 0.5, height: height * 0.3 }} /> <Image source={require('@/assets/images/png/icon/ip.png')} style={{ width: width * 0.5, height: height * 0.3 }} />
</View> */} </View>
<ThemedText className="!text-textPrimary text-center -mt-10" style={{ fontSize: 16 }}> <ThemedText className="!text-textPrimary text-center -mt-10" style={{ fontSize: 16 }}>
{t('ask.ready', { ns: 'ask' })} {t('ask.ready', { ns: 'ask' })}
{"\n"} {"\n"}
{t('ask.justAsk', { ns: 'ask' })} {t('ask.justAsk', { ns: 'ask' })}
</ThemedText> </ThemedText>
{/* <View style={styles.caseContainer}> <View style={styles.caseContainer}>
<TouchableOpacity onPress={() => { <TouchableOpacity onPress={() => {
handleCase(t('ask:ask.case1')); handleCase(t('ask:ask.case1'));
}}> }}>
@ -118,31 +117,31 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell
{t('ask:ask.case3')} {t('ask:ask.case3')}
</ThemedText> </ThemedText>
</TouchableOpacity> </TouchableOpacity>
</View> */}
</View> </View>
{/* </ScrollView> */} </View>
</ScrollView>
</View > </View >
); );
} }
// 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"
// } }
// }) })