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 1 commit 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 handleScroll = () => {
if (!logContainerRef.current) return;

const { scrollTop, scrollHeight, clientHeight } = logContainerRef.current;
const isBottom = Math.abs(scrollHeight - clientHeight - scrollTop) < 10;
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={handleScroll}
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
Loading