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

useThreadFindPositionReader.ts

103 lines3.6 KB

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 { attachThreadFindTimeline } from "./threadFindStore";
11import { readThreadFindPosition } from "./threadFindPosition";
12​
13/** Hands thread find how to read the find position of this timeline's viewport. */
14export function useThreadFindPositionReader({
15 threadKey,
16 navigate,
17 timelineViewportElement,
18 findFoldRunIds,
19 timelineEntries,
20 latestRun,
21 runningRunId,
22 isWorking,
23 runlessWorkActive,
24 rows,
25 contentInsetEndAdjustment,
26}: {
27 readonly threadKey: string;
28 readonly navigate: () => void;
29 readonly timelineViewportElement: HTMLDivElement | null;
30 readonly findFoldRunIds: ReadonlyMap<string, RunId> | null;
31 readonly timelineEntries: ReadonlyArray<TimelineEntry>;
32 readonly latestRun: TimelineLatestRun | null;
33 readonly runningRunId: RunId | null;
34 readonly isWorking: boolean;
35 readonly runlessWorkActive: boolean;
36 readonly rows: MessagesTimelineRow[];
37 readonly contentInsetEndAdjustment: number;
38}) {
39 // Layout effect so a find opened right after a thread switch reads this viewport.
40 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches the timeline viewport's position reader to the thread find registry
41 useLayoutEffect(() => {
42 if (!timelineViewportElement) return;
43 const read: ThreadFindPositionReader = (query) => {
44 const foldRunIds =
45 findFoldRunIds ??
46 timelineTurnFoldRunIdsByEntryId({
47 timelineEntries,
48 latestRun,
49 runningRunId,
50 isWorking,
51 runlessWorkActive,
52 });
53 const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));
54 return readThreadFindPosition(
55 timelineViewportElement,
56 query,
57 (rowId) => {
58 const rowIndex = rows.findIndex((row) => row.id === rowId);
59 for (const row of rows.slice(Math.max(0, rowIndex))) {
60 if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {
61 const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);
62 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
63 return entry.id;
64 continue;
65 }
66 const entry = timelineEntries.find((entry) => {
67 const runId =
68 row.kind === "turn-fold"
69 ? foldRunIds.get(entry.id)
70 : entry.kind === "message"
71 ? entry.message.runId
72 : entry.kind === "proposed-plan"
73 ? entry.proposedPlan.runId
74 : null;
75 return (
76 runId === row.runId &&
77 (entry.kind !== "message" || entry.message.role === "assistant") &&
78 (row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)
79 );
80 });
81 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
82 return entry.id;
83 }
84 return undefined;
85 },
86 contentInsetEndAdjustment,
87 );
88 };
89 return attachThreadFindTimeline({ threadKey, readPosition: read, navigate });
90 }, [
91 contentInsetEndAdjustment,
92 findFoldRunIds,
93 isWorking,
94 navigate,
95 threadKey,
96 latestRun,
97 rows,
98 runlessWorkActive,
99 runningRunId,
100 timelineEntries,
101 timelineViewportElement,
102 ]);