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
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@dnd-kit/utilities": "^3.2.2",
"@effect/atom-react": "catalog:",
"@formkit/auto-animate": "^0.9.0",
"@legendapp/list": "3.2.0",
"@legendapp/list": "3.3.3",
"@lexical/react": "^0.41.0",
"@pierre/diffs": "catalog:",
"@pierre/trees": "1.0.0-beta.4",
Expand Down
136 changes: 32 additions & 104 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3527,12 +3527,8 @@ function ChatViewContent(props: ChatViewProps) {
const activeTimelineAnchorIndexRef = useRef<number | null>(null);
const anchorUserScrollGenerationRef = useRef(0);
const liveFollowUserScrollGenerationRef = useRef<number | null>(0);
const pendingAnchorScrollRestoreRef = useRef<{
readonly messageId: MessageId;
readonly offset: number;
readonly userScrollGeneration: number;
} | null>(null);
const anchorScrollRestoreFrameRef = useRef<number | null>(null);
// Manual navigation stops live-follow without removing anchored end space.
// Collapsing that space during a gesture clamps the viewport back to the end.
const cancelTimelineLiveFollowForUserNavigation = useCallback(() => {
anchorUserScrollGenerationRef.current += 1;
timelineScrollModeRef.current = "free-scrolling";
Expand All @@ -3542,11 +3538,6 @@ function ChatViewContent(props: ChatViewProps) {
positionedTimelineAnchorRef.current = null;
settledTimelineAnchorRef.current = null;
activeTimelineAnchorIndexRef.current = null;
pendingAnchorScrollRestoreRef.current = null;
if (anchorScrollRestoreFrameRef.current !== null) {
cancelAnimationFrame(anchorScrollRestoreFrameRef.current);
anchorScrollRestoreFrameRef.current = null;
}
}, []);
const cancelTimelineLiveFollowForUserNavigationRef = useRef(
cancelTimelineLiveFollowForUserNavigation,
Expand Down Expand Up @@ -3602,7 +3593,6 @@ function ChatViewContent(props: ChatViewProps) {
},
[composerOverlayHeight],
);

// Live-follow stays active after send/thread-open until an actual list scroll
// gesture opts out.
const scrollToEnd = useCallback((animated = false) => {
Expand All @@ -3614,7 +3604,12 @@ function ChatViewContent(props: ChatViewProps) {
activeTimelineAnchorIndexRef.current = null;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
void legendListRef.current?.scrollToEnd?.({ animated });
setTimelineAnchor((current) =>
current.messageId === null ? current : { ...current, messageId: null },
);
requestAnimationFrame(() => {
void legendListRef.current?.scrollToEnd?.({ animated });
});
}, []);
useEffect(() => {
let removeListeners: (() => void) | null = null;
Expand Down Expand Up @@ -3753,75 +3748,23 @@ function ChatViewContent(props: ChatViewProps) {
}
return;
}
const scrollNode = list.getScrollableNode();
let finished = false;
const finishAnimatedPositioning = () => {
if (finished) {
return;
}
finished = true;
window.clearTimeout(fallbackTimer);
scrollNode.removeEventListener("scrollend", finishAnimatedPositioning);
if (positionedTimelineAnchorRef.current !== messageId) {
return;
}
const scrollOffset = list.getState().scroll;
void list.scrollToOffset({ offset: scrollOffset, animated: false });
settledTimelineAnchorRef.current = messageId;
};
const fallbackTimer = window.setTimeout(finishAnimatedPositioning, 750);
scrollNode.addEventListener("scrollend", finishAnimatedPositioning, { once: true });
void list.scrollToIndex({
index: anchorIndex,
animated: true,
viewPosition: 0,
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
});
void list
.scrollToIndex({
index: anchorIndex,
animated: true,
viewPosition: 0,
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
})
.then(() => {
if (positionedTimelineAnchorRef.current !== messageId) {
return;
}
settledTimelineAnchorRef.current = messageId;
});
});
};
requestAnimationFrame(() => positionAnchor(12));
}, []);
const onTimelineAnchorSizeChanged = useCallback((messageId: MessageId) => {
if (settledTimelineAnchorRef.current !== messageId) {
return;
}
if (liveFollowUserScrollGenerationRef.current === anchorUserScrollGenerationRef.current) {
return;
}
const scrollOffset = legendListRef.current?.getState().scroll;
if (scrollOffset === undefined) {
return;
}
if (pendingAnchorScrollRestoreRef.current === null) {
pendingAnchorScrollRestoreRef.current = {
messageId,
offset: scrollOffset,
userScrollGeneration: anchorUserScrollGenerationRef.current,
};
}
if (anchorScrollRestoreFrameRef.current !== null) {
return;
}
anchorScrollRestoreFrameRef.current = requestAnimationFrame(() => {
anchorScrollRestoreFrameRef.current = null;
const pending = pendingAnchorScrollRestoreRef.current;
pendingAnchorScrollRestoreRef.current = null;
if (
pending &&
settledTimelineAnchorRef.current === pending.messageId &&
pending.userScrollGeneration === anchorUserScrollGenerationRef.current
) {
const list = legendListRef.current;
const currentScrollOffset = list?.getState().scroll;
if (
typeof currentScrollOffset === "number" &&
Math.abs(currentScrollOffset - pending.offset) <= 2
) {
void list?.scrollToOffset({ offset: pending.offset, animated: false });
}
}
});
}, []);

const onIsAtEndChange = useCallback((isAtEnd: boolean) => {
if (
Expand All @@ -3847,13 +3790,19 @@ function ChatViewContent(props: ChatViewProps) {
}
}, []);

// Anchored end space intentionally disables LegendList's normal end-follow so
// the sent message can stay near the top. T3 only owns streaming adjustments
// during that mode; LegendList owns ordinary end-follow everywhere else.
useEffect(() => {
if (!activeThread?.id) {
return;
}
if (liveFollowUserScrollGenerationRef.current !== anchorUserScrollGenerationRef.current) {
return;
}
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
return;
}

let secondFrame: number | null = null;
const frame = requestAnimationFrame(() => {
Expand All @@ -3875,28 +3824,13 @@ function ChatViewContent(props: ChatViewProps) {
return;
}

if (timelineScrollModeRef.current === "anchoring-new-turn") {
const metrics = getActiveTimelineTurnMetrics(list);
if (!metrics) {
return;
}
if (metrics.scrollDeltaToRevealEnd <= 1) {
return;
}

const nextOffset = list.getState().scroll + metrics.scrollDeltaToRevealEnd;
void list.scrollToOffset({ offset: nextOffset, animated: false });
return;
}

if (timelineScrollModeRef.current !== "following-end") {
return;
}
if (!timelineRealContentOverflowsViewport(list)) {
const metrics = getActiveTimelineTurnMetrics(list);
if (!metrics || metrics.scrollDeltaToRevealEnd <= 1) {
return;
}

void list.scrollToEnd?.({ animated: false });
const nextOffset = list.getState().scroll + metrics.scrollDeltaToRevealEnd;
void list.scrollToOffset({ offset: nextOffset, animated: false });
});
});

Expand All @@ -3906,12 +3840,7 @@ function ChatViewContent(props: ChatViewProps) {
cancelAnimationFrame(secondFrame);
}
};
}, [
activeThread?.id,
timelineEntries,
getActiveTimelineTurnMetrics,
timelineRealContentOverflowsViewport,
]);
}, [activeThread?.id, timelineEntries, getActiveTimelineTurnMetrics]);

useEffect(() => {
setPullRequestDialogState(null);
Expand Down Expand Up @@ -6103,7 +6032,6 @@ function ChatViewContent(props: ChatViewProps) {
skills={activeProviderStatus?.skills ?? EMPTY_PROVIDER_SKILLS}
anchorMessageId={timelineAnchorMessageId}
onAnchorReady={onTimelineAnchorReady}
onAnchorSizeChanged={onTimelineAnchorSizeChanged}
contentInsetEndAdjustment={composerOverlayHeight}
liveFollowEnabled={timelineLiveFollowEnabled}
onIsAtEndChange={onIsAtEndChange}
Expand Down
14 changes: 7 additions & 7 deletions apps/web/src/components/chat/MessagesTimeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ vi.mock("@legendapp/list/react", async () => {
anchorMaxSize?: number;
anchorOffset?: number;
onReady?: (info: { anchorIndex: number }) => void;
onSizeChanged?: (size: number) => void;
};
contentInsetEndAdjustment?: number;
className?: string;
Expand All @@ -42,7 +41,6 @@ vi.mock("@legendapp/list/react", async () => {
ref?: Ref<LegendListRef>;
}) => {
if (props.anchoredEndSpace) {
props.anchoredEndSpace.onSizeChanged?.(240);
props.anchoredEndSpace.onReady?.({ anchorIndex: props.anchoredEndSpace.anchorIndex });
}
return (
Expand Down Expand Up @@ -90,6 +88,11 @@ vi.mock("@legendapp/list/react", async () => {
? props.maintainVisibleContentPosition.size
: undefined
}
data-maintain-visible-content-position-restore={
typeof props.maintainVisibleContentPosition === "object"
? Boolean(props.maintainVisibleContentPosition.shouldRestorePosition)
: undefined
}
>
{props.ListHeaderComponent}
{props.data.map((item) => (
Expand Down Expand Up @@ -192,7 +195,6 @@ function buildProps() {
workspaceRoot: undefined,
anchorMessageId: null,
onAnchorReady: () => {},
onAnchorSizeChanged: () => {},
contentInsetEndAdjustment: 0,
liveFollowEnabled: true,
onIsAtEndChange: () => {},
Expand Down Expand Up @@ -386,7 +388,6 @@ describe("MessagesTimeline", () => {

it("anchors a sent attachment message using its measured height", () => {
const onAnchorReady = vi.fn();
const onAnchorSizeChanged = vi.fn();
const firstEntry = buildUserTimelineEntry("First prompt.");
const secondEntry = {
...buildUserTimelineEntry("Newest prompt."),
Expand All @@ -411,7 +412,6 @@ describe("MessagesTimeline", () => {
{...buildProps()}
anchorMessageId={secondEntry.message.id}
onAnchorReady={onAnchorReady}
onAnchorSizeChanged={onAnchorSizeChanged}
contentInsetEndAdjustment={144}
timelineEntries={[firstEntry, secondEntry]}
/>,
Expand All @@ -426,10 +426,10 @@ describe("MessagesTimeline", () => {
expect(markup).not.toContain('data-maintain-scroll-at-end="enabled"');
expect(markup).toContain('data-maintain-visible-content-position="object"');
expect(markup).toContain('data-maintain-visible-content-position-data="true"');
expect(markup).toContain('data-maintain-visible-content-position-size="false"');
expect(markup).toContain('data-maintain-visible-content-position-size="true"');
expect(markup).toContain('data-maintain-visible-content-position-restore="true"');
expect(onAnchorReady).toHaveBeenCalledOnce();
expect(onAnchorReady).toHaveBeenCalledWith(secondEntry.message.id, 1);
expect(onAnchorSizeChanged).toHaveBeenCalledWith(secondEntry.message.id, 240);
});

it("renders collapse controls for long user messages", () => {
Expand Down
Loading
Loading