Skip to content

The Open Standard for Generative UI

4.5 KB
Raw Blame
import React, { useCallback, useRef, useState } from "react";
import { FlatList, KeyboardAvoidingView, Platform, StyleSheet, Text, View } from "react-native";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
import { BotBubble } from "../components/BotBubble";
import { ChatInput } from "../components/ChatInput";
import { SuggestedPrompts } from "../components/SuggestedPrompts";
import { UserBubble } from "../components/UserBubble";
import { StreamMessage, useStreamingChat } from "../hooks/useStreamingChat";
import { pushStream } from "../store/streamStore";

// ─── Message types ────────────────────────────────────────────────────────────

type UserMessage = { id: string; type: "user"; text: string };
type BotMessage = { id: string; type: "bot" };
type ErrorMessage = { id: string; type: "error"; text: string };
type ChatMessage = UserMessage | BotMessage | ErrorMessage;

let _counter = 0;
const uid = () => String(++_counter);

// ─── ChatScreen ───────────────────────────────────────────────────────────────

type Props = { backendUrl: string };

export default function ChatScreen({ backendUrl }: Props) {
  const [messages, setMessages] = useState<ChatMessage[]>([]);
  const [isStreaming, setIsStreaming] = useState(false);
  const { sendMessage } = useStreamingChat(backendUrl);
  const historyRef = useRef<StreamMessage[]>([]);

  const handleSend = useCallback(
    async (text: string) => {
      // 1. User message
      setMessages((prev) => [{ id: uid(), type: "user", text }, ...prev]);

      // 2. Conversation history
      const newHistory: StreamMessage[] = [...historyRef.current, { role: "user", content: text }];
      historyRef.current = newHistory;

      // 3. Bot placeholder — content lives in the stream store, not here
      const botId = uid();
      pushStream(botId, { openui: "", isStreaming: true });
      setMessages((prev) => [{ id: botId, type: "bot" }, ...prev]);
      setIsStreaming(true);

      // 4. Stream chunks into the store
      await sendMessage(
        newHistory,
        (accumulated, isDone) => {
          pushStream(botId, { openui: accumulated, isStreaming: !isDone });
          if (isDone) {
            setIsStreaming(false);
            historyRef.current = [...newHistory, { role: "assistant", content: accumulated }];
          }
        },
        // Show failures (e.g. a rejected key) as plain text in place of the bot reply.
        (message) => {
          setIsStreaming(false);
          setMessages((prev) =>
            prev.map((m) => (m.id === botId ? { id: botId, type: "error", text: message } : m)),
          );
        },
      );
    },
    [sendMessage],
  );

  const renderItem = useCallback(({ item }: { item: ChatMessage }) => {
    if (item.type === "user") {
      return <UserBubble text={item.text} />;
    }
    if (item.type === "error") {
      return (
        <View style={styles.errorBubble}>
          <Text style={styles.errorText}>{item.text}</Text>
        </View>
      );
    }
    return <BotBubble messageId={item.id} />;
  }, []);

  return (
    <SafeAreaProvider>
      <SafeAreaView style={styles.screen}>
        <KeyboardAvoidingView
          style={styles.flex}
          behavior={Platform.OS === "ios" ? "padding" : "height"}
          keyboardVerticalOffset={Platform.OS === "ios" ? 8 : 0}
        >
          <FlatList
            data={messages}
            renderItem={renderItem}
            keyExtractor={(m) => m.id}
            inverted
            contentContainerStyle={styles.list}
            keyboardShouldPersistTaps="handled"
          />
          <SuggestedPrompts onSelect={handleSend} />
          <ChatInput onSend={handleSend} disabled={isStreaming} />
        </KeyboardAvoidingView>
      </SafeAreaView>
    </SafeAreaProvider>
  );
}

const styles = StyleSheet.create({
  errorBubble: {
    alignSelf: "flex-start",
    maxWidth: "85%",
    backgroundColor: "#fef2f2",
    borderColor: "#fecaca",
    borderWidth: 1,
    borderRadius: 12,
    paddingHorizontal: 14,
    paddingVertical: 10,
  },
  errorText: {
    color: "#b91c1c",
    fontSize: 14,
    lineHeight: 20,
  },
  screen: {
    flex: 1,
    backgroundColor: "#f9fafb",
  },
  flex: {
    flex: 1,
  },
  list: {
    paddingHorizontal: 16,
    paddingVertical: 12,
    gap: 8,
  },
});