Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { type ComponentProps, useLayoutEffect } from "react";
3import { useIncrementalTimelineEntries } from "../../hooks/useIncrementalTimelineEntries";
4import type { TimelineEntriesInput, TimelineEntry } from "../../session-logic";
5import { useThreadVisibleTurnItems } from "../../state/entities";
6import {
7 peekRememberedThreadTimeline,
8 rememberReadyThreadTimeline,
9 resolveThreadSwitchTimeline,
10 timelineHasEphemeralPreviewUrls,
11} from "../ChatView.logic";
12import { MessagesTimeline } from "./MessagesTimeline";
13
14/**
15 * The timeline with the streamed text. ChatView reads the thread through its quiet
16 * atoms, which hold through text deltas; this reads the live turn items, so a
17 * delta re-renders the timeline without re-rendering ChatView.
18 */
19export function ThreadTimeline({
20 threadDetailRef,
21 entriesInput,
22 draftEntries,
23 activeThreadKey,
24 syncing,
25 detailLoading,
26 ...timelineProps
27}: Omit<ComponentProps<typeof MessagesTimeline>, "timelineEntries"> & {
28 /** Null until the thread's detail may load; see resolveThreadDetailRef. */
29 threadDetailRef: ScopedThreadRef | null;
30 entriesInput: Omit<TimelineEntriesInput, "visibleTurnItems">;
31 /** A local draft's optimistic rows; null for a server thread. */
32 draftEntries: ReadonlyArray<TimelineEntry> | null;
33 activeThreadKey: string | null;
34 syncing: boolean;
35 detailLoading: boolean;
36}) {
37 const visibleTurnItems = useThreadVisibleTurnItems(threadDetailRef);
38 const serverEntries = useIncrementalTimelineEntries(activeThreadKey, {
39 ...entriesInput,
40 visibleTurnItems,
41 });
42 const entries = draftEntries ?? serverEntries;
43 const displayedEntries = resolveThreadSwitchTimeline({
44 loading: entries.length === 0 && syncing,
45 activeThreadKey,
46 nextEntries: entries,
47 rememberedForActive: peekRememberedThreadTimeline<typeof entries>(activeThreadKey),
48 });
49 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- stores the committed ready timeline in the module-level cache used during thread switches
50 useLayoutEffect(() => {
51 if (detailLoading || entries.length === 0 || timelineHasEphemeralPreviewUrls(entries)) {
52 return;
53 }
54 rememberReadyThreadTimeline({ threadKey: activeThreadKey, entries });
55 }, [activeThreadKey, detailLoading, entries]);
56 return <MessagesTimeline {...timelineProps} timelineEntries={displayedEntries} />;
57}