1
- import React , { useRef , useEffect } from 'react' ;
1
+ import React , { useRef , useEffect , useState } from 'react' ;
2
2
import { ChevronDown , ChevronUp } from 'lucide-react' ;
3
3
4
4
interface LogViewerProps {
@@ -15,12 +15,24 @@ const LogViewer: React.FC<LogViewerProps> = ({
15
15
onToggle
16
16
} ) => {
17
17
const logsEndRef = useRef < HTMLDivElement > ( null ) ;
18
+ const logContainerRef = useRef < HTMLDivElement > ( null ) ;
19
+ const [ isAtBottom , setIsAtBottom ] = useState ( true ) ;
18
20
21
+ // Check if user is at bottom of logs
22
+ const handleScroll = ( ) => {
23
+ if ( ! logContainerRef . current ) return ;
24
+
25
+ const { scrollTop, scrollHeight, clientHeight } = logContainerRef . current ;
26
+ const isBottom = Math . abs ( scrollHeight - clientHeight - scrollTop ) < 10 ;
27
+ setIsAtBottom ( isBottom ) ;
28
+ } ;
29
+
30
+ // Only auto-scroll if user is at bottom
19
31
useEffect ( ( ) => {
20
- if ( logsEndRef . current && isExpanded ) {
32
+ if ( logsEndRef . current && isExpanded && isAtBottom ) {
21
33
logsEndRef . current . scrollIntoView ( { behavior : 'smooth' } ) ;
22
34
}
23
- } , [ logs , isExpanded ] ) ;
35
+ } , [ logs , isExpanded , isAtBottom ] ) ;
24
36
25
37
return (
26
38
< div className = "mt-6" data-testid = "log-viewer" >
@@ -38,6 +50,8 @@ const LogViewer: React.FC<LogViewerProps> = ({
38
50
</ button >
39
51
{ isExpanded && (
40
52
< div
53
+ ref = { logContainerRef }
54
+ onScroll = { handleScroll }
41
55
className = "bg-gray-900 text-gray-200 rounded-md p-4 h-48 overflow-y-auto font-mono text-xs mt-2"
42
56
data-testid = "log-viewer-content"
43
57
>
0 commit comments