diff --git a/frontend/src/components/log-viewer/LogContent.jsx b/frontend/src/components/log-viewer/LogContent.jsx index 6494ab4f..a27518bb 100644 --- a/frontend/src/components/log-viewer/LogContent.jsx +++ b/frontend/src/components/log-viewer/LogContent.jsx @@ -1,4 +1,4 @@ -import { forwardRef, useMemo } from 'react'; +import { forwardRef, useMemo, useRef, useCallback, useLayoutEffect } from 'react'; import { severityOf, splitOnMatch } from './logHelpers'; const LogContent = forwardRef(function LogContent({ @@ -8,10 +8,44 @@ const LogContent = forwardRef(function LogContent({ showLineNumbers, wrapLines, searchPattern, + live = false, + scrollKey, }, ref) { - const lines = useMemo(() => { - if (!content) return []; - return content.split('\n'); + const innerRef = useRef(null); + // Whether the viewer is parked at the bottom (following the tail). Starts + // true so a freshly opened log lands on its newest line. + const followRef = useRef(true); + + // Merge our internal ref with the forwarded ref so the parent keeps its + // direct scroll access (jump-to-end button, etc.) while we manage tailing. + const setRefs = useCallback((node) => { + innerRef.current = node; + if (typeof ref === 'function') ref(node); + else if (ref) ref.current = node; + }, [ref]); + + const lines = useMemo(() => (content ? content.split('\n') : []), [content]); + + // Stop following once the user scrolls up; resume when they return to the + // bottom — so live updates don't yank them away while reading history. + const handleScroll = useCallback(() => { + const el = innerRef.current; + if (!el) return; + followRef.current = el.scrollHeight - el.scrollTop - el.clientHeight < 48; + }, []); + + // New selection (different file / unit / service): jump to the newest line + // and re-arm follow. + useLayoutEffect(() => { + followRef.current = true; + const el = innerRef.current; + if (el) el.scrollTop = el.scrollHeight; + }, [scrollKey]); + + // New content while following: stick to the last record. + useLayoutEffect(() => { + const el = innerRef.current; + if (el && followRef.current) el.scrollTop = el.scrollHeight; }, [content]); if (loading) { @@ -27,33 +61,42 @@ const LogContent = forwardRef(function LogContent({ } return ( -