Stabilize chat scroll anchoring after send #3564
Mergedjuliusmarmingemerged 6 commits into
main fromrefs/pull/3564/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -289,10 +266,13 @@289266 ],290267 );291268 const rows = useStableRows(rawRows);292- const anchoredEndSpace = useMemo(293- () =>294- resolveChatListAnchoredEndSpace(rows, anchorMessageId, (row) =>295- row.kind === "message" ? row.message.id : null,296- ),297- [anchorMessageId, rows],298- );269+ const handleAnchorReady = useCallback(() => {270+ if (anchorMessageId !== null) {271+ onAnchorReady(anchorMessageId);272+ }273+ }, [anchorMessageId, onAnchorReady]);274+ const anchoredEndSpace = useMemo(() => {275+ const config = resolveChatListAnchoredEndSpace(rows, anchorMessageId, (row) =>276+ row.kind === "message" ? row.message.id : null,277+ );278+ return config ? { ...config, onReady: handleAnchorReady } : undefined;
- apps/web/src/components/ChatView.tsx line 3160OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3146,7 +3146,15 @@31463146 ],31473147 );314831483149- // Scroll helpers — LegendList handles auto-scroll via maintainScrollAtEnd.3149+ // Scrolling is explicit so streamed timeline updates never take control away3150+ // from the user after the newly sent row has been positioned once.31503151 const scrollToEnd = useCallback((animated = false) => {31513152 void legendListRef.current?.scrollToEnd?.({ animated });31523153 }, []);3154+ const positionedTimelineAnchorRef = useRef<MessageId | null>(null);3155+ const onTimelineAnchorReady = useCallback((messageId: MessageId) => {3156+ if (positionedTimelineAnchorRef.current === messageId) {3157+ return;3158+ }3159+ positionedTimelineAnchorRef.current = messageId;3160+ void legendListRef.current?.scrollToEnd?.({ animated: false });
- apps/web/src/components/chat/MessagesTimeline.tsx line 363OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -376,6 +360,4 @@376360 initialScrollAtEnd377361 {...(anchoredEndSpace ? { anchoredEndSpace } : {})}378362 contentInsetEndAdjustment={contentInsetEndAdjustment}379- maintainScrollAtEnd={!foldToggleSettling}380- maintainScrollAtEndThreshold={0.1}381- maintainVisibleContentPosition363+ maintainVisibleContentPosition={false}
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -3152,0 +3197,21 @@3197+ animated: true,3198+ viewPosition: 0,3199+ viewOffset: CHAT_LIST_ANCHOR_OFFSET,3200+ });3201+ });3202+ });3203+ }, []);3204+ const onTimelineAnchorSizeChanged = useCallback((messageId: MessageId) => {3205+ if (settledTimelineAnchorRef.current !== messageId) {3206+ return;3207+ }3208+ const scrollOffset = legendListRef.current?.getState().scroll;3209+ if (scrollOffset === undefined) {3210+ return;3211+ }3212+ if (pendingAnchorScrollRestoreRef.current === null) {3213+ pendingAnchorScrollRestoreRef.current = { messageId, offset: scrollOffset };3214+ }3215+ if (anchorScrollRestoreFrameRef.current !== null) {3216+ return;3217+ }@@ -3152,0 +3204,21 @@3204+ const onTimelineAnchorSizeChanged = useCallback((messageId: MessageId) => {3205+ if (settledTimelineAnchorRef.current !== messageId) {3206+ return;3207+ }3208+ const scrollOffset = legendListRef.current?.getState().scroll;3209+ if (scrollOffset === undefined) {3210+ return;3211+ }3212+ if (pendingAnchorScrollRestoreRef.current === null) {3213+ pendingAnchorScrollRestoreRef.current = { messageId, offset: scrollOffset };3214+ }3215+ if (anchorScrollRestoreFrameRef.current !== null) {3216+ return;3217+ }3218+ anchorScrollRestoreFrameRef.current = requestAnimationFrame(() => {3219+ anchorScrollRestoreFrameRef.current = null;3220+ const pending = pendingAnchorScrollRestoreRef.current;3221+ pendingAnchorScrollRestoreRef.current = null;3222+ if (pending && settledTimelineAnchorRef.current === pending.messageId) {3223+ void legendListRef.current?.scrollToOffset({ offset: pending.offset, animated: false });3224+ }
- apps/web/src/components/chat/MessagesTimeline.tsx line 375OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -376,6 +372,4 @@376372 initialScrollAtEnd377373 {...(anchoredEndSpace ? { anchoredEndSpace } : {})}378374 contentInsetEndAdjustment={contentInsetEndAdjustment}379- maintainScrollAtEnd={!foldToggleSettling}380- maintainScrollAtEndThreshold={0.1}381- maintainVisibleContentPosition375+ maintainVisibleContentPosition={false}
- @@ -364,0 +380,21 @@380+ nextRows.push({381+ kind: "work",382+ id: timelineEntry.id,383+ createdAt: timelineEntry.createdAt,384+ groupedEntries: visibleGroupedEntries,385+ });386+ } else {387+ const groupId = `work-group:${timelineEntry.id}`;388+ const expanded = input.expandedWorkGroupIds?.has(groupId) ?? false;389+ const hiddenEntries = visibleGroupedEntries.slice(0, -MAX_VISIBLE_WORK_LOG_ENTRIES);390+ const visibleEntries = visibleGroupedEntries.slice(-MAX_VISIBLE_WORK_LOG_ENTRIES);391+ const renderedEntries = expanded ? [...hiddenEntries, ...visibleEntries] : visibleEntries;392+393+ for (const workEntry of renderedEntries) {394+ nextRows.push({395+ kind: "work",396+ id: workEntry.id,397+ createdAt: workEntry.createdAt,398+ groupedEntries: [workEntry],399+ });400+ }
- @@ -364,0 +380,21 @@380+ nextRows.push({381+ kind: "work",382+ id: timelineEntry.id,383+ createdAt: timelineEntry.createdAt,384+ groupedEntries: visibleGroupedEntries,385+ });386+ } else {387+ const groupId = `work-group:${timelineEntry.id}`;388+ const expanded = input.expandedWorkGroupIds?.has(groupId) ?? false;389+ const hiddenEntries = visibleGroupedEntries.slice(0, -MAX_VISIBLE_WORK_LOG_ENTRIES);390+ const visibleEntries = visibleGroupedEntries.slice(-MAX_VISIBLE_WORK_LOG_ENTRIES);391+ const renderedEntries = expanded ? [...hiddenEntries, ...visibleEntries] : visibleEntries;392+393+ for (const workEntry of renderedEntries) {394+ nextRows.push({395+ kind: "work",396+ id: workEntry.id,397+ createdAt: workEntry.createdAt,398+ groupedEntries: [workEntry],399+ });400+ }@@ -834,0 +810,21 @@810+ event.currentTarget.closest<HTMLElement>("[data-timeline-row-id]") ?? event.currentTarget;811+ ctx.onToggleWorkGroup(row.groupId, anchorElement);812+ }}813+ >814+ <span className="flex size-5 shrink-0 items-center justify-center text-muted-foreground/65">815+ <ChevronDownIcon816+ className={cn(817+ "size-3.5 shrink-0 opacity-70 transition-transform duration-200",818+ row.expanded && "rotate-180",819+ )}820+ />821+ </span>822+ {row.expanded ? (823+ <span className="font-medium text-foreground/82">824+ Show fewer {row.onlyToolEntries ? "tool calls" : "log entries"}825+ </span>826+ ) : (827+ <span className="font-medium text-foreground/82">828+ +{row.hiddenCount} previous {labelNoun}829+ {row.hiddenCount === 1 ? "" : "s"}830+ </span>@@ -233,7 +234,15 @@233- return () => {234- window.cancelAnimationFrame(firstFrameId);235- if (secondFrameId !== null) {236- window.cancelAnimationFrame(secondFrameId);234+235+ if (anchorBottomBeforeToggle === null || !anchorElement) {236+ return;237237 }238- };239- }, [foldToggleSettling]);238+239+ const delta = anchorElement.getBoundingClientRect().bottom - anchorBottomBeforeToggle;240+ if (Math.abs(delta) < 0.5) {241+ return;242+ }243+244+ const list = listRef.current;245+ const currentScroll = list?.getState?.().scroll;246+ if (list && typeof currentScroll === "number") {247+ list.scrollToOffset({ offset: currentScroll + delta, animated: false });248+ }
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/mobile/src/features/threads/ThreadFeed.tsx line 1334Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1331,4 +1353,3 @@13311353 renderFeedEntry(info, {13321354 environmentId: props.environmentId,13331355 copiedRowId,1334- expandedWorkGroups,
- @@ -1182,12 +1190,20 @@11821190 }),11831191 [11841192 copiedRowId,1185- expandedWorkGroups,11861193 expandedWorkRows,11871194 iconSubtleColor,11881195 markdownStyles,11891196 reviewCommentColors,11901197 userBubbleColor,11911198 ],11921199 );1200+ const expandedWorkGroupIds = useMemo(() => {1201+ const ids = new Set<string>();1202+ for (const [groupId, expanded] of Object.entries(expandedWorkGroups)) {1203+ if (expanded) {1204+ ids.add(groupId);1205+ }1206+ }1207+ return ids;1208+ }, [expandedWorkGroups]);11931209 const presentedFeed = useMemo(
- @@ -1288,10 +1340,12 @@1340+ expandedWorkGroups: {1341+ ...current.expandedWorkGroups,1342+ [groupId]: !(current.expandedWorkGroups[groupId] ?? false),1343+ },1344+ }));1345+ },1346+ [suspendEndScrollMaintenanceForDisclosure],1347+ );128813481289- const onToggleWorkRow = useCallback((rowId: string) => {1290- setInteractionState((current) => ({1291- ...current,1292- expandedWorkRows: {1293- ...current.expandedWorkRows,1294- [rowId]: !(current.expandedWorkRows[rowId] ?? false),1295- },1296- }));1297- }, []);1349+ const onToggleWorkRow = useCallback(1350+ (rowId: string) => {1351+ suspendEndScrollMaintenanceForDisclosure(rowId);
- @@ -1261,0 +1287,21 @@1287+ setDisclosureToggleSettling(true);1288+ if (foldSettleFrameRef.current !== null) {1289+ cancelAnimationFrame(foldSettleFrameRef.current);1290+ }1291+ if (foldSettleSecondFrameRef.current !== null) {1292+ cancelAnimationFrame(foldSettleSecondFrameRef.current);1293+ }1294+ foldSettleFrameRef.current = requestAnimationFrame(() => {1295+ foldSettleSecondFrameRef.current = requestAnimationFrame(() => {1296+ disclosureAnchorKeyRef.current = null;1297+ setDisclosureToggleSettling(false);1298+ foldSettleFrameRef.current = null;1299+ foldSettleSecondFrameRef.current = null;1300+ });1301+ });1302+ }, []);1303+1304+ const shouldRestoreVisibleContentPosition = useCallback((entry: ThreadFeedEntry) => {1305+ const disclosureAnchorKey = disclosureAnchorKeyRef.current;1306+ return disclosureAnchorKey === null || entry.id === disclosureAnchorKey;1307+ }, []);
juliusmarminge merged this24abab7
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:
5fab2c50-e611-4399-b3d9-546a58d0008aYou 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.