Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ vi.mock("@/components/ui/bottom-anchored-scroll-body.js", () => ({
isAtBottom: false,
scrollToBottom: mocks.scrollToBottom,
scrollElementIntoView: vi.fn(),
scrollElementIntoViewClampedToMaxScroll: vi.fn(),
positionKeeper: createInertScrollPositionKeeper(),
holdContentPosition: vi.fn(),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ vi.mock("@/components/ui/bottom-anchored-scroll-body.js", () => ({
isAtBottom: true,
scrollToBottom: () => {},
scrollElementIntoView: () => {},
scrollElementIntoViewClampedToMaxScroll: () => {},
positionKeeper: createInertScrollPositionKeeper(),
holdContentPosition: () => {},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ describe("ThreadPendingInteractionBanner collapsed strip", () => {
isAtBottom: true,
scrollToBottom: () => {},
scrollElementIntoView: () => {},
scrollElementIntoViewClampedToMaxScroll: () => {},
positionKeeper: createInertScrollPositionKeeper(),
holdContentPosition: () => {},
}}
Expand Down
143 changes: 93 additions & 50 deletions apps/app/src/components/thread/timeline/ThreadTimelineRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import {
TimelineScrollRestoreContext,
useBottomAnchoredScroll,
} from "@/components/ui/bottom-anchored-scroll-body.js";
import type { ScrollPositionKeeper } from "@/components/ui/scroll-position-keeper.js";
import {
collectSearchedMessageAncestorRowIds,
SearchMessageLocationProvider,
Expand Down Expand Up @@ -242,10 +243,6 @@ interface TimelineRowsListProps {
unreadDividerPlacement: ThreadTimelineUnreadDividerPlacement | null;
}

interface TimelineUnreadDividerProps {
autoScroll: boolean;
}

interface TimelineRowViewProps {
activeLatestBundleId: string | null;
compactActivityIntents: boolean;
Expand Down Expand Up @@ -288,7 +285,15 @@ interface BuildTimelineRowsListItemsArgs {
unreadDividerPlacement: ThreadTimelineUnreadDividerPlacement | null;
}

interface FindUnreadDividerIndexArgs {
interface UseLandOnUnreadDividerArgs {
items: readonly TimelineRowsListItem[];
positionKeeper: ScrollPositionKeeper | null;
unreadDividerAutoScroll: boolean;
unreadDividerPlacement: ThreadTimelineUnreadDividerPlacement | null;
}

interface UseLoadedUnreadDividerPlacementArgs {
hasOlderRows: boolean;
rows: readonly ThreadTimelineViewRow[];
unreadDividerPlacement: ThreadTimelineUnreadDividerPlacement | null;
}
Expand Down Expand Up @@ -1045,34 +1050,9 @@ const ConversationRowContent = memo(function ConversationRowContent({
);
});

function TimelineUnreadDivider({ autoScroll }: TimelineUnreadDividerProps) {
const bottomAnchor = useBottomAnchoredScroll();
const dividerRef = useRef<HTMLDivElement>(null);
const hasScrolledRef = useRef(false);

useLayoutEffect(() => {
if (!autoScroll || !bottomAnchor || hasScrolledRef.current) {
return;
}

const frameId = window.requestAnimationFrame(() => {
const divider = dividerRef.current;
if (!divider) {
return;
}

hasScrolledRef.current = true;
bottomAnchor.scrollElementIntoViewClampedToMaxScroll({
element: divider,
});
});

return () => window.cancelAnimationFrame(frameId);
}, [autoScroll, bottomAnchor]);

function TimelineUnreadDivider() {
return (
<div
ref={dividerRef}
role="separator"
aria-label="New messages"
className={cn(
Expand Down Expand Up @@ -1901,27 +1881,76 @@ const MemoizedTimelineExpandableRowView = memo(
areTimelineExpandableRowViewPropsEqual,
);

export function useLoadedUnreadDividerPlacement({
hasOlderRows,
rows,
unreadDividerPlacement,
}: UseLoadedUnreadDividerPlacementArgs): ThreadTimelineUnreadDividerPlacement | null {
const [unplaceablePlacement, setUnplaceablePlacement] =
useState<ThreadTimelineUnreadDividerPlacement | null>(null);
const firstRow = rows[0];
const isUnplaceable =
unreadDividerPlacement !== null &&
(unreadDividerPlacement === unplaceablePlacement ||
(hasOlderRows &&
firstRow !== undefined &&
firstRow.startedAt > unreadDividerPlacement.cutoffAt));
if (isUnplaceable && unreadDividerPlacement !== unplaceablePlacement) {
setUnplaceablePlacement(unreadDividerPlacement);
}
return isUnplaceable ? null : unreadDividerPlacement;
}

function useLandOnUnreadDivider({
items,
positionKeeper,
unreadDividerAutoScroll,
unreadDividerPlacement,
}: UseLandOnUnreadDividerArgs): void {
const landedPlacementRef =
useRef<ThreadTimelineUnreadDividerPlacement | null>(null);
const divider = items.find((item) => item.kind === "unread-divider");
const dividerId = divider?.id ?? null;

useLayoutEffect(() => {
if (
!unreadDividerAutoScroll ||
unreadDividerPlacement === null ||
dividerId === null ||
positionKeeper === null ||
landedPlacementRef.current === unreadDividerPlacement
) {
return;
}
landedPlacementRef.current = unreadDividerPlacement;
if (positionKeeper.hasUserScrolled()) return;
positionKeeper.restore({
rowId: dividerId,
offsetWithinRow: 0,
atBottom: false,
});
}, [
dividerId,
positionKeeper,
unreadDividerAutoScroll,
unreadDividerPlacement,
]);
}

function findUnreadDividerIndex({
rows,
unreadDividerPlacement,
}: FindUnreadDividerIndexArgs): number {
}: BuildTimelineRowsListItemsArgs): number {
if (unreadDividerPlacement === null) {
return -1;
}

switch (unreadDividerPlacement.kind) {
case "before-first":
return rows.length > 0 ? 0 : -1;
case "after-cutoff":
return rows.findIndex((row) =>
isUnreadDividerCandidateAfterCutoff({
cutoffAt: unreadDividerPlacement.cutoffAt,
row,
}),
);
default:
assertNever(unreadDividerPlacement);
}
return rows.findIndex((row) =>
isUnreadDividerCandidateAfterCutoff({
cutoffAt: unreadDividerPlacement.cutoffAt,
row,
}),
);
}

function isUserAuthoredConversationRow(row: ThreadTimelineViewRow): boolean {
Expand Down Expand Up @@ -2054,10 +2083,25 @@ function TimelineRowsList({
() => findActiveLatestBundleId(rows),
[rows],
);
const loadedUnreadDividerPlacement = useLoadedUnreadDividerPlacement({
hasOlderRows: hasOlderTimelineRows ?? false,
rows,
unreadDividerPlacement,
});
const items = useMemo(
() => buildTimelineRowsListItems({ rows, unreadDividerPlacement }),
[rows, unreadDividerPlacement],
() =>
buildTimelineRowsListItems({
rows,
unreadDividerPlacement: loadedUnreadDividerPlacement,
}),
[rows, loadedUnreadDividerPlacement],
);
useLandOnUnreadDivider({
items,
positionKeeper: bottomAnchor?.positionKeeper ?? null,
unreadDividerAutoScroll,
unreadDividerPlacement: loadedUnreadDividerPlacement,
});
const itemKeys = useMemo(
() =>
items.map((item) =>
Expand Down Expand Up @@ -2141,11 +2185,10 @@ function TimelineRowsList({
<div
key={item.id}
ref={windowedState.itemRef}
data-scroll-landmark-id={item.id}
data-timeline-window-key={`divider:${item.id}`}
>
<TimelineUnreadDivider
autoScroll={unreadDividerAutoScroll}
/>
<TimelineUnreadDivider />
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @vitest-environment jsdom

import { renderHook } from "@testing-library/react";
import { buildTimelineViewRows } from "@bb/thread-view";
import { describe, expect, it } from "vitest";
import type { TimelineRow } from "@bb/server-contract";
Expand All @@ -7,7 +10,10 @@ import {
turnRow,
} from "@/test/fixtures/thread-timeline-rows";
import type { ThreadTimelineUnreadDividerPlacement } from "./types";
import { buildTimelineRowsListItems } from "./ThreadTimelineRows";
import {
buildTimelineRowsListItems,
useLoadedUnreadDividerPlacement,
} from "./ThreadTimelineRows";

const DIVIDER = "__divider__";

Expand Down Expand Up @@ -69,7 +75,7 @@ describe("unread divider placement", () => {
command(400),
assistantMessage(500),
],
{ kind: "after-cutoff", cutoffAt: 350 },
{ cutoffAt: 350 },
);

expect(sequence).toEqual(["user", "step-summary", DIVIDER, "assistant"]);
Expand All @@ -84,7 +90,7 @@ describe("unread divider placement", () => {
command(500),
assistantMessage(600),
],
{ kind: "after-cutoff", cutoffAt: 300 },
{ cutoffAt: 300 },
);

expect(sequence).toEqual([
Expand All @@ -111,7 +117,7 @@ describe("unread divider placement", () => {
}),
assistantMessage(500),
],
{ kind: "after-cutoff", cutoffAt: 350 },
{ cutoffAt: 350 },
);

expect(collapsed).toEqual(["user", "turn", DIVIDER, "assistant"]);
Expand All @@ -120,7 +126,7 @@ describe("unread divider placement", () => {
it("never anchors the divider on the reader's own message", () => {
const sequence = renderTopLevelSequence(
[assistantMessage(100), userMessage(400), command(500)],
{ kind: "after-cutoff", cutoffAt: 300 },
{ cutoffAt: 300 },
);

expect(sequence).toEqual(["assistant", "user", DIVIDER, "work"]);
Expand All @@ -129,18 +135,59 @@ describe("unread divider placement", () => {
it("omits the divider when no row started after the cutoff", () => {
const sequence = renderTopLevelSequence(
[userMessage(100), command(200), assistantMessage(300)],
{ kind: "after-cutoff", cutoffAt: 400 },
{ cutoffAt: 400 },
);

expect(sequence).not.toContain(DIVIDER);
});
});

it("places the divider above the first row for an explicitly unread thread", () => {
const sequence = renderTopLevelSequence(
[userMessage(100), command(200), assistantMessage(300)],
{ kind: "before-first" },
describe("loaded unread divider placement", () => {
it("drops a divider whose cutoff is older than the loaded rows, and keeps it dropped after older rows load", () => {
const placement = { cutoffAt: 350 };
const latestPage = buildTimelineViewRows([
command(400),
assistantMessage(500),
]);
const { result, rerender } = renderHook(
(props: Parameters<typeof useLoadedUnreadDividerPlacement>[0]) =>
useLoadedUnreadDividerPlacement(props),
{
initialProps: {
hasOlderRows: true,
rows: latestPage,
unreadDividerPlacement: placement,
},
},
);
expect(result.current).toBeNull();

expect(sequence[0]).toBe(DIVIDER);
rerender({
hasOlderRows: false,
rows: buildTimelineViewRows([
userMessage(100),
command(200),
command(400),
assistantMessage(500),
]),
unreadDividerPlacement: placement,
});
expect(result.current).toBeNull();
});

it("keeps a divider whose cutoff falls inside the loaded rows", () => {
const placement = { cutoffAt: 350 };
const { result } = renderHook(() =>
useLoadedUnreadDividerPlacement({
hasOlderRows: true,
rows: buildTimelineViewRows([
userMessage(300),
command(400),
assistantMessage(500),
]),
unreadDividerPlacement: placement,
}),
);
expect(result.current).toBe(placement);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ describe("ThreadTimelineRows windowing", () => {
getScrollElement: () => scrollElement,
isAtBottom: false,
scrollElementIntoView: vi.fn(),
scrollElementIntoViewClampedToMaxScroll: vi.fn(),
scrollToBottom: vi.fn(),
};
const rows = Array.from({ length: count }, (_, index) =>
Expand Down Expand Up @@ -245,7 +244,6 @@ describe("ThreadTimelineRows windowing", () => {
getScrollElement: () => scrollElement,
isAtBottom: false,
scrollElementIntoView,
scrollElementIntoViewClampedToMaxScroll: vi.fn(),
scrollToBottom: vi.fn(),
};
const rows = Array.from({ length: 80 }, (_, index) => {
Expand Down Expand Up @@ -300,7 +298,6 @@ describe("ThreadTimelineRows windowing", () => {
getScrollElement: () => scrollElement,
isAtBottom: false,
scrollElementIntoView,
scrollElementIntoViewClampedToMaxScroll: vi.fn(),
scrollToBottom: vi.fn(),
};
const rows = Array.from({ length: 80 }, (_, index) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe("ThreadTimelineSurface load-older control", () => {
getScrollElement: () => scrollElement,
isAtBottom: false,
scrollElementIntoView: vi.fn(),
scrollElementIntoViewClampedToMaxScroll: vi.fn(),
scrollToBottom: vi.fn(),
};
const onLoadOlderRows = vi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ describe("ConversationMessageOverflowToggle", () => {
isAtBottom: true,
scrollToBottom: vi.fn(),
scrollElementIntoView: vi.fn(),
scrollElementIntoViewClampedToMaxScroll: vi.fn(),
positionKeeper: createInertScrollPositionKeeper(),
holdContentPosition,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ const conversationRows: TimelineRow[] = [
];

const afterCutoffPlacement: ThreadTimelineUnreadDividerPlacement = {
kind: "after-cutoff",
cutoffAt: T0 + 3 * MINUTE + 30_000,
};

Expand Down
Loading
Loading