Open History to see the last commit
1import { useLayoutEffect } from "react";
2import type { RunId } from "@t3tools/contracts";
3import type { TimelineEntry } from "../../session-logic";
4import {
5 timelineTurnFoldRunIdsByEntryId,
6 type MessagesTimelineRow,
7 type TimelineLatestRun,
8} from "./MessagesTimeline.logic";
9import type { ThreadFindPositionReader } from "./threadFind";
10import { readThreadFindPosition } from "./threadFindPosition";
11
12/** Publishes how to read the find position of the timeline's current viewport. */
13export function useThreadFindPositionReader({
14 findPositionReaderRef,
15 timelineViewportElement,
16 findFoldRunIds,
17 timelineEntries,
18 latestRun,
19 runningRunId,
20 isWorking,
21 runlessWorkActive,
22 rows,
23 contentInsetEndAdjustment,
24}: {
25 readonly findPositionReaderRef: React.RefObject<ThreadFindPositionReader | null> | undefined;
26 readonly timelineViewportElement: HTMLDivElement | null;
27 readonly findFoldRunIds: ReadonlyMap<string, RunId> | null;
28 readonly timelineEntries: ReadonlyArray<TimelineEntry>;
29 readonly latestRun: TimelineLatestRun | null;
30 readonly runningRunId: RunId | null;
31 readonly isWorking: boolean;
32 readonly runlessWorkActive: boolean;
33 readonly rows: MessagesTimelineRow[];
34 readonly contentInsetEndAdjustment: number;
35}) {
36 useLayoutEffect(() => {
37 if (!findPositionReaderRef || !timelineViewportElement) return;
38 const read: ThreadFindPositionReader = (query) => {
39 const foldRunIds =
40 findFoldRunIds ??
41 timelineTurnFoldRunIdsByEntryId({
42 timelineEntries,
43 latestRun,
44 runningRunId,
45 isWorking,
46 runlessWorkActive,
47 });
48 const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));
49 return readThreadFindPosition(
50 timelineViewportElement,
51 query,
52 (rowId) => {
53 const rowIndex = rows.findIndex((row) => row.id === rowId);
54 for (const row of rows.slice(Math.max(0, rowIndex))) {
55 if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {
56 const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);
57 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
58 return entry.id;
59 continue;
60 }
61 const entry = timelineEntries.find((entry) => {
62 const runId =
63 row.kind === "turn-fold"
64 ? foldRunIds.get(entry.id)
65 : entry.kind === "message"
66 ? entry.message.runId
67 : entry.kind === "proposed-plan"
68 ? entry.proposedPlan.runId
69 : null;
70 return (
71 runId === row.runId &&
72 (entry.kind !== "message" || entry.message.role === "assistant") &&
73 (row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)
74 );
75 });
76 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
77 return entry.id;
78 }
79 return undefined;
80 },
81 contentInsetEndAdjustment,
82 );
83 };
84 findPositionReaderRef.current = read;
85 return () => {
86 if (findPositionReaderRef.current === read) findPositionReaderRef.current = null;
87 };
88 }, [
89 contentInsetEndAdjustment,
90 findFoldRunIds,
91 findPositionReaderRef,
92 isWorking,
93 latestRun,
94 rows,
95 runlessWorkActive,
96 runningRunId,
97 timelineEntries,
98 timelineViewportElement,
99 ]);
100}