Skip to content

Commit c8d0bf3

Browse files
committed
Fix auto-scroll race condition during agent streaming
1 parent 246dcbf commit c8d0bf3

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

apps/twig/src/renderer/features/sessions/components/ConversationView.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ export function ConversationView({
8484
const queuedMessages = useQueuedMessagesForTask(taskId);
8585
const { removeQueuedMessage } = useSessionActions();
8686

87-
const isNearBottomRef = useRef(true);
8887
const prevItemsLengthRef = useRef(0);
8988
const prevPendingCountRef = useRef(0);
89+
const prevScrollHeightRef = useRef(0);
9090
const [showScrollButton, setShowScrollButton] = useState(false);
9191

9292
useLayoutEffect(() => {
@@ -96,7 +96,6 @@ export function ConversationView({
9696
const handleScroll = () => {
9797
const distanceFromBottom =
9898
el.scrollHeight - el.scrollTop - el.clientHeight;
99-
isNearBottomRef.current = distanceFromBottom <= SCROLL_THRESHOLD;
10099
setShowScrollButton(distanceFromBottom > SHOW_BUTTON_THRESHOLD);
101100
};
102101

@@ -113,7 +112,12 @@ export function ConversationView({
113112
prevItemsLengthRef.current = items.length;
114113
prevPendingCountRef.current = pendingPermissionsCount;
115114

116-
if (isNearBottomRef.current || isNewContent || isNewPending) {
115+
const prevScrollHeight = prevScrollHeightRef.current || el.scrollHeight;
116+
const wasNearBottom =
117+
prevScrollHeight - el.scrollTop - el.clientHeight <= SCROLL_THRESHOLD;
118+
prevScrollHeightRef.current = el.scrollHeight;
119+
120+
if (wasNearBottom || isNewContent || isNewPending) {
117121
el.scrollTop = el.scrollHeight;
118122
}
119123
}, [items, pendingPermissionsCount]);

0 commit comments

Comments
 (0)