view love/epi/src/components/ChatUI/ChatUI.tsx @ 279:b3b547563ec7

Add Google connector service and agent wiki Implement the C/Seobeo Google Drive and Gmail connector with encrypted OAuth storage, Zenbu authentication, browser testing, AI tool discovery, chunked HTTP decoding, and Bazel coverage. Consolidate repository guidance into progressive wiki documentation and enforce arena-first allocation for new first-party C code. Co-authored-by: Copilot <[email protected]> Copilot-Session: 84c338fd-0939-4bb3-b7f3-1062eb213e5d
author MrJuneJune <me@mrjunejune.com>
date Mon, 17 Aug 2026 22:22:36 -0700
parents cf9caa4abc3e
children
line wrap: on
line source

import { MessageContainer } from './MessageContainer';
import { Composer } from './Composer';
import { useChat } from '@/hooks/useChat';
import { useEffect, useRef, useState } from 'react';
import { apiUrl } from '@/utils';
import { currentChatId } from '@/atoms/chatAtoms';
import { useAtom } from 'jotai';

export function ChatUI({ chatId }: {chatId: string}) {
  const [loading, setLoading] = useState(false);
  const { messages, setMessages } = useChat(chatId);
  const scrollRef = useRef<HTMLDivElement>(null);
  const setCurrentChatId = useAtom(currentChatId)[1];
  setCurrentChatId(chatId);

  // Ignore warning since I want to render once.
  useEffect(() => {
    const loadHistory = async () => {
      if (!chatId || chatId === 'new') return;
      setLoading(true);
      try {
        const res = await fetch(apiUrl(`/chats/${chatId}/messages`));
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        const data = await res.json();
        if (data.messages.length > 0) {
          setMessages(data.messages);
        }
      } catch (err) {
        console.error('Error from loading history: ', err);
        setMessages([]);
      } finally {
        setLoading(false);
      }
    };
    console.log('loadhistory');
    loadHistory();
  }, [chatId]);

  useEffect(() => {
    // This is not ideal, but couldnt' get CSS to work. 
    const timer = setTimeout(() => {
      scrollRef.current?.scrollTo({
        top: scrollRef.current.scrollHeight,
        behavior: 'smooth',
      });
    }, 100);
  
    return () => clearTimeout(timer);
  }, [messages]);

  return (
    <div className="flex flex-col bg-gray-100 dark:bg-black h-screen">
      <div
        ref={scrollRef}
        className="flex-1 overflow-y-scroll"
      >
        {loading && (
          <div className="flex justify-center items-center h-full">
            <span>Loading messages...</span>
          </div>
        )}
        <MessageContainer messages={messages} />
      </div>
      <Composer chatId={chatId} />
    </div>
  );
}