2026-09-03 12:57:06 -07:00
// Components
import Stack from "@mui/material/Stack" ;
import { BaseBox } from "@/Components/design-elements" ;
2026-09-04 11:42:11 -07:00
import { HeaderLogs } from "@/Pages/Docker/ContainerDetails/Components/HeaderLogs" ;
2026-09-03 12:57:06 -07:00
// Types
import type { DockerContainerLogsResponse } from "@/Types/Monitor" ;
// Hooks
import { useGet } from "@/Hooks/UseApi" ;
2026-09-04 11:42:11 -07:00
import {
useState ,
useEffect ,
useLayoutEffect ,
useMemo ,
Fragment ,
useRef ,
useCallback ,
} from "react" ;
2026-09-03 12:57:06 -07:00
import type { DockerLog } from "@/Types/Check" ;
2026-09-04 11:42:11 -07:00
import { useTheme } from "@mui/material" ;
2026-09-03 12:57:06 -07:00
// Util
import { LAYOUT , SPACING } from "@/Utils/Theme/constants" ;
import { flattenDockerLogs } from "@/Utils/MonitorUtils" ;
2026-09-04 11:42:11 -07:00
import { RowLog } from "@/Pages/Docker/ContainerDetails/Components/RowLog" ;
2026-09-03 12:57:06 -07:00
interface TabLogsProps {
monitorId? : string ;
containerName? : string ;
}
2026-09-04 11:42:11 -07:00
const SCROLL_PIN_THRESHOLD_PX = 8 ;
const isScrolledToBottom = ( el : HTMLElement ) =>
el . scrollHeight - el . scrollTop - el . clientHeight <= SCROLL_PIN_THRESHOLD_PX ;
2026-09-03 12:57:06 -07:00
export const TabLogs = ({ monitorId , containerName } : TabLogsProps ) => {
2026-09-04 11:42:11 -07:00
const theme = useTheme ();
2026-09-03 12:57:06 -07:00
// State
const [ olderLogs , setOlderLogs ] = useState < DockerLog [] >([]);
const [ olderCursor , setOlderCursor ] = useState < string | null | undefined >( undefined ); // undefined -> no older requested, string -> cursor, null -> no more data
const [ loadingOlder , setLoadingOlder ] = useState ( false );
const [ odlerError , setOlderError ] = useState < unknown >( null );
const baseLogsUrl =
monitorId && containerName
? `/monitors/docker/details/ ${ encodeURIComponent ( monitorId ) } /containers/ ${ encodeURIComponent ( containerName ) } /logs`
: null ;
const {
data : head ,
isLoading ,
isValidating ,
error : headError ,
refetch ,
} = useGet < DockerContainerLogsResponse >(
baseLogsUrl ,
{},
{
refreshInterval : 10_000 ,
keepPreviousData : true ,
revalidateOnFocus : true ,
}
);
const rows = useMemo (() => flattenDockerLogs ( head ? . logs ?? []), [ head ? . logs ]);
2026-09-04 11:42:11 -07:00
const logBoxRef = useRef < HTMLDivElement >( null );
const pinnedToBottom = useRef ( true );
// Always pin to bottom when container/monitor changes
useLayoutEffect (() => {
pinnedToBottom . current = true ;
}, [ monitorId , containerName ]);
// New log rows
useLayoutEffect (() => {
const el = logBoxRef . current ;
if ( ! el || ! pinnedToBottom . current ) return ;
el . scrollTop = el . scrollHeight ;
}, [ rows ]);
const handleScroll = useCallback (( event : React.UIEvent < HTMLDivElement >) => {
pinnedToBottom . current = isScrolledToBottom ( event . currentTarget );
}, []);
2026-09-03 12:57:06 -07:00
return (
< Stack gap = { LAYOUT . MD }>
2026-09-04 11:42:11 -07:00
< HeaderLogs
lines = { rows . length }
isValidating = { isValidating }
/>
2026-09-03 12:57:06 -07:00
< BaseBox
2026-09-04 11:42:11 -07:00
ref = { logBoxRef }
onScroll = { handleScroll }
2026-09-03 12:57:06 -07:00
minHeight = { 240 }
2026-09-04 11:42:11 -07:00
maxHeight = "50vh"
2026-09-03 12:57:06 -07:00
overflow = { "auto" }
padding = { LAYOUT . MD }
>
2026-09-04 11:42:11 -07:00
{ rows . map (( row ) => {
2026-09-03 12:57:06 -07:00
return (
2026-09-04 11:42:11 -07:00
< RowLog
key = { row . key }
row = { row }
/>
2026-09-03 12:57:06 -07:00
);
})}
</ BaseBox >
</ Stack >
);
2026-09-01 11:23:03 -07:00
};