f
This commit is contained in:
parent
85d9b823de
commit
0482f23d97
@ -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() {
|
||||
}
|
||||
]}
|
||||
>
|
||||
{/* <AskHello setUserMessages={setUserMessages} setConversationId={setConversationId} setIsHello={setIsHello} /> */}
|
||||
<AskHello setUserMessages={setUserMessages} setConversationId={setConversationId} setIsHello={setIsHello} />
|
||||
</Animated.View>
|
||||
|
||||
{/* 聊天页面 */}
|
||||
|
||||
@ -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,7 +16,14 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell
|
||||
const width = Dimensions.get('window').width;
|
||||
const height = Dimensions.get('window').height;
|
||||
|
||||
const handleCase = async (text: string) => {
|
||||
//
|
||||
const inFlightRef = useRef(false);
|
||||
|
||||
const handleCase = useCallback(async (text: string) => {
|
||||
if (inFlightRef.current) return;
|
||||
inFlightRef.current = true;
|
||||
try {
|
||||
// UI
|
||||
setIsHello(false);
|
||||
setUserMessages([
|
||||
{
|
||||
@ -34,18 +41,38 @@ export default function AskHello({ setUserMessages, setConversationId, setIsHell
|
||||
]);
|
||||
|
||||
const sessionId = await createNewConversation(text);
|
||||
if (sessionId) {
|
||||
if (!sessionId) {
|
||||
console.error("Failed to create a new conversation.");
|
||||
//
|
||||
setUserMessages(prev => prev.filter(item => item.content !== 'keepSearchIng'));
|
||||
return;
|
||||
}
|
||||
|
||||
setConversationId(sessionId);
|
||||
webSocketManager.send({
|
||||
try {
|
||||
if (webSocketManager && typeof (webSocketManager as any).send === 'function') {
|
||||
(webSocketManager as any).send({
|
||||
type: 'Chat',
|
||||
session_id: sessionId,
|
||||
message: text
|
||||
});
|
||||
} else {
|
||||
console.error("Failed to create a new conversation.");
|
||||
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 (
|
||||
<View className="flex-1 bg-white w-full">
|
||||
{/* <ScrollView
|
||||
@ -106,7 +133,6 @@ const styles = StyleSheet.create({
|
||||
flexDirection: 'row',
|
||||
flexWrap: 'wrap',
|
||||
justifyContent: 'center',
|
||||
gap: 8,
|
||||
width: '100%',
|
||||
marginTop: 16
|
||||
},
|
||||
@ -115,6 +141,8 @@ const styles = StyleSheet.create({
|
||||
borderColor: "#AC7E35",
|
||||
borderRadius: 10,
|
||||
paddingHorizontal: 8,
|
||||
marginHorizontal: 4,
|
||||
marginVertical: 4,
|
||||
width: 'auto',
|
||||
fontSize: 14,
|
||||
color: "#4C320C"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user