Skip to content

Commit cf24abd

Browse files
Fix terminal auto-scroll on agent switch (#9)
Co-authored-by: rokt-clayschubiner <[email protected]>
1 parent d8e95ae commit cf24abd

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/renderer/components/ChatInterface.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const ChatInterface: React.FC<Props> = ({
9494
const { formatted: commentsContext } = useTaskComments(task.id);
9595

9696
// Auto-scroll to bottom when this task becomes active
97-
useAutoScrollOnTaskSwitch(true, task.id);
97+
const { scrollToBottom } = useAutoScrollOnTaskSwitch(true, task.id);
9898

9999
// Load conversations when task changes
100100
useEffect(() => {
@@ -519,6 +519,15 @@ const ChatInterface: React.FC<Props> = ({
519519
prevAgentRef.current = agent;
520520
}, [agent]);
521521

522+
// When switching agents/conversations, keep the terminal view pinned to the bottom
523+
// if the user was already near the bottom.
524+
useEffect(() => {
525+
const timeout = setTimeout(() => {
526+
scrollToBottom({ onlyIfNearBottom: true });
527+
}, 150);
528+
return () => clearTimeout(timeout);
529+
}, [agent, activeConversationId, scrollToBottom]);
530+
522531
useEffect(() => {
523532
const installed = currentAgentStatus?.installed === true;
524533
setIsAgentInstalled(installed);

src/renderer/components/MultiAgentTask.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ const MultiAgentTask: React.FC<Props> = ({ task }) => {
445445
if (variants.length > 0 && activeTabIndex >= 0 && activeTabIndex < variants.length) {
446446
// Small delay to ensure the tab content is rendered
447447
const timeout = setTimeout(() => {
448-
scrollToBottom({ onlyIfNearTop: true });
448+
scrollToBottom({ onlyIfNearBottom: true });
449449
// Focus the active terminal when switching tabs
450450
focusVariantByIndex(activeTabIndex);
451451
}, 150);

src/renderer/hooks/useAutoScrollOnTaskSwitch.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import { useEffect, useRef, useCallback } from 'react';
55
*/
66
type ScrollOptions = {
77
/**
8-
* Only scroll if the user is already near the top of the pane (avoids yanking them
8+
* Only scroll if the user is already near the bottom of the pane (avoids yanking them
99
* away from where they were reading).
1010
*/
11-
onlyIfNearTop?: boolean;
11+
onlyIfNearBottom?: boolean;
1212
};
1313

1414
export function useAutoScrollOnTaskSwitch(isActive: boolean, taskId: string | null) {
1515
const previousTaskIdRef = useRef<string | null>(null);
1616
const scrollTimeoutRef = useRef<NodeJS.Timeout | null>(null);
1717

1818
const scrollToBottom = useCallback((options: ScrollOptions = {}) => {
19-
const { onlyIfNearTop = true } = options;
19+
const { onlyIfNearBottom = true } = options;
2020

2121
// Restrict to terminal panes so we don't accidentally scroll unrelated panels.
2222
const selectors = [
@@ -35,10 +35,12 @@ export function useAutoScrollOnTaskSwitch(isActive: boolean, taskId: string | nu
3535
container.getClientRects().length > 0 &&
3636
container.clientHeight > 0;
3737
const hasScrollableContent = container.scrollHeight > container.clientHeight;
38-
const nearTop = container.scrollTop <= 32;
38+
const distanceFromBottom =
39+
container.scrollHeight - (container.scrollTop + container.clientHeight);
40+
const nearBottom = distanceFromBottom <= 32;
3941

4042
if (!isVisible || !hasScrollableContent) return;
41-
if (onlyIfNearTop && !nearTop) return;
43+
if (onlyIfNearBottom && !nearBottom) return;
4244

4345
container.scrollTo({
4446
top: container.scrollHeight,
@@ -69,7 +71,7 @@ export function useAutoScrollOnTaskSwitch(isActive: boolean, taskId: string | nu
6971

7072
// Delay scroll to allow content to render
7173
scrollTimeoutRef.current = setTimeout(() => {
72-
scrollToBottom({ onlyIfNearTop: false });
74+
scrollToBottom({ onlyIfNearBottom: false });
7375
}, 200);
7476
}
7577

0 commit comments

Comments
 (0)