"use client"; import { useEffect, useRef } from "react"; import { useChatStore } from "@/lib/chatStore"; import type { SourceDoc } from "@/lib/types"; import { AssistantMessage, ErrorMessage, UserMessage } from "./Message"; import { Thinking } from "./Thinking"; export function Thread({ onOpenSource, }: { onOpenSource?: (doc: SourceDoc, index: number) => void; }) { const conv = useChatStore((s) => s.activeId ? s.conversations[s.activeId] : null ); const ref = useRef(null); // Auto-scroll to bottom on new turns / when streaming tokens arrive const turns = conv?.turns ?? []; const lastTurn = turns[turns.length - 1]; useEffect(() => { const el = ref.current; if (!el) return; el.scrollTo({ top: el.scrollHeight, behavior: "smooth" }); }, [ turns.length, lastTurn?.pending, lastTurn?.response?.answer?.length, ]); if (!conv || conv.turns.length === 0) return null; return (
{conv.turns.map((t) => { const hasAnswer = Boolean(t.response?.answer); const showThinking = t.pending && !hasAnswer && !t.error; const showAnswer = !!t.response && (hasAnswer || !t.pending); return (
{showThinking && } {t.error && } {showAnswer && ( )}
); })}
); }