Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(issues): keep Jump to latest up while the timeline tail is unrendered (MUL-9303) (#10648)

Jiayuan Zhang4949978c1be5
diff --git a/packages/views/chat/components/jump-to-live-end.test.tsx b/packages/views/chat/components/jump-to-live-end.test.tsx
index 223c367296..696015d1bf 100644
--- a/packages/views/chat/components/jump-to-live-end.test.tsx
+++ b/packages/views/chat/components/jump-to-live-end.test.tsx
@@ -99,6 +99,18 @@ describe("useJumpToLiveEnd", () => {
expect(screen.getByRole("button", { name: "latest" })).toBeTruthy();
});
+ it("once shown, stays until the reader is within half a screen of the live end", () => {
+ render(<Harness metrics={{ scrollHeight: 3000, clientHeight: 500 }} pin={toBottom} />);
+ const scroller = screen.getByTestId("scroller");
+ scrollTo(scroller, 1000);
+
+ scrollTo(scroller, 2100); // 400px up
+ expect(screen.getByRole("button", { name: "latest" })).toBeTruthy();
+
+ scrollTo(scroller, 2300); // 200px up
+ expect(screen.queryByRole("button")).toBeNull();
+ });
+
it("flags a reply that lands while the reader is away, and drops it back at the end", () => {
const metrics = { scrollHeight: 3000, clientHeight: 500 };
const { rerender } = render(<Harness metrics={metrics} pin={toBottom} tailKey="t1" />);
diff --git a/packages/views/chat/components/jump-to-live-end.tsx b/packages/views/chat/components/jump-to-live-end.tsx
index f5f7103769..beb92bd7f1 100644
--- a/packages/views/chat/components/jump-to-live-end.tsx
+++ b/packages/views/chat/components/jump-to-live-end.tsx
@@ -10,14 +10,19 @@ import { useT } from "../../i18n";
// "Jump to latest" for the chat list (MUL-8951).
//
-// The button shows once the reader is more than a screen above the live end.
-// A jump only has to reach the live end: arriving within its edge zone
+// The button shows once the reader is more than a screen above the live end,
+// and stays until they are back within half a screen of it, so it does not
+// flicker at the threshold. A jump only has to reach the live end: arriving
+// within its edge zone
// re-engages the stick-to-bottom follow, which keeps the list pinned from
// there as the reply streams on.
/** Most frames a jump keeps re-aiming at the last row while Virtuoso measures it. */
export const JUMP_MAX_FRAMES = 30;
+/** How close to the live end, in screens, hides the button once shown. */
+const RETURN_WITHIN_SCREENS = 0.5;
+
export interface JumpToLiveEnd {
visible: boolean;
/** The agent replied while the reader was away from the live end. */
@@ -53,9 +58,12 @@ export function useJumpToLiveEnd(
return;
}
let frame = 0;
+ let shown = false;
const measure = () => {
frame = 0;
- setVisible(distanceFromBottom(scrollEl) > scrollEl.clientHeight);
+ const threshold = shown ? scrollEl.clientHeight * RETURN_WITHIN_SCREENS : scrollEl.clientHeight;
+ shown = distanceFromBottom(scrollEl) > threshold;
+ setVisible(shown);
};
const schedule = () => {
if (frame === 0) frame = requestAnimationFrame(measure);
diff --git a/packages/views/issues/components/issue-detail.tsx b/packages/views/issues/components/issue-detail.tsx
index d46caad1ce..14cfabdbbd 100644
--- a/packages/views/issues/components/issue-detail.tsx
+++ b/packages/views/issues/components/issue-detail.tsx
@@ -12,7 +12,7 @@ import { useStatusLabel } from "../utils/status-label";
import { priorityLabel } from "../utils/priority-label";
import { useIssueStatuses } from "@multica/core/issue-statuses/hooks";
import { useState, useEffect, useCallback, useMemo, useRef, useId, Fragment, type ReactNode } from "react";
-import { Virtuoso, type VirtuosoHandle } from "react-virtuoso";
+import { Virtuoso, type ListRange, type VirtuosoHandle } from "react-virtuoso";
import { useDefaultLayout, usePanelRef } from "react-resizable-panels";
import { AppLink, useBackOrReplace } from "../../navigation";
import { IssueDuplicateBanner, IssueDuplicatesSection, isDuplicateIssue } from "./issue-duplicates";
@@ -1632,10 +1632,6 @@ export function IssueDetail({ issueId, onDelete, onDone, defaultSidebarOpen = tr
const stickyComposer = useStickyComposer();
// Read by the image rules in editor/styles/attachment.css.
const commentImageLayout = useCommentComposerStore((s) => s.imageLayout);
- // Mobile shows it only near the top of the page, where it covers the text
- // column the least; desktop shows it whenever the latest entry is more than
- // a screen away.
- const jumpToLatest = useJumpToLatest(scrollContainerEl, { nearTopOnly: isMobile });
// Per-session: which resolved threads the user has temporarily expanded.
// Not persisted (matches Linear) — reload collapses everything back to bars.
@@ -1813,12 +1809,6 @@ export function IssueDetail({ issueId, onDelete, onDone, defaultSidebarOpen = tr
submitComment, submitReply,
editComment, deleteComment, toggleResolveComment, toggleReaction: handleToggleReaction,
} = useIssueTimeline(id, user?.id, activeDeliverableJump?.commentId);
- const newComments = useNewCommentsWhileAway(timeline, {
- ready: !timelineLoading,
- away: jumpToLatest.visible,
- userId: user?.id,
- resetKey: id,
- });
const { data: commentTasks, isLoading: commentTasksLoading } = useQuery(issueTasksOptions(id));
const enteringRunIds = useNewRunIds(id, commentTasks);
@@ -2067,6 +2057,32 @@ export function IssueDetail({ issueId, onDelete, onDone, defaultSidebarOpen = tr
const handleTimelineHeightChange = useCallback((height: number) => {
if (height > 0) setMeasuredTimelineId(id);
}, [id]);
+ // Whether Virtuoso's rendered rows (overscan included) stop short of the
+ // last one; rows past them are only estimated. Settled from Virtuoso's own
+ // range report, not `items.length`: a comment appended at the bottom is
+ // unrendered for a frame, and that frame must not read as "away".
+ const timelineItemCountRef = useRef(items.length);
+ timelineItemCountRef.current = items.length;
+ const [timelineTail, setTimelineTail] = useState({ issueId: id, pending: false });
+ const handleTimelineRangeChanged = useCallback((range: ListRange) => {
+ const pending = range.endIndex < timelineItemCountRef.current - 1;
+ setTimelineTail((prev) => (prev.issueId === id && prev.pending === pending ? prev : { issueId: id, pending }));
+ }, [id]);
+ // Mobile shows it only near the top of the page, where it covers the text
+ // column the least; desktop shows it whenever the latest entry is more than
+ // a screen away.
+ const jumpToLatest = useJumpToLatest(scrollContainerEl, {
+ nearTopOnly: isMobile,
+ tailPending: !isFlatTimeline && timelineTail.issueId === id && timelineTail.pending,
+ });
+ // Keyed to `away`, not `visible`: mobile hides the button past the top of
+ // the page, but the reader has not reached the new comments there.
+ const newComments = useNewCommentsWhileAway(timeline, {
+ ready: !timelineLoading,
+ away: jumpToLatest.away,
+ userId: user?.id,
+ resetKey: id,
+ });
const pendingHighlight = !!highlightCommentId && consumedHighlightId !== highlightCommentId;
const { pending: readingAnchor, complete: completeAnchorRestore } = useIssueTimelineAnchor({
containerKey: scrollContainerKey,
@@ -4199,6 +4215,7 @@ export function IssueDetail({ issueId, onDelete, onDone, defaultSidebarOpen = tr
initialScrollTop={readingAnchor ? undefined : restoredScrollTop}
increaseViewportBy={{ top: 800, bottom: 800 }}
totalListHeightChanged={handleTimelineHeightChange}
+ rangeChanged={handleTimelineRangeChanged}
computeItemKey={(_i, item) => `${item.kind}:${item.kind === "run" ? item.run.task.id : item.id}`}
// Also load-bearing for long threads: keepThreadRowAnchored
// corrects after Virtuoso's resize handling in the same frame.
diff --git a/packages/views/issues/hooks/use-jump-to-latest.test.tsx b/packages/views/issues/hooks/use-jump-to-latest.test.tsx
index 752160c518..c057b72f73 100644
--- a/packages/views/issues/hooks/use-jump-to-latest.test.tsx
+++ b/packages/views/issues/hooks/use-jump-to-latest.test.tsx
@@ -34,15 +34,18 @@ function defineMetrics(el: HTMLElement, metrics: { scrollHeight: number; clientH
function Harness({
metrics,
nearTopOnly = false,
+ tailPending = false,
}: {
metrics: { scrollHeight: number; clientHeight: number };
nearTopOnly?: boolean;
+ tailPending?: boolean;
}) {
const [el, setEl] = useState<HTMLDivElement | null>(null);
- const { visible, jump } = useJumpToLatest(el, { nearTopOnly });
+ const { visible, away, jump } = useJumpToLatest(el, { nearTopOnly, tailPending });
return (
<div
data-testid="scroller"
+ data-away={away}
ref={(node) => {
if (node && node !== el) {
defineMetrics(node, metrics);
@@ -55,6 +58,14 @@ function Harness({
);
}
+function scrollTo(scroller: HTMLElement, top: number) {
+ scroller.scrollTop = top;
+ fireEvent.scroll(scroller);
+ flushFrame();
+}
+
+const latestButton = () => screen.queryByRole("button", { name: "latest" });
+
beforeEach(() => {
rafCallbacks = [];
now = 0;
@@ -75,12 +86,56 @@ describe("useJumpToLatest", () => {
it("shows only when the bottom is more than one viewport away", () => {
render(<Harness metrics={{ scrollHeight: 2000, clientHeight: 500 }} />);
const scroller = screen.getByTestId("scroller");
- expect(screen.getByRole("button", { name: "latest" })).toBeTruthy();
+ expect(latestButton()).toBeTruthy();
- scroller.scrollTop = 1100; // 400px from the bottom
- fireEvent.scroll(scroller);
- flushFrame();
- expect(screen.queryByRole("button", { name: "latest" })).toBeNull();
+ scrollTo(scroller, 1300); // 200px from the bottom
+ expect(latestButton()).toBeNull();
+
+ scrollTo(scroller, 1100); // 400px from the bottom: not yet a screen away
+ expect(latestButton()).toBeNull();
+
+ scrollTo(scroller, 900); // 600px from the bottom
+ expect(latestButton()).toBeTruthy();
+ });
+
+ it("once shown, stays until the reader is within half a screen of the bottom", () => {
+ render(<Harness metrics={{ scrollHeight: 2000, clientHeight: 500 }} />);
+ const scroller = screen.getByTestId("scroller");
+
+ scrollTo(scroller, 1100); // 400px from the bottom
+ expect(latestButton()).toBeTruthy();
+
+ scrollTo(scroller, 1260); // 240px from the bottom
+ expect(latestButton()).toBeNull();
+ });
+
+ it("stays up while the timeline's last row is unrendered, however close the estimate says the bottom is", () => {
+ const metrics = { scrollHeight: 1600, clientHeight: 500 };
+ const { rerender } = render(<Harness metrics={metrics} tailPending />);
+ const scroller = screen.getByTestId("scroller");
+
+ scrollTo(scroller, 1000); // estimated 100px from the bottom
+ expect(latestButton()).toBeTruthy();
+
+ // The tail renders and measures taller than estimated: 1600px are left.
+ metrics.scrollHeight = 3100;
+ rerender(<Harness metrics={metrics} />);
+ expect(latestButton()).toBeTruthy();
+
+ scrollTo(scroller, 2500);
+ expect(latestButton()).toBeNull();
+ });
+
+ it("near-top-only: stays away while the button hides past the top fifth", () => {
+ render(<Harness metrics={{ scrollHeight: 4000, clientHeight: 500 }} nearTopOnly />);
+ const scroller = screen.getByTestId("scroller");
+
+ scrollTo(scroller, 2000);
+ expect(latestButton()).toBeNull();
+ expect(scroller.dataset.away).toBe("true");
+
+ scrollTo(scroller, 3400); // 100px from the bottom
+ expect(scroller.dataset.away).toBe("false");
});
it("stays up anywhere more than a screen from the bottom", () => {
diff --git a/packages/views/issues/hooks/use-jump-to-latest.ts b/packages/views/issues/hooks/use-jump-to-latest.ts
index 1ec680b9cd..ab941c1ce4 100644
--- a/packages/views/issues/hooks/use-jump-to-latest.ts
+++ b/packages/views/issues/hooks/use-jump-to-latest.ts
@@ -1,11 +1,22 @@
import { useCallback, useEffect, useRef, useState } from "react";
/**
- * Drives the issue timeline's "Jump to latest" affordance (MUL-8324, MUL-8951).
+ * Drives the issue timeline's "Jump to latest" affordance (MUL-8324, MUL-8951,
+ * MUL-9303).
*
- * `visible` is on while the newest entry is more than one screen away. With
- * `nearTopOnly` (mobile) the reader must also still be near the top of the
- * page; see `shouldShow`.
+ * `away` is on while the newest entry is more than one screen away, and stays
+ * on until the reader is within half a screen of the bottom, so the button
+ * does not flicker when the reader rests near the threshold or late content
+ * nudges the page. `visible` is `away`, plus — with `nearTopOnly` (mobile) —
+ * the reader still being near the top of the page; see `isNearTop`.
+ *
+ * `tailPending` says the timeline's last row has not been rendered yet. The
+ * virtualized timeline estimates every row it has not rendered at the height
+ * of the first row it measured, usually a one-line activity row, so
+ * `scrollHeight` can claim the bottom is close while screens of comments are
+ * still below. With the tail unrendered, at least the list's bottom overscan
+ * and the composer are below the viewport, which counts as away; once the
+ * tail renders, `scrollHeight` is exact below the viewport again.
*
* `jump` drives scrollTop to the bottom directly instead of asking Virtuoso
* for the last index: with `customScrollParent`, writing scrollTop is what
@@ -18,20 +29,25 @@ export const JUMP_FOLLOW_MS = 2000;
export function useJumpToLatest(
scrollContainerEl: HTMLElement | null,
- { nearTopOnly = false }: { nearTopOnly?: boolean } = {},
+ { nearTopOnly = false, tailPending = false }: { nearTopOnly?: boolean; tailPending?: boolean } = {},
) {
- const [visible, setVisible] = useState(false);
+ const [state, setState] = useState(HIDDEN);
+ const awayRef = useRef(false);
const cancelJumpRef = useRef<(() => void) | null>(null);
useEffect(() => {
if (!scrollContainerEl) {
- setVisible(false);
+ awayRef.current = false;
+ setState(HIDDEN);
return;
}
let frame = 0;
const measure = () => {
frame = 0;
- setVisible(shouldShow(scrollContainerEl, nearTopOnly));
+ const away = tailPending || isAway(scrollContainerEl, awayRef.current);
+ awayRef.current = away;
+ const visible = away && (!nearTopOnly || isNearTop(scrollContainerEl));
+ setState((prev) => (prev.away === away && prev.visible === visible ? prev : { away, visible }));
};
const schedule = () => {
if (frame === 0) frame = requestAnimationFrame(measure);
@@ -48,7 +64,7 @@ export function useJumpToLatest(
observer?.disconnect();
if (frame !== 0) cancelAnimationFrame(frame);
};
- }, [scrollContainerEl, nearTopOnly]);
+ }, [scrollContainerEl, nearTopOnly, tailPending]);
useEffect(() => () => cancelJumpRef.current?.(), []);
@@ -98,20 +114,31 @@ export function useJumpToLatest(
frame = requestAnimationFrame(tick);
}, [scrollContainerEl]);
- return { visible, jump };
+ return { visible: state.visible, away: state.away, jump };
}
+const HIDDEN = { away: false, visible: false };
+
/**
- * Show while more than one screen is left below; short pages never show it.
- *
- * `nearTopOnly` also requires the viewport's top edge to be within the first
- * `SHOW_WITHIN_TOP_RATIO` of the page: on mobile the button covers the text
- * column, so once the reader has scrolled further on their own they are
- * reading, not hunting for the latest entry, and it gets out of the way.
+ * Away once more than a screen is left below; short pages never are. Once
+ * away, the reader has to come within `RETURN_WITHIN_SCREENS` of the bottom.
+ */
+function isAway(el: HTMLElement, wasAway: boolean) {
+ const threshold = wasAway ? el.clientHeight * RETURN_WITHIN_SCREENS : el.clientHeight;
+ return distanceFromBottom(el) > threshold;
+}
+
+/** How close to the bottom, in screens, counts as back at the latest entry. */
+const RETURN_WITHIN_SCREENS = 0.5;
+
+/**
+ * The viewport's top edge is within the first `SHOW_WITHIN_TOP_RATIO` of the
+ * page: on mobile the button covers the text column, so once the reader has
+ * scrolled further on their own they are reading, not hunting for the latest
+ * entry, and it gets out of the way.
*/
-function shouldShow(el: HTMLElement, nearTopOnly: boolean) {
- if (distanceFromBottom(el) <= el.clientHeight) return false;
- return !nearTopOnly || el.scrollTop <= el.scrollHeight * SHOW_WITHIN_TOP_RATIO;
+function isNearTop(el: HTMLElement) {
+ return el.scrollTop <= el.scrollHeight * SHOW_WITHIN_TOP_RATIO;
}
/** Share of the page the reader can scroll through before the button hides. */