Migrate chat scrolling and branch lists to LegendList #1953
Mergedjuliusmarmingemerged 20 commits into
main fromrefs/pull/1953/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- @@ -287,20 +124 @@287- const row = rows[measurement.index];288- if (!row) {289- return [];290- }291- return [292- {293- id: row.id,294- kind: row.kind,295- index: measurement.index,296- size: measurement.size,297- start: measurement.start,298- end: measurement.end,299- },300- ];301- }),302- });303- }, [onVirtualizerSnapshot, rowVirtualizer, rows, virtualizedRowCount]);304-305- const virtualRows = rowVirtualizer.getVirtualItems();306- const nonVirtualizedRows = rows.slice(virtualizedRowCount);124+ }, [listRef, onIsAtEndChange]);apps/web/src/components/ChatView.tsx line 2901OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3046,4 +2898,4 @@30462898 activeThread,30472899 activeProposedPlan,30482900 beginLocalDispatch,3049- forceStickToBottom,2901+ scrollToEnd,
- @@ -443,20 +428 @@443- branchListScrollElementRef.current =444- (element?.parentElement as HTMLDivElement | null) ?? null;445- if (element) {446- branchListVirtualizer.measure();447- }448- },449- [branchListVirtualizer],450- );451-452- useEffect(() => {453- if (!isBranchMenuOpen || !shouldVirtualizeBranchList) return;454- queueMicrotask(() => {455- branchListVirtualizer.measure();456- });457- }, [458- branchListVirtualizer,459- filteredBranchPickerItems.length,460- isBranchMenuOpen,461- shouldVirtualizeBranchList,462- ]);428+ const branchListRef = useRef<LegendListRef | null>(null);
- @@ -2587,5 +2422,8 @@25872422 },25882423 ]);25892424 // Sending a message should always bring the latest user turn into view.2590- shouldAutoScrollRef.current = true;2591- forceStickToBottom();2425+ isAtEndRef.current = true;2426+ requestAnimationFrame(() => {2427+ legendListRef.current?.scrollToEnd?.({ animated: true });2428+ setShowScrollToBottom(false);2429+ });
- apps/web/src/components/BranchToolbarBranchSelector.tsx line 460Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -440,21 +427,0 @@440- const virtualBranchRows = branchListVirtualizer.getVirtualItems();441- const setBranchListRef = useCallback(442- (element: HTMLDivElement | null) => {443- branchListScrollElementRef.current =444- (element?.parentElement as HTMLDivElement | null) ?? null;445- if (element) {446- branchListVirtualizer.measure();447- }448- },449- [branchListVirtualizer],450- );451-452- useEffect(() => {453- if (!isBranchMenuOpen || !shouldVirtualizeBranchList) return;454- queueMicrotask(() => {455- branchListVirtualizer.measure();456- });457- }, [458- branchListVirtualizer,459- filteredBranchPickerItems.length,460- isBranchMenuOpen,
- apps/web/src/components/ChatView.tsx line 1978Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1970,9 +1955,3 @@19701955 [environmentId, serverThread],19711956 );197219571973- // Auto-scroll on new messages1974- const messageCount = timelineMessages.length;1975- const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {1976- const scrollContainer = messagesScrollRef.current;1977- if (!scrollContainer) return;1978- scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/web/src/components/chat/MessagesTimeline.tsx lines 811 to 812OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -776,0 +792,21 @@792+ const prevById = useRef(new Map<string, MessagesTimelineRow>());793+794+ return useMemo(() => {795+ const prev = prevById.current;796+ const next = new Map<string, MessagesTimelineRow>();797+ let anyChanged = false;798+799+ const result = rows.map((row) => {800+ const prevRow = prev.get(row.id);801+ if (prevRow && isRowUnchanged(prevRow, row)) {802+ next.set(row.id, prevRow);803+ return prevRow;804+ }805+ next.set(row.id, row);806+ anyChanged = true;807+ return row;808+ });809+810+ prevById.current = next;811+ // If nothing changed and length matches, reuse the previous array reference812+ return anyChanged || rows.length !== prev.size ? result : Array.from(prev.values());
- apps/web/src/components/chat/MessagesTimeline.tsx line 812OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -776,0 +792,21 @@792+ const prevById = useRef(new Map<string, MessagesTimelineRow>());793+794+ return useMemo(() => {795+ const prev = prevById.current;796+ const next = new Map<string, MessagesTimelineRow>();797+ let anyChanged = false;798+799+ const result = rows.map((row) => {800+ const prevRow = prev.get(row.id);801+ if (prevRow && isRowUnchanged(prevRow, row)) {802+ next.set(row.id, prevRow);803+ return prevRow;804+ }805+ next.set(row.id, row);806+ anyChanged = true;807+ return row;808+ });809+810+ prevById.current = next;811+ // If nothing changed and length matches, reuse the previous array reference812+ return anyChanged || rows.length !== prev.size ? result : Array.from(prev.values());
- apps/web/src/components/chat/MessagesTimeline.tsx line 772Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -776,0 +787,21 @@787+ const prevById = useRef(new Map<string, MessagesTimelineRow>());788+789+ return useMemo(() => {790+ const prev = prevById.current;791+ const next = new Map<string, MessagesTimelineRow>();792+ let anyChanged = false;793+794+ const result = rows.map((row) => {795+ const prevRow = prev.get(row.id);796+ if (prevRow && isRowUnchanged(prevRow, row)) {797+ next.set(row.id, prevRow);798+ return prevRow;799+ }800+ next.set(row.id, row);801+ anyChanged = true;802+ return row;803+ });804+805+ prevById.current = next;806+ return result;807+ }, [rows]);
- apps/web/src/components/chat/MessagesTimeline.tsx line 548Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -626,6 +554,17 @@554+ </p>555+ {hasOverflow && (556+ <button557+ type="button"558+ className="text-[9px] uppercase tracking-[0.12em] text-muted-foreground/55 transition-colors duration-150 hover:text-foreground/75"559+ onClick={() => setIsExpanded((v) => !v)}560+ >561+ {isExpanded ? "Show less" : `Show ${hiddenCount} more`}562+ </button>563+ )}626564 </div>627565 )}628-629- {nonVirtualizedRows.map((row) => (630- <div key={`non-virtual-row:${row.id}`}>{renderRowContent(row)}</div>631- ))}566+ <div className="space-y-0.5">567+ {visibleEntries.map((workEntry) => (568+ <SimpleWorkEntryRow key={`work-row:${workEntry.id}`} workEntry={workEntry} />569+ ))}570+ </div>
- apps/web/src/components/chat/MessagesTimeline.tsx line 188OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -189,7 +173,16 @@173+ if (state) {174+ onIsAtEndChange(state.isAtEnd);189175 }176+ }, [listRef, onIsAtEndChange]);190177191- if (firstCurrentTurnRowIndex < 0) {192- firstCurrentTurnRowIndex = rows.findIndex(193- (row) => row.kind === "message" && row.message.streaming,194- );195- }178+ // Volatile Maps go into a ref so they never bust the context identity.179+ // Components read them via getMaps() during their own render pass.180+ const mapsRef = useRef<TimelineRowMaps>({181+ turnDiffSummaryByAssistantMessageId,182+ revertTurnCountByUserMessageId,183+ });184+ mapsRef.current = {185+ turnDiffSummaryByAssistantMessageId,186+ revertTurnCountByUserMessageId,187+ };188+ const getMaps = useCallback(() => mapsRef.current, []);
- @@ -2090,7 +1941,14 @@2090- }, []);2091- const onMessagesTouchMove = useCallback((event: React.TouchEvent<HTMLDivElement>) => {2092- const touch = event.touches[0];2093- if (!touch) return;2094- const previousTouchY = lastTouchClientYRef.current;2095- if (previousTouchY !== null && touch.clientY > previousTouchY + 1) {2096- pendingUserScrollUpIntentRef.current = true;1941+ // Debounce *showing* the scroll-to-bottom pill so it doesn't flash during1942+ // thread switches. LegendList fires scroll events with isAtEnd=false while1943+ // initialScrollAtEnd is settling; hiding is always immediate.1944+ const showScrollDebouncer = useRef(1945+ new Debouncer(() => setShowScrollToBottom(true), { wait: 150 }),1946+ );1947+ const onIsAtEndChange = useCallback((isAtEnd: boolean) => {1948+ if (isAtEndRef.current === isAtEnd) return;1949+ isAtEndRef.current = isAtEnd;1950+ if (isAtEnd) {1951+ showScrollDebouncer.current.cancel();1952+ setShowScrollToBottom(false);1953+ } else {1954+ showScrollDebouncer.current.maybeExecute();
- apps/web/src/components/chat/MessagesTimeline.tsx line 772Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -776,0 +765,21 @@765+ const prev = prevById.current;766+ const next = new Map<string, MessagesTimelineRow>();767+ let anyChanged = rows.length !== prev.size;768+769+ const result = rows.map((row) => {770+ const prevRow = prev.get(row.id);771+ if (prevRow && isRowUnchanged(prevRow, row)) {772+ next.set(row.id, prevRow);773+ return prevRow;774+ }775+ next.set(row.id, row);776+ anyChanged = true;777+ return row;778+ });779+780+ prevById.current = next;781+782+ if (!anyChanged) return prevResult.current;783+ prevResult.current = result;784+ return result;785+ }, [rows]);
juliusmarminge merged this96c9306
juliusmarminge closed this
Sign in to join this conversation.



Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
45911304-63bb-4dc3-a4d2-198382d9c725You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands and usage tips.