f
This commit is contained in:
parent
60152a64f0
commit
c6be061130
@ -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'));
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
setConversationId(sessionId);
|
setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
|
||||||
// // try {
|
return;
|
||||||
// // if (webSocketManager && typeof (webSocketManager as any).send === 'function') {
|
}
|
||||||
// // (webSocketManager as any).send({
|
|
||||||
// // type: 'Chat',
|
setConversationId(sessionId);
|
||||||
// // session_id: sessionId,
|
try {
|
||||||
// // message: text
|
if (webSocketManager && typeof (webSocketManager as any).send === 'function') {
|
||||||
// // });
|
(webSocketManager as any).send({
|
||||||
// // } else {
|
type: 'Chat',
|
||||||
// // throw new Error('WebSocket manager is not ready');
|
session_id: sessionId,
|
||||||
// // }
|
message: text
|
||||||
// // } catch (wsErr) {
|
});
|
||||||
// // console.error('WebSocket send failed:', wsErr);
|
} else {
|
||||||
// // //
|
throw new Error('WebSocket manager is not ready');
|
||||||
// // setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
|
}
|
||||||
// // }
|
} catch (wsErr) {
|
||||||
// } catch (err) {
|
console.error('WebSocket send failed:', wsErr);
|
||||||
// console.error('handleCase failed:', err);
|
setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
|
||||||
// //
|
}
|
||||||
// // setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
|
} catch (err) {
|
||||||
// } finally {
|
console.error('handleCase failed:', err);
|
||||||
// inFlightRef.current = false;
|
|
||||||
// }
|
setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
|
||||||
|
} finally {
|
||||||
|
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,68 +80,68 @@ 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'));
|
||||||
}}>
|
}}>
|
||||||
<ThemedText style={styles.case}>
|
<ThemedText style={styles.case}>
|
||||||
{t('ask:ask.case1')}
|
{t('ask:ask.case1')}
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
<TouchableOpacity onPress={() => {
|
<TouchableOpacity onPress={() => {
|
||||||
handleCase(t('ask:ask.case2'));
|
handleCase(t('ask:ask.case2'));
|
||||||
}}>
|
}}>
|
||||||
<ThemedText style={styles.case}>
|
<ThemedText style={styles.case}>
|
||||||
{t('ask:ask.case2')}
|
{t('ask:ask.case2')}
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
<TouchableOpacity onPress={() => {
|
<TouchableOpacity onPress={() => {
|
||||||
handleCase(t('ask:ask.case3'));
|
handleCase(t('ask:ask.case3'));
|
||||||
}}>
|
}}>
|
||||||
<ThemedText style={styles.case}>
|
<ThemedText style={styles.case}>
|
||||||
{t('ask:ask.case3')}
|
{t('ask:ask.case3')}
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View> */}
|
</View>
|
||||||
</View>
|
</View>
|
||||||
{/* </ScrollView> */}
|
</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"
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user