Skip to content
Merged
Changes from 3 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
65 changes: 64 additions & 1 deletion ui/providers/BottomDrawer/containers/LogViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ interface Props {

const LogViewerContainer: React.FC<Props> = ({ sessionId, source, toolbarPrefix, toolbarActions }) => {
const parentRef = useRef<HTMLDivElement>(null);
const [scrollElement, setScrollElement] = useState<HTMLDivElement | null>(null);
const scrollRefCallback = useCallback((node: HTMLDivElement | null) => {
parentRef.current = node;
setScrollElement(node);
}, []);
const [showTimestamps, setShowTimestamps] = useState(false);
const [showSources, setShowSources] = useState(false);
const [showLineNumbers, setShowLineNumbers] = useState(false);
Expand Down Expand Up @@ -153,6 +158,64 @@ const LogViewerContainer: React.FC<Props> = ({ sessionId, source, toolbarPrefix,
}
}, [follow, filteredLineCount, version]);

// Keep refs in sync for use inside ResizeObserver (avoids re-subscribing)
const followRef = useRef(follow);
followRef.current = follow;
const filteredLineCountRef = useRef(filteredLineCount);
filteredLineCountRef.current = filteredLineCount;

// Re-pin to bottom (or restore position) when the scroll container resizes.
// This covers drawer drag-resize, minimize/re-expand, and fullscreen toggle.
const savedFirstVisibleRef = useRef<number | null>(null);
const lastContainerHeightRef = useRef(0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

React.useEffect(() => {
if (!scrollElement) return;

const observer = new ResizeObserver((entries) => {
const rect = entries[0]?.contentRect;
if (!rect) return;
const newHeight = rect.height;
const prevHeight = lastContainerHeightRef.current;

if (newHeight > 0 && filteredLineCountRef.current > 0) {
if (followRef.current) {
// Follow mode: always pin to bottom
isAutoScrolling.current = true;
rowVirtualizer.scrollToIndex(filteredLineCountRef.current - 1, { align: 'end' });
requestAnimationFrame(() => {
requestAnimationFrame(() => {
isAutoScrolling.current = false;
});
});
} else if (prevHeight === 0 && savedFirstVisibleRef.current !== null) {
// Restoring from minimized without follow: jump back to saved position
isAutoScrolling.current = true;
rowVirtualizer.scrollToIndex(savedFirstVisibleRef.current, { align: 'start' });
savedFirstVisibleRef.current = null;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
isAutoScrolling.current = false;
});
});
}
}

// Save the first visible index before the container collapses
if (prevHeight > 0 && newHeight === 0 && !followRef.current) {
const range = rowVirtualizer.range;
if (range) {
savedFirstVisibleRef.current = range.startIndex;
}
}

lastContainerHeightRef.current = newHeight;
});

observer.observe(scrollElement);
return () => observer.disconnect();
}, [scrollElement, rowVirtualizer]);

// Detect scroll position to engage/disengage follow
const handleScroll = useCallback(() => {
if (!parentRef.current || isAutoScrolling.current) return;
Expand Down Expand Up @@ -600,7 +663,7 @@ const LogViewerContainer: React.FC<Props> = ({ sessionId, source, toolbarPrefix,
) : (
/* Virtual log list */
<Box
ref={parentRef}
ref={scrollRefCallback}
onScroll={handleScroll}
onKeyDown={handleLogKeyDown}
onMouseDown={handleLogMouseDown}
Expand Down
Loading