Skip to content

only auto scroll if at bottom of logs #2342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions web/src/components/wizard/installation/shared/LogViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useEffect } from 'react';
import React, { useRef, useEffect, useState } from 'react';
import { ChevronDown, ChevronUp } from 'lucide-react';

interface LogViewerProps {
Expand All @@ -15,12 +15,24 @@ const LogViewer: React.FC<LogViewerProps> = ({
onToggle
}) => {
const logsEndRef = useRef<HTMLDivElement>(null);
const logContainerRef = useRef<HTMLDivElement>(null);
const [isAtBottom, setIsAtBottom] = useState(true);

// Check if user is at bottom of logs
const handleLogContainerScroll = () => {
if (!logContainerRef.current) return;

const { scrollTop, scrollHeight, clientHeight } = logContainerRef.current;
const isBottom = Math.abs(scrollHeight - clientHeight - scrollTop) < 10;
Copy link
Preview

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The threshold value '10' is a magic number. Extract it into a named constant (e.g., SCROLL_THRESHOLD) to improve readability and make future adjustments easier.

Suggested change
const isBottom = Math.abs(scrollHeight - clientHeight - scrollTop) < 10;
const isBottom = Math.abs(scrollHeight - clientHeight - scrollTop) < SCROLL_THRESHOLD;

Copilot uses AI. Check for mistakes.

setIsAtBottom(isBottom);
};

// Only auto-scroll if user is at bottom
useEffect(() => {
if (logsEndRef.current && isExpanded) {
if (logsEndRef.current && isExpanded && isAtBottom) {
logsEndRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, [logs, isExpanded]);
}, [logs, isExpanded, isAtBottom]);

return (
<div className="mt-6" data-testid="log-viewer">
Expand All @@ -38,6 +50,8 @@ const LogViewer: React.FC<LogViewerProps> = ({
</button>
{isExpanded && (
<div
ref={logContainerRef}
onScroll={handleLogContainerScroll}
className="bg-gray-900 text-gray-200 rounded-md p-4 h-48 overflow-y-auto font-mono text-xs mt-2"
data-testid="log-viewer-content"
>
Expand Down