TypeScript
perf(web): thread find navigation compiles without manual memoization (#20603)
Julius Marmingeed16d9f24169
diff --git a/apps/web/src/components/chat/useThreadFindNavigation.ts b/apps/web/src/components/chat/useThreadFindNavigation.tsindex 9f6d8c893b0..c6381119d51 100644--- a/apps/web/src/components/chat/useThreadFindNavigation.ts+++ b/apps/web/src/components/chat/useThreadFindNavigation.ts@@ -1,5 +1,5 @@import type { LegendListRef } from "@legendapp/list/react";-import { useCallback, useEffect, useLayoutEffect, useRef, type RefObject } from "react";+import { useEffect, useLayoutEffect, useRef, type RefObject } from "react";import type { ThreadFindMatch } from "./threadFind";import { useThreadFindHighlights } from "./threadFindHighlights";@@ -102,147 +102,132 @@ export function useThreadFindNavigation({historyControls.onLoadEarlier(match.entryId);}, [entries, historyControls, listReady, match, matchKey, rowIndex]);- const reveal = useCallback(- (range: Range | null) => {- activeRangeRef.current = range;- if (!matchKey) {- positioningRef.current = null;- settledMatchRef.current = null;- revealedMatchRef.current = null;- pagingPositionRef.current = null;- return;- }- const pagingPosition = pagingPositionRef.current;- if (pagingPosition && pagingPosition.key !== matchKey) pagingPositionRef.current = null;- if (- range?.startContainer.isConnected &&- pagingPosition?.key === matchKey &&- pagingPosition.entries !== entries- ) {- const restore = () => {- const pinned = pagingPositionRef.current;- const currentRange = activeRangeRef.current;- if (- currentMatchKeyRef.current !== matchKey ||- !pinned ||- !currentRange?.startContainer.isConnected- )- return;- const delta = currentRange.getBoundingClientRect().top - pinned.top;- const scroll = listRef.current?.getState?.().scroll;- if (Math.abs(delta) >= 1 && typeof scroll === "number")- listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });- };- pagingPosition.entries = entries;- restore();- // The list measures newly inserted activity after the first layout pass.- requestAnimationFrame(() =>- requestAnimationFrame(() => {- restore();- if (!historyControls?.loading && pagingPositionRef.current === pagingPosition)- pagingPositionRef.current = null;- }),- );- }- if (historyControls?.error) pagingPositionRef.current = null;- if (revealedMatchRef.current === matchKey) return;- if (!listReady || rowIndex < 0) return;- const materialize = () => {- const list = listRef.current;- const previous = positioningRef.current;+ const reveal = (range: Range | null) => {+ activeRangeRef.current = range;+ if (!matchKey) {+ positioningRef.current = null;+ settledMatchRef.current = null;+ revealedMatchRef.current = null;+ pagingPositionRef.current = null;+ return;+ }+ const pagingPosition = pagingPositionRef.current;+ if (pagingPosition && pagingPosition.key !== matchKey) pagingPositionRef.current = null;+ if (+ range?.startContainer.isConnected &&+ pagingPosition?.key === matchKey &&+ pagingPosition.entries !== entries+ ) {+ const restore = () => {+ const pinned = pagingPositionRef.current;+ const currentRange = activeRangeRef.current;if (- !list ||- (previous?.key === matchKey &&- previous.rowIndex === rowIndex &&- previous.entries === entries)+ currentMatchKeyRef.current !== matchKey ||+ !pinned ||+ !currentRange?.startContainer.isConnected)return;- // Activity can insert rows while LegendList waits for layout. Retarget the- // pending jump instead of treating the first requested index as final.- const request = { key: matchKey, rowIndex, entries };- positioningRef.current = request;-- void list- .scrollToIndex({- index: rowIndex,- animated: false,- viewOffset: FIND_MATCH_VIEW_MARGIN,- })- .then(() => {- if (positioningRef.current !== request || currentMatchKeyRef.current !== matchKey)- return;-- positionedEntriesRef.current = entries;- settledMatchRef.current = matchKey;- revealRef.current(activeRangeRef.current);- });+ const delta = currentRange.getBoundingClientRect().top - pinned.top;+ const scroll = listRef.current?.getState?.().scroll;+ if (Math.abs(delta) >= 1 && typeof scroll === "number")+ listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });};- if (!range) {- if (container) materialize();- return;- }-- const codeScroller = range.startContainer.parentElement?.closest("pre");- if (codeScroller) {- const rect = range.getBoundingClientRect();- const viewport = codeScroller.getBoundingClientRect();- if (rect.left < viewport.left) codeScroller.scrollLeft += rect.left - viewport.left - 16;- else if (rect.right > viewport.right)- codeScroller.scrollLeft += rect.right - viewport.right + 16;- }- const rect = range.getBoundingClientRect();- const viewport = container?.getBoundingClientRect();- if (!viewport || rect.height === 0) return;- const margin = Math.min(- FIND_MATCH_VIEW_MARGIN,- Math.max(0, (viewport.height - contentInsetEndAdjustment - rect.height) / 2),+ pagingPosition.entries = entries;+ restore();+ // The list measures newly inserted activity after the first layout pass.+ requestAnimationFrame(() =>+ requestAnimationFrame(() => {+ restore();+ if (!historyControls?.loading && pagingPositionRef.current === pagingPosition)+ pagingPositionRef.current = null;+ }),);- const top = viewport.top + margin;- const bottom = viewport.bottom - margin - contentInsetEndAdjustment;- const delta =- rect.top < top ? rect.top - top : rect.bottom > bottom ? rect.bottom - bottom : 0;- // A new list can expose DOM text before its virtual row sizes settle.- // Await row positioning before measuring an offscreen occurrence.+ }+ if (historyControls?.error) pagingPositionRef.current = null;+ if (revealedMatchRef.current === matchKey) return;+ if (!listReady || rowIndex < 0) return;+ const materialize = () => {+ const list = listRef.current;+ const previous = positioningRef.current;if (- Math.abs(delta) >= 1 &&- positionedEntriesRef.current !== entries &&- settledMatchRef.current !== matchKey- ) {- materialize();+ !list ||+ (previous?.key === matchKey &&+ previous.rowIndex === rowIndex &&+ previous.entries === entries)+ )return;- }- const scroll = listRef.current?.getState?.().scroll;- positionedEntriesRef.current = entries;+ // Activity can insert rows while LegendList waits for layout. Retarget the+ // pending jump instead of treating the first requested index as final.+ const request = { key: matchKey, rowIndex, entries };+ positioningRef.current = request;- revealedMatchRef.current = matchKey;- if (historyControls?.loading)- pagingPositionRef.current = { key: matchKey, entries, top: rect.top - delta };- if (Math.abs(delta) >= 1 && typeof scroll === "number") {- listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });- requestAnimationFrame(() => {- if (currentMatchKeyRef.current !== matchKey) return;- if (- listRef.current?.getState?.().scroll === scroll &&- range.getBoundingClientRect().top === rect.top- )- return;- revealedMatchRef.current = null;+ void list+ .scrollToIndex({+ index: rowIndex,+ animated: false,+ viewOffset: FIND_MATCH_VIEW_MARGIN,+ })+ .then(() => {+ if (positioningRef.current !== request || currentMatchKeyRef.current !== matchKey) return;++ positionedEntriesRef.current = entries;+ settledMatchRef.current = matchKey;revealRef.current(activeRangeRef.current);});- }- },- [- container,- contentInsetEndAdjustment,- entries,- historyControls?.loading,- historyControls?.error,- listRef,- listReady,- matchKey,- rowIndex,- ],- );+ };+ if (!range) {+ if (container) materialize();+ return;+ }++ const codeScroller = range.startContainer.parentElement?.closest("pre");+ if (codeScroller) {+ const rect = range.getBoundingClientRect();+ const viewport = codeScroller.getBoundingClientRect();+ if (rect.left < viewport.left) codeScroller.scrollLeft += rect.left - viewport.left - 16;+ else if (rect.right > viewport.right)+ codeScroller.scrollLeft += rect.right - viewport.right + 16;+ }+ const rect = range.getBoundingClientRect();+ const viewport = container?.getBoundingClientRect();+ if (!viewport || rect.height === 0) return;+ const margin = Math.min(+ FIND_MATCH_VIEW_MARGIN,+ Math.max(0, (viewport.height - contentInsetEndAdjustment - rect.height) / 2),+ );+ const top = viewport.top + margin;+ const bottom = viewport.bottom - margin - contentInsetEndAdjustment;+ const delta = rect.top < top ? rect.top - top : rect.bottom > bottom ? rect.bottom - bottom : 0;+ // A new list can expose DOM text before its virtual row sizes settle.+ // Await row positioning before measuring an offscreen occurrence.+ if (+ Math.abs(delta) >= 1 &&+ positionedEntriesRef.current !== entries &&+ settledMatchRef.current !== matchKey+ ) {+ materialize();+ return;+ }+ const scroll = listRef.current?.getState?.().scroll;+ positionedEntriesRef.current = entries;++ revealedMatchRef.current = matchKey;+ if (historyControls?.loading)+ pagingPositionRef.current = { key: matchKey, entries, top: rect.top - delta };+ if (Math.abs(delta) >= 1 && typeof scroll === "number") {+ listRef.current?.scrollToOffset({ offset: scroll + delta, animated: false });+ requestAnimationFrame(() => {+ if (currentMatchKeyRef.current !== matchKey) return;+ if (+ listRef.current?.getState?.().scroll === scroll &&+ range.getBoundingClientRect().top === rect.top+ )+ return;+ revealedMatchRef.current = null;+ revealRef.current(activeRangeRef.current);+ });+ }+ };useLayoutEffect(() => {revealRef.current = reveal;