Defer chat auto-scroll to animation frame

- Queue tail-to-bottom scrolling with `requestAnimationFrame` to avoid redundant scroll calls
- Preserve user scroll position by checking auto-scroll intent before running
- Cancel any pending frame on unmount to prevent stale callbacks
This commit is contained in:
Julius Marminge committed 2026-02-19 15:51:14 -08:00
1 parent 0d9c26810f
commit 01accc005e
1 file changed
+19 -2
+19 -2
View File
@@ -402,6 +402,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
>(() => readLastInvokedScriptByProjectFromStorage());
const messagesScrollRef = useRef<HTMLDivElement>(null);
const shouldAutoScrollRef = useRef(true);
const pendingAutoScrollFrameRef = useRef<number | null>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const composerCommandInputRef = useRef<HTMLInputElement>(null);
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
@@ -1102,6 +1103,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
shouldAutoScrollRef.current = true;
}, []);
const requestAutoTailToBottom = useCallback(() => {
if (pendingAutoScrollFrameRef.current !== null) return;
pendingAutoScrollFrameRef.current = window.requestAnimationFrame(() => {
pendingAutoScrollFrameRef.current = null;
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom();
});
}, [scrollMessagesToBottom]);
const onMessagesScroll = useCallback(() => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
@@ -1113,8 +1122,16 @@ export default function ChatView({ threadId }: ChatViewProps) {
}, [activeThread?.id, scrollMessagesToBottom]);
useLayoutEffect(() => {
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom();
}, [activeThread?.messages, workLogCount, scrollMessagesToBottom]);
requestAutoTailToBottom();
}, [activeThread?.messages, workLogCount, requestAutoTailToBottom]);
useEffect(
() => () => {
if (pendingAutoScrollFrameRef.current === null) return;
window.cancelAnimationFrame(pendingAutoScrollFrameRef.current);
pendingAutoScrollFrameRef.current = null;
},
[],
);
useEffect(() => {
setExpandedWorkGroups({});