|
| 1 | +import { Stack, useLocalSearchParams, useRouter } from "expo-router"; |
| 2 | +import { useCallback, useEffect, useState } from "react"; |
| 3 | +import { |
| 4 | + ActivityIndicator, |
| 5 | + Pressable, |
| 6 | + Text, |
| 7 | + TouchableOpacity, |
| 8 | + View, |
| 9 | +} from "react-native"; |
| 10 | +import Animated, { useAnimatedStyle } from "react-native-reanimated"; |
| 11 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 12 | +import { ChatInput } from "../../components/ChatInput"; |
| 13 | +import { MessagesList } from "../../components/MessagesList"; |
| 14 | +import { useGradualAnimation } from "../../hooks/useGradualAnimation"; |
| 15 | +import { useMaxStore } from "../../stores/maxStore"; |
| 16 | + |
| 17 | +export default function ConversationDetailScreen() { |
| 18 | + const { id } = useLocalSearchParams<{ id: string }>(); |
| 19 | + const router = useRouter(); |
| 20 | + const insets = useSafeAreaInsets(); |
| 21 | + const [loadError, setLoadError] = useState<string | null>(null); |
| 22 | + |
| 23 | + const { |
| 24 | + conversation, |
| 25 | + thread, |
| 26 | + streamingActive, |
| 27 | + conversationLoading, |
| 28 | + askMax, |
| 29 | + stopGeneration, |
| 30 | + loadConversation, |
| 31 | + resetThread, |
| 32 | + } = useMaxStore(); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (!id) return; |
| 36 | + |
| 37 | + setLoadError(null); |
| 38 | + loadConversation(id).catch((err) => { |
| 39 | + console.error("Failed to load conversation:", err); |
| 40 | + setLoadError("Failed to load conversation"); |
| 41 | + }); |
| 42 | + |
| 43 | + return () => { |
| 44 | + // Reset when leaving the screen |
| 45 | + resetThread(); |
| 46 | + }; |
| 47 | + }, [id, loadConversation, resetThread]); |
| 48 | + |
| 49 | + const handleSend = useCallback( |
| 50 | + async (message: string) => { |
| 51 | + await askMax(message); |
| 52 | + }, |
| 53 | + [askMax], |
| 54 | + ); |
| 55 | + |
| 56 | + const headerRight = useCallback(() => { |
| 57 | + if (streamingActive) { |
| 58 | + return ( |
| 59 | + <TouchableOpacity onPress={stopGeneration} className="px-2"> |
| 60 | + <Text className="font-medium text-red-500">Stop</Text> |
| 61 | + </TouchableOpacity> |
| 62 | + ); |
| 63 | + } |
| 64 | + return null; |
| 65 | + }, [streamingActive, stopGeneration]); |
| 66 | + |
| 67 | + const { height } = useGradualAnimation(); |
| 68 | + |
| 69 | + const contentPosition = useAnimatedStyle(() => { |
| 70 | + return { |
| 71 | + transform: [{ translateY: -height.value }], |
| 72 | + }; |
| 73 | + }, []); |
| 74 | + |
| 75 | + if (loadError) { |
| 76 | + return ( |
| 77 | + <> |
| 78 | + <Stack.Screen |
| 79 | + options={{ |
| 80 | + headerShown: true, |
| 81 | + headerTitle: "Error", |
| 82 | + headerBackTitle: "Back", |
| 83 | + headerStyle: { backgroundColor: "#09090b" }, |
| 84 | + headerTintColor: "#fff", |
| 85 | + }} |
| 86 | + /> |
| 87 | + <View className="flex-1 items-center justify-center bg-dark-bg px-4"> |
| 88 | + <Text className="mb-4 text-center text-red-400">{loadError}</Text> |
| 89 | + <Pressable |
| 90 | + onPress={() => router.back()} |
| 91 | + className="rounded-lg bg-dark-surface px-4 py-2" |
| 92 | + > |
| 93 | + <Text className="text-white">Go Back</Text> |
| 94 | + </Pressable> |
| 95 | + </View> |
| 96 | + </> |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + if (conversationLoading && thread.length === 0) { |
| 101 | + return ( |
| 102 | + <> |
| 103 | + <Stack.Screen |
| 104 | + options={{ |
| 105 | + headerShown: true, |
| 106 | + headerTitle: "Loading...", |
| 107 | + headerBackTitle: "Back", |
| 108 | + headerStyle: { backgroundColor: "#09090b" }, |
| 109 | + headerTintColor: "#fff", |
| 110 | + }} |
| 111 | + /> |
| 112 | + <View className="flex-1 items-center justify-center bg-dark-bg"> |
| 113 | + <ActivityIndicator size="large" color="#f97316" /> |
| 114 | + <Text className="mt-4 text-dark-text-muted"> |
| 115 | + Loading conversation... |
| 116 | + </Text> |
| 117 | + </View> |
| 118 | + </> |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + return ( |
| 123 | + <> |
| 124 | + <Stack.Screen |
| 125 | + options={{ |
| 126 | + headerShown: true, |
| 127 | + headerTitle: conversation?.title || "Conversation", |
| 128 | + headerBackTitle: "Back", |
| 129 | + headerStyle: { backgroundColor: "#09090b" }, |
| 130 | + headerTintColor: "#fff", |
| 131 | + headerTitleStyle: { |
| 132 | + fontWeight: "600", |
| 133 | + }, |
| 134 | + headerRight, |
| 135 | + }} |
| 136 | + /> |
| 137 | + <Animated.View className="flex-1 bg-dark-bg" style={[contentPosition]}> |
| 138 | + <MessagesList |
| 139 | + messages={thread} |
| 140 | + streamingActive={streamingActive} |
| 141 | + contentContainerStyle={{ |
| 142 | + paddingTop: 80 + insets.bottom, |
| 143 | + paddingBottom: 16, |
| 144 | + flexGrow: thread.length === 0 ? 1 : undefined, |
| 145 | + }} |
| 146 | + /> |
| 147 | + |
| 148 | + {/* Fixed input at bottom */} |
| 149 | + <View className="absolute inset-x-0 bottom-0"> |
| 150 | + <ChatInput onSend={handleSend} disabled={streamingActive} /> |
| 151 | + </View> |
| 152 | + </Animated.View> |
| 153 | + </> |
| 154 | + ); |
| 155 | +} |
0 commit comments