Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadTimeline.tsx

65 lines2.4 KB

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 readyMarkdownCwd,
27 readyWorkspaceRoot,
28 ...timelineProps
29}: Omit<ComponentProps<typeof MessagesTimeline>, "timelineEntries"> & {
30 /** Null until the thread's detail may load; see resolveThreadDetailRef. */
31 threadDetailRef: ScopedThreadRef | null;
32 entriesInput: Omit<TimelineEntriesInput, "visibleTurnItems">;
33 /** A local draft's optimistic rows; null for a server thread. */
34 draftEntries: ReadonlyArray<TimelineEntry> | null;
35 activeThreadKey: string | null;
36 syncing: boolean;
37 detailLoading: boolean;
38 readyMarkdownCwd: string | null;
39 readyWorkspaceRoot: string | null;
40}) {
41 const visibleTurnItems = useThreadVisibleTurnItems(threadDetailRef);
42 const serverEntries = useIncrementalTimelineEntries(activeThreadKey, {
43 ...entriesInput,
44 visibleTurnItems,
45 });
46 const entries = draftEntries ?? serverEntries;
47 const displayed = resolveThreadSwitchTimeline({
48 loading: entries.length === 0 && syncing,
49 activeThreadKey,
50 nextEntries: entries,
51 rememberedForActive: peekRememberedThreadTimeline<typeof entries>(activeThreadKey),
52 });
53 useLayoutEffect(() => {
54 if (detailLoading || entries.length === 0 || timelineHasEphemeralPreviewUrls(entries)) {
55 return;
56 }
57 rememberReadyThreadTimeline({
58 threadKey: activeThreadKey,
59 entries,
60 markdownCwd: readyMarkdownCwd,
61 workspaceRoot: readyWorkspaceRoot,
62 });
63 }, [activeThreadKey, detailLoading, entries, readyMarkdownCwd, readyWorkspaceRoot]);
64 return <MessagesTimeline {...timelineProps} timelineEntries={displayed.entries} />;