f
This commit is contained in:
parent
b960ebac72
commit
bf12a157c5
@ -5,8 +5,8 @@ import { ThemedText } from "@/components/ThemedText";
|
||||
import { useWebSocketStreamHandler } from "@/hooks/useWebSocketStreamHandler";
|
||||
import { fetchApi } from "@/lib/server-api-util";
|
||||
import { Message } from "@/types/ask";
|
||||
import { useLocalSearchParams, useRouter } from "expo-router";
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { useFocusEffect, useLocalSearchParams, useRouter } from "expo-router";
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
Animated,
|
||||
@ -17,7 +17,7 @@ import {
|
||||
TouchableOpacity,
|
||||
View
|
||||
} from 'react-native';
|
||||
import { useSharedValue } from 'react-native-reanimated';
|
||||
import { useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
export default function AskScreen() {
|
||||
@ -44,91 +44,91 @@ export default function AskScreen() {
|
||||
}>();
|
||||
|
||||
// 创建一个安全的滚动函数
|
||||
// const scrollToEnd = useCallback((animated = true) => {
|
||||
// if (!isMountedRef.current || !chatListRef.current) return;
|
||||
const scrollToEnd = useCallback((animated = true) => {
|
||||
if (!isMountedRef.current || !chatListRef.current) return;
|
||||
|
||||
// // 清理之前的定时器
|
||||
// if (scrollTimeoutRef.current) {
|
||||
// clearTimeout(scrollTimeoutRef.current);
|
||||
// }
|
||||
// 清理之前的定时器
|
||||
if (scrollTimeoutRef.current) {
|
||||
clearTimeout(scrollTimeoutRef.current);
|
||||
}
|
||||
|
||||
// scrollTimeoutRef.current = setTimeout(() => {
|
||||
// if (isMountedRef.current && chatListRef.current) {
|
||||
// try {
|
||||
// chatListRef.current.scrollToEnd({ animated });
|
||||
// } catch (error) {
|
||||
// console.warn('滚动到底部失败:', error);
|
||||
// }
|
||||
// }
|
||||
// }, 100);
|
||||
// }, []);
|
||||
scrollTimeoutRef.current = setTimeout(() => {
|
||||
if (isMountedRef.current && chatListRef.current) {
|
||||
try {
|
||||
chatListRef.current.scrollToEnd({ animated });
|
||||
} catch (error) {
|
||||
console.warn('滚动到底部失败:', error);
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
}, []);
|
||||
|
||||
// 清理函数
|
||||
// const cleanup = useCallback(() => {
|
||||
// isMountedRef.current = false;
|
||||
const cleanup = useCallback(() => {
|
||||
isMountedRef.current = false;
|
||||
|
||||
// // 清理定时器
|
||||
// if (scrollTimeoutRef.current) {
|
||||
// clearTimeout(scrollTimeoutRef.current);
|
||||
// scrollTimeoutRef.current = null;
|
||||
// }
|
||||
// if (keyboardTimeoutRef.current) {
|
||||
// clearTimeout(keyboardTimeoutRef.current);
|
||||
// keyboardTimeoutRef.current = null;
|
||||
// }
|
||||
// 清理定时器
|
||||
if (scrollTimeoutRef.current) {
|
||||
clearTimeout(scrollTimeoutRef.current);
|
||||
scrollTimeoutRef.current = null;
|
||||
}
|
||||
if (keyboardTimeoutRef.current) {
|
||||
clearTimeout(keyboardTimeoutRef.current);
|
||||
keyboardTimeoutRef.current = null;
|
||||
}
|
||||
|
||||
// // 取消API请求
|
||||
// if (abortControllerRef.current) {
|
||||
// abortControllerRef.current.abort();
|
||||
// abortControllerRef.current = null;
|
||||
// }
|
||||
// }, []);
|
||||
// 取消API请求
|
||||
if (abortControllerRef.current) {
|
||||
abortControllerRef.current.abort();
|
||||
abortControllerRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
// useEffect(() => {
|
||||
// if (!isHello && userMessages.length > 0 && isMountedRef.current) {
|
||||
// scrollToEnd();
|
||||
// }
|
||||
// }, [userMessages, isHello, scrollToEnd]);
|
||||
useEffect(() => {
|
||||
if (!isHello && userMessages.length > 0 && isMountedRef.current) {
|
||||
scrollToEnd();
|
||||
}
|
||||
}, [userMessages, isHello, scrollToEnd]);
|
||||
|
||||
// useEffect(() => {
|
||||
// const keyboardDidShowListener = Keyboard.addListener(
|
||||
// 'keyboardDidShow',
|
||||
// (e) => {
|
||||
// if (keyboardTimeoutRef.current) {
|
||||
// clearTimeout(keyboardTimeoutRef.current);
|
||||
// }
|
||||
useEffect(() => {
|
||||
const keyboardDidShowListener = Keyboard.addListener(
|
||||
'keyboardDidShow',
|
||||
(e) => {
|
||||
if (keyboardTimeoutRef.current) {
|
||||
clearTimeout(keyboardTimeoutRef.current);
|
||||
}
|
||||
|
||||
// keyboardTimeoutRef.current = setTimeout(() => {
|
||||
// if (isMountedRef.current && !isHello) {
|
||||
// scrollToEnd();
|
||||
// }
|
||||
// }, 100);
|
||||
// }
|
||||
// );
|
||||
keyboardTimeoutRef.current = setTimeout(() => {
|
||||
if (isMountedRef.current && !isHello) {
|
||||
scrollToEnd();
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
);
|
||||
|
||||
// const keyboardDidHideListener = Keyboard.addListener(
|
||||
// 'keyboardDidHide',
|
||||
// () => {
|
||||
// if (keyboardTimeoutRef.current) {
|
||||
// clearTimeout(keyboardTimeoutRef.current);
|
||||
// }
|
||||
const keyboardDidHideListener = Keyboard.addListener(
|
||||
'keyboardDidHide',
|
||||
() => {
|
||||
if (keyboardTimeoutRef.current) {
|
||||
clearTimeout(keyboardTimeoutRef.current);
|
||||
}
|
||||
|
||||
// keyboardTimeoutRef.current = setTimeout(() => {
|
||||
// if (isMountedRef.current && !isHello) {
|
||||
// scrollToEnd(false);
|
||||
// }
|
||||
// }, 100);
|
||||
// }
|
||||
// );
|
||||
keyboardTimeoutRef.current = setTimeout(() => {
|
||||
if (isMountedRef.current && !isHello) {
|
||||
scrollToEnd(false);
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
);
|
||||
|
||||
// return () => {
|
||||
// keyboardDidShowListener.remove();
|
||||
// keyboardDidHideListener.remove();
|
||||
// if (keyboardTimeoutRef.current) {
|
||||
// clearTimeout(keyboardTimeoutRef.current);
|
||||
// }
|
||||
// };
|
||||
// }, [isHello, scrollToEnd]);
|
||||
return () => {
|
||||
keyboardDidShowListener.remove();
|
||||
keyboardDidHideListener.remove();
|
||||
if (keyboardTimeoutRef.current) {
|
||||
clearTimeout(keyboardTimeoutRef.current);
|
||||
}
|
||||
};
|
||||
}, [isHello, scrollToEnd]);
|
||||
|
||||
// 使用新的WebSocket流处理hook,使用实时模式
|
||||
const { subscribeToWebSocket } = useWebSocketStreamHandler({
|
||||
@ -153,25 +153,25 @@ export default function AskScreen() {
|
||||
// );
|
||||
|
||||
// 创建动画样式
|
||||
// const welcomeStyle = useAnimatedStyle(() => {
|
||||
// return {
|
||||
// opacity: fadeAnim.value,
|
||||
// pointerEvents: isHello ? 'auto' : 'none',
|
||||
// };
|
||||
// });
|
||||
const welcomeStyle = useAnimatedStyle(() => {
|
||||
return {
|
||||
opacity: fadeAnim.value,
|
||||
pointerEvents: isHello ? 'auto' : 'none',
|
||||
};
|
||||
});
|
||||
|
||||
// const chatStyle = useAnimatedStyle(() => {
|
||||
// return {
|
||||
// opacity: fadeAnimChat.value,
|
||||
// pointerEvents: isHello ? 'none' : 'auto',
|
||||
// };
|
||||
// });
|
||||
const chatStyle = useAnimatedStyle(() => {
|
||||
return {
|
||||
opacity: fadeAnimChat.value,
|
||||
pointerEvents: isHello ? 'none' : 'auto',
|
||||
};
|
||||
});
|
||||
|
||||
// 触发动画
|
||||
// useEffect(() => {
|
||||
// fadeAnim.value = withTiming(isHello ? 1 : 0, { duration: 300 });
|
||||
// fadeAnimChat.value = withTiming(isHello ? 0 : 1, { duration: 300 });
|
||||
// }, [isHello]);
|
||||
useEffect(() => {
|
||||
fadeAnim.value = withTiming(isHello ? 1 : 0, { duration: 300 });
|
||||
fadeAnimChat.value = withTiming(isHello ? 0 : 1, { duration: 300 });
|
||||
}, [isHello]);
|
||||
|
||||
useEffect(() => {
|
||||
if (sessionId && isMountedRef.current) {
|
||||
@ -200,29 +200,29 @@ export default function AskScreen() {
|
||||
}
|
||||
}, [sessionId, newSession]);
|
||||
|
||||
// useEffect(() => {
|
||||
// if (!isHello && isMountedRef.current) {
|
||||
// // 不再自动关闭键盘,让用户手动控制
|
||||
// // 这里可以添加其他需要在隐藏hello界面时执行的逻辑
|
||||
// scrollToEnd(false);
|
||||
// }
|
||||
// }, [isHello, scrollToEnd]);
|
||||
useEffect(() => {
|
||||
if (!isHello && isMountedRef.current) {
|
||||
// 不再自动关闭键盘,让用户手动控制
|
||||
// 这里可以添加其他需要在隐藏hello界面时执行的逻辑
|
||||
scrollToEnd(false);
|
||||
}
|
||||
}, [isHello, scrollToEnd]);
|
||||
|
||||
// useFocusEffect(
|
||||
// useCallback(() => {
|
||||
// if (!sessionId && isMountedRef.current) {
|
||||
// setIsHello(true);
|
||||
// setUserMessages([]);
|
||||
// }
|
||||
// }, [sessionId])
|
||||
// );
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
if (!sessionId && isMountedRef.current) {
|
||||
setIsHello(true);
|
||||
setUserMessages([]);
|
||||
}
|
||||
}, [sessionId])
|
||||
);
|
||||
|
||||
// 组件卸载时的清理
|
||||
// useEffect(() => {
|
||||
// return () => {
|
||||
// cleanup();
|
||||
// };
|
||||
// }, [cleanup]);
|
||||
// 组件卸载时的清理
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
cleanup();
|
||||
};
|
||||
}, [cleanup]);
|
||||
|
||||
return (
|
||||
<View style={[styles.container, { paddingTop: insets.top, paddingBottom: insets.bottom }]}>
|
||||
@ -253,7 +253,7 @@ export default function AskScreen() {
|
||||
{/* 欢迎页面 */}
|
||||
<Animated.View
|
||||
style={[styles.absoluteView,
|
||||
// welcomeStyle,
|
||||
welcomeStyle,
|
||||
{ zIndex: 1 }]}
|
||||
>
|
||||
<AskHello setUserMessages={setUserMessages} setConversationId={setConversationId} setIsHello={setIsHello} />
|
||||
@ -262,7 +262,7 @@ export default function AskScreen() {
|
||||
{/* 聊天页面 */}
|
||||
<Animated.View
|
||||
style={[styles.absoluteView,
|
||||
// chatStyle,
|
||||
chatStyle,
|
||||
{ zIndex: 0 }]}
|
||||
>
|
||||
<Chat
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user