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
35 changes: 14 additions & 21 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import { type LegendListRef } from "@legendapp/list/react";
import {
CHAT_TIMELINE_ANCHOR_OFFSET,
getAnchoredTurnMetrics,
readTimelinePosition,
timelineContentOverflowsViewport,
type TimelineScrollMode,
} from "./chat/timelineScrollAnchoring";
Expand Down Expand Up @@ -5222,10 +5223,12 @@ export default function ChatView(props: ChatViewProps) {
const settledTimelineAnchorRef = useRef<MessageId | null>(null);
const activeTimelineAnchorIndexRef = useRef<number | null>(null);
const anchorUserScrollGenerationRef = useRef(0);
const cancelPositionRestoreRef = useRef<(() => void) | null>(null);
const liveFollowUserScrollGenerationRef = useRef<number | null>(0);
// 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(() => {
cancelPositionRestoreRef.current?.();
anchorUserScrollGenerationRef.current += 1;
timelineScrollModeRef.current = "free-scrolling";
liveFollowUserScrollGenerationRef.current = null;
Expand Down Expand Up @@ -5305,6 +5308,7 @@ export default function ChatView(props: ChatViewProps) {
// Live-follow stays active after send/thread-open until an actual list scroll
// gesture opts out.
const scrollToEnd = useCallback((animated = false) => {
cancelPositionRestoreRef.current?.();
isAtEndRef.current = true;
timelineScrollModeRef.current = "following-end";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
Expand All @@ -5320,21 +5324,6 @@ export default function ChatView(props: ChatViewProps) {
void legendListRef.current?.scrollToEnd?.({ animated });
});
}, []);
const displayedTimelineKeyRef = useRef(displayedTimeline.displayThreadKey);
useLayoutEffect(() => {
const displayKey = displayedTimeline.displayThreadKey;
if (displayKey === null || displayKey !== activeThreadKey) {
displayedTimelineKeyRef.current = displayKey;
return;
}
if (displayedTimelineKeyRef.current === displayKey) {
return;
}
displayedTimelineKeyRef.current = displayKey;
// Keep the list mounted across jumps; pin the newly displayed thread to
// its end the way a remount used to via initialScrollAtEnd.
scrollToEnd();
}, [activeThreadKey, displayedTimeline.displayThreadKey, scrollToEnd]);
useLayoutEffect(() => {
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
return;
Expand Down Expand Up @@ -5644,19 +5633,22 @@ export default function ChatView(props: ChatViewProps) {

useEffect(() => {
setPullRequestDialogState(null);
isAtEndRef.current = true;
const followEnd = readTimelinePosition(routeThreadKey)?.atEnd !== false;
isAtEndRef.current = followEnd;
timelineScrollIntentRef.current = null;
timelineScrollModeRef.current = "following-end";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
setTimelineLiveFollowEnabled(true);
timelineScrollModeRef.current = followEnd ? "following-end" : "free-scrolling";
liveFollowUserScrollGenerationRef.current = followEnd
? anchorUserScrollGenerationRef.current
: null;
setTimelineLiveFollowEnabled(followEnd);
pendingTimelineAnchorRef.current = null;
positionedTimelineAnchorRef.current = null;
settledTimelineAnchorRef.current = null;
activeTimelineAnchorIndexRef.current = null;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
setShowScrollToBottom(!followEnd);
// activeThreadRef resets transitively with the active thread.
}, [activeThread?.id]);
}, [activeThread?.id, routeThreadKey]);

useEffect(() => {
if (!activeThread?.id || terminalUiState.terminalOpen) return;
Expand Down Expand Up @@ -9513,6 +9505,7 @@ export default function ChatView(props: ChatViewProps) {
onContentOverflowChange={setTimelineOverflows}
onToolOutputCollapsedAtEnd={onToolOutputCollapsedAtEnd}
onManualNavigation={cancelTimelineLiveFollowForUserNavigation}
cancelPositionRestoreRef={cancelPositionRestoreRef}
hideEmptyPlaceholder={isDraftHeroState || threadDetailLoading}
topFadeEnabled={!hasTimelineTopBanner}
loadEarlier={paintOnlyDisplayedTimeline ? null : loadEarlierTurns}
Expand Down
Loading
Loading