Skip to content

Commit b5a0c81

Browse files
refactor(web): the timeline attaches its find reader from a layout effect
useImperativeHandle with a module function as the ref was a layout effect in disguise; keep the registration as the ordinary lifetime effect it is. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 618ed45 commit b5a0c81

3 files changed

Lines changed: 11 additions & 16 deletions

File tree

‎apps/web/src/components/chat/threadFindStore.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("thread find store", () => {
4141
navigationId: 2,
4242
requestId: 2,
4343
});
44-
detach?.();
44+
detach();
4545
});
4646

4747
it("opens a search result's find for a thread before its timeline shows it", () => {
@@ -56,7 +56,7 @@ describe("thread find store", () => {
5656
// Another thread's timeline does not answer for this one.
5757
setThreadFindQuery(threadKey, "Caf");
5858
expect(readPosition).not.toHaveBeenCalled();
59-
detach?.();
59+
detach();
6060
});
6161

6262
it("closes find once the route leaves its thread", () => {

‎apps/web/src/components/chat/threadFindStore.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const CLOSED_THREAD_FIND: ThreadFindState = {
4646
export const useThreadFindStore = create<ThreadFindState>(() => CLOSED_THREAD_FIND);
4747

4848
/** What find needs from the timeline that shows a thread. */
49-
export interface ThreadFindTimeline {
49+
interface ThreadFindTimeline {
5050
readonly threadKey: string;
5151
readonly readPosition: ThreadFindPositionReader;
5252
}
@@ -57,11 +57,8 @@ function timelineFor(threadKey: string): ThreadFindTimeline | null {
5757
return attachedTimeline?.threadKey === threadKey ? attachedTimeline : null;
5858
}
5959

60-
/** Ref for the timeline's find handle, attached while the timeline shows a thread. */
61-
export function attachThreadFindTimeline(
62-
timeline: ThreadFindTimeline | null,
63-
): (() => void) | undefined {
64-
if (!timeline) return;
60+
/** Attaches the timeline showing a thread; returns the detach. */
61+
export function attachThreadFindTimeline(timeline: ThreadFindTimeline): () => void {
6562
attachedTimeline = timeline;
6663
return () => {
6764
if (attachedTimeline === timeline) attachedTimeline = null;

‎apps/web/src/components/chat/useThreadFindPositionReader.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useImperativeHandle } from "react";
1+
import { useLayoutEffect } from "react";
22
import type { RunId } from "@t3tools/contracts";
33
import type { TimelineEntry } from "../../session-logic";
44
import {
@@ -7,7 +7,7 @@ import {
77
type TimelineLatestRun,
88
} from "./MessagesTimeline.logic";
99
import type { ThreadFindPositionReader } from "./threadFind";
10-
import { attachThreadFindTimeline, type ThreadFindTimeline } from "./threadFindStore";
10+
import { attachThreadFindTimeline } from "./threadFindStore";
1111
import { readThreadFindPosition } from "./threadFindPosition";
1212

1313
/** Hands thread find how to read the find position of this timeline's viewport. */
@@ -34,11 +34,9 @@ export function useThreadFindPositionReader({
3434
readonly rows: MessagesTimelineRow[];
3535
readonly contentInsetEndAdjustment: number;
3636
}) {
37-
useImperativeHandle<
38-
ThreadFindTimeline | null,
39-
ThreadFindTimeline | null
40-
>(attachThreadFindTimeline, () => {
41-
if (!timelineViewportElement) return null;
37+
// Layout effect so a find opened right after a thread switch reads this viewport.
38+
useLayoutEffect(() => {
39+
if (!timelineViewportElement) return;
4240
const read: ThreadFindPositionReader = (query) => {
4341
const foldRunIds =
4442
findFoldRunIds ??
@@ -85,7 +83,7 @@ export function useThreadFindPositionReader({
8583
contentInsetEndAdjustment,
8684
);
8785
};
88-
return { threadKey, readPosition: read };
86+
return attachThreadFindTimeline({ threadKey, readPosition: read });
8987
}, [
9088
contentInsetEndAdjustment,
9189
findFoldRunIds,

0 commit comments

Comments
 (0)