@devries-cameron20 / t3code-1 / commits / ed16d9f241
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): thread find navigation compiles without manual memoization (#20603)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 22:02 UTC ed16d9f2416951366ecc20607df125912420d558
1 file changed +117 -132
apps/web/src/components/chat/useThreadFindNavigation.ts
+117 -132
@@ -1,5 +1,5 @@
1 import type { LegendListRef } from "@legendapp/list/react";
2 -import { useCallback, useEffect, useLayoutEffect, useRef, type RefObject } from "react";
2 +import { useEffect, useLayoutEffect, useRef, type RefObject } from "react";
3 import type { ThreadFindMatch } from "./threadFind";
4 import { useThreadFindHighlights } from "./threadFindHighlights";
5
@@ -102,147 +102,132 @@ export function useThreadFindNavigation({
102
103 historyControls.onLoadEarlier(match.entryId);
104 }, [entries, historyControls, listReady, match, matchKey, rowIndex]);
105 - const reveal = useCallback(
106 - (range: Range | null) => {
107 - activeRangeRef.current = range;
108 - if (!matchKey) {
109 - positioningRef.current = null;
110 - settledMatchRef.current = null;
111 - revealedMatchRef.current = null;
112 - pagingPositionRef.current = null;
113 - return;
114 - }
115 - const pagingPosition = pagingPositionRef.current;
116 - if (pagingPosition && pagingPosition.key !== matchKey) pagingPositionRef.current = null;
117 - if (
118 - range?.startContainer.isConnected &&
119 - pagingPosition?.key === matchKey &&
120 - pagingPosition.entries !== entries
121 - ) {
122 - const restore = () => {
123 - const pinned = pagingPositionRef.current;
124 - const currentRange = activeRangeRef.current;
125 - if (
126 - currentMatchKeyRef.current !== matchKey ||
127 - !pinned ||
128 - !currentRange?.startContainer.isConnected
129 - )
130 - return;
131 - const delta = currentRange.getBoundingClientRect().top - pinned.top;
132 - const scroll = listRef.current?.getState?.().scroll;
133 - if (Math.abs(delta) >= 1 && typeof scroll === "number")
134 - listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });
135 - };
136 - pagingPosition.entries = entries;
137 - restore();
138 - // The list measures newly inserted activity after the first layout pass.
139 - requestAnimationFrame(() =>
140 - requestAnimationFrame(() => {
141 - restore();
142 - if (!historyControls?.loading && pagingPositionRef.current === pagingPosition)
143 - pagingPositionRef.current = null;
144 - }),
145 - );
146 - }
147 - if (historyControls?.error) pagingPositionRef.current = null;
148 - if (revealedMatchRef.current === matchKey) return;
149 - if (!listReady || rowIndex < 0) return;
150 - const materialize = () => {
151 - const list = listRef.current;
152 - const previous = positioningRef.current;
105 + const reveal = (range: Range | null) => {
106 + activeRangeRef.current = range;
107 + if (!matchKey) {
108 + positioningRef.current = null;
109 + settledMatchRef.current = null;
110 + revealedMatchRef.current = null;
111 + pagingPositionRef.current = null;
112 + return;
113 + }
114 + const pagingPosition = pagingPositionRef.current;
115 + if (pagingPosition && pagingPosition.key !== matchKey) pagingPositionRef.current = null;
116 + if (
117 + range?.startContainer.isConnected &&
118 + pagingPosition?.key === matchKey &&
119 + pagingPosition.entries !== entries
120 + ) {
121 + const restore = () => {
122 + const pinned = pagingPositionRef.current;
123 + const currentRange = activeRangeRef.current;
124 if (
154 - !list ||
155 - (previous?.key === matchKey &&
156 - previous.rowIndex === rowIndex &&
157 - previous.entries === entries)
125 + currentMatchKeyRef.current !== matchKey ||
126 + !pinned ||
127 + !currentRange?.startContainer.isConnected
128 )
129 return;
160 - // Activity can insert rows while LegendList waits for layout. Retarget the
161 - // pending jump instead of treating the first requested index as final.
162 - const request = { key: matchKey, rowIndex, entries };
163 - positioningRef.current = request;
164 -
165 - void list
166 - .scrollToIndex({
167 - index: rowIndex,
168 - animated: false,
169 - viewOffset: FIND_MATCH_VIEW_MARGIN,
170 - })
171 - .then(() => {
172 - if (positioningRef.current !== request || currentMatchKeyRef.current !== matchKey)
173 - return;
174 -
175 - positionedEntriesRef.current = entries;
176 - settledMatchRef.current = matchKey;
177 - revealRef.current(activeRangeRef.current);
178 - });
130 + const delta = currentRange.getBoundingClientRect().top - pinned.top;
131 + const scroll = listRef.current?.getState?.().scroll;
132 + if (Math.abs(delta) >= 1 && typeof scroll === "number")
133 + listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });
134 };
180 - if (!range) {
181 - if (container) materialize();
182 - return;
183 - }
184 -
185 - const codeScroller = range.startContainer.parentElement?.closest("pre");
186 - if (codeScroller) {
187 - const rect = range.getBoundingClientRect();
188 - const viewport = codeScroller.getBoundingClientRect();
189 - if (rect.left < viewport.left) codeScroller.scrollLeft += rect.left - viewport.left - 16;
190 - else if (rect.right > viewport.right)
191 - codeScroller.scrollLeft += rect.right - viewport.right + 16;
192 - }
193 - const rect = range.getBoundingClientRect();
194 - const viewport = container?.getBoundingClientRect();
195 - if (!viewport || rect.height === 0) return;
196 - const margin = Math.min(
197 - FIND_MATCH_VIEW_MARGIN,
198 - Math.max(0, (viewport.height - contentInsetEndAdjustment - rect.height) / 2),
135 + pagingPosition.entries = entries;
136 + restore();
137 + // The list measures newly inserted activity after the first layout pass.
138 + requestAnimationFrame(() =>
139 + requestAnimationFrame(() => {
140 + restore();
141 + if (!historyControls?.loading && pagingPositionRef.current === pagingPosition)
142 + pagingPositionRef.current = null;
143 + }),
144 );
200 - const top = viewport.top + margin;
201 - const bottom = viewport.bottom - margin - contentInsetEndAdjustment;
202 - const delta =
203 - rect.top < top ? rect.top - top : rect.bottom > bottom ? rect.bottom - bottom : 0;
204 - // A new list can expose DOM text before its virtual row sizes settle.
205 - // Await row positioning before measuring an offscreen occurrence.
145 + }
146 + if (historyControls?.error) pagingPositionRef.current = null;
147 + if (revealedMatchRef.current === matchKey) return;
148 + if (!listReady || rowIndex < 0) return;
149 + const materialize = () => {
150 + const list = listRef.current;
151 + const previous = positioningRef.current;
152 if (
207 - Math.abs(delta) >= 1 &&
208 - positionedEntriesRef.current !== entries &&
209 - settledMatchRef.current !== matchKey
210 - ) {
211 - materialize();
153 + !list ||
154 + (previous?.key === matchKey &&
155 + previous.rowIndex === rowIndex &&
156 + previous.entries === entries)
157 + )
158 return;
213 - }
214 - const scroll = listRef.current?.getState?.().scroll;
215 - positionedEntriesRef.current = entries;
159 + // Activity can insert rows while LegendList waits for layout. Retarget the
160 + // pending jump instead of treating the first requested index as final.
161 + const request = { key: matchKey, rowIndex, entries };
162 + positioningRef.current = request;
163
217 - revealedMatchRef.current = matchKey;
218 - if (historyControls?.loading)
219 - pagingPositionRef.current = { key: matchKey, entries, top: rect.top - delta };
220 - if (Math.abs(delta) >= 1 && typeof scroll === "number") {
221 - listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });
222 - requestAnimationFrame(() => {
223 - if (currentMatchKeyRef.current !== matchKey) return;
224 - if (
225 - listRef.current?.getState?.().scroll === scroll &&
226 - range.getBoundingClientRect().top === rect.top
227 - )
228 - return;
229 - revealedMatchRef.current = null;
164 + void list
165 + .scrollToIndex({
166 + index: rowIndex,
167 + animated: false,
168 + viewOffset: FIND_MATCH_VIEW_MARGIN,
169 + })
170 + .then(() => {
171 + if (positioningRef.current !== request || currentMatchKeyRef.current !== matchKey) return;
172 +
173 + positionedEntriesRef.current = entries;
174 + settledMatchRef.current = matchKey;
175 revealRef.current(activeRangeRef.current);
176 });
232 - }
233 - },
234 - [
235 - container,
236 - contentInsetEndAdjustment,
237 - entries,
238 - historyControls?.loading,
239 - historyControls?.error,
240 - listRef,
241 - listReady,
242 - matchKey,
243 - rowIndex,
244 - ],
245 - );
177 + };
178 + if (!range) {
179 + if (container) materialize();
180 + return;
181 + }
182 +
183 + const codeScroller = range.startContainer.parentElement?.closest("pre");
184 + if (codeScroller) {
185 + const rect = range.getBoundingClientRect();
186 + const viewport = codeScroller.getBoundingClientRect();
187 + if (rect.left < viewport.left) codeScroller.scrollLeft += rect.left - viewport.left - 16;
188 + else if (rect.right > viewport.right)
189 + codeScroller.scrollLeft += rect.right - viewport.right + 16;
190 + }
191 + const rect = range.getBoundingClientRect();
192 + const viewport = container?.getBoundingClientRect();
193 + if (!viewport || rect.height === 0) return;
194 + const margin = Math.min(
195 + FIND_MATCH_VIEW_MARGIN,
196 + Math.max(0, (viewport.height - contentInsetEndAdjustment - rect.height) / 2),
197 + );
198 + const top = viewport.top + margin;
199 + const bottom = viewport.bottom - margin - contentInsetEndAdjustment;
200 + const delta = rect.top < top ? rect.top - top : rect.bottom > bottom ? rect.bottom - bottom : 0;
201 + // A new list can expose DOM text before its virtual row sizes settle.
202 + // Await row positioning before measuring an offscreen occurrence.
203 + if (
204 + Math.abs(delta) >= 1 &&
205 + positionedEntriesRef.current !== entries &&
206 + settledMatchRef.current !== matchKey
207 + ) {
208 + materialize();
209 + return;
210 + }
211 + const scroll = listRef.current?.getState?.().scroll;
212 + positionedEntriesRef.current = entries;
213 +
214 + revealedMatchRef.current = matchKey;
215 + if (historyControls?.loading)
216 + pagingPositionRef.current = { key: matchKey, entries, top: rect.top - delta };
217 + if (Math.abs(delta) >= 1 && typeof scroll === "number") {
218 + listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });
219 + requestAnimationFrame(() => {
220 + if (currentMatchKeyRef.current !== matchKey) return;
221 + if (
222 + listRef.current?.getState?.().scroll === scroll &&
223 + range.getBoundingClientRect().top === rect.top
224 + )
225 + return;
226 + revealedMatchRef.current = null;
227 + revealRef.current(activeRangeRef.current);
228 + });
229 + }
230 + };
231
232 useLayoutEffect(() => {
233 revealRef.current = reveal;