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

useThreadFindPositionReader.ts

102 lines3.5 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 useLayoutEffect(() => {
41 if (!timelineViewportElement) return;
42 const read: ThreadFindPositionReader = (query) => {
43 const foldRunIds =
44 findFoldRunIds ??
45 timelineTurnFoldRunIdsByEntryId({
46 timelineEntries,
47 latestRun,
48 runningRunId,
49 isWorking,
50 runlessWorkActive,
51 });
52 const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));
53 return readThreadFindPosition(
54 timelineViewportElement,
55 query,
56 (rowId) => {
57 const rowIndex = rows.findIndex((row) => row.id === rowId);
58 for (const row of rows.slice(Math.max(0, rowIndex))) {
59 if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {
60 const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);
61 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
62 return entry.id;
63 continue;
64 }
65 const entry = timelineEntries.find((entry) => {
66 const runId =
67 row.kind === "turn-fold"
68 ? foldRunIds.get(entry.id)
69 : entry.kind === "message"
70 ? entry.message.runId
71 : entry.kind === "proposed-plan"
72 ? entry.proposedPlan.runId
73 : null;
74 return (
75 runId === row.runId &&
76 (entry.kind !== "message" || entry.message.role === "assistant") &&
77 (row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)
78 );
79 });
80 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
81 return entry.id;
82 }
83 return undefined;
84 },
85 contentInsetEndAdjustment,
86 );
87 };
88 return attachThreadFindTimeline({ threadKey, readPosition: read, navigate });
89 }, [
90 contentInsetEndAdjustment,
91 findFoldRunIds,
92 isWorking,
93 navigate,
94 threadKey,
95 latestRun,
96 rows,
97 runlessWorkActive,
98 runningRunId,
99 timelineEntries,
100 timelineViewportElement,
101 ]);