Implement log streaming with subscription-based approach instead of polling
Co-authored-by: ajnart <49837342+ajnart@users.noreply.github.com>
This commit is contained in:
1 parent
762cdab78f
commit
23ce0d14ae
3 files changed
+156
-18
No files matched your search
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Box, Group, Loader, ScrollArea, Stack, Text } from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
@@ -16,41 +16,79 @@ interface DockerLogsModalInnerProps {
|
||||
export const DockerLogsModal = createModal<DockerLogsModalInnerProps>(({ innerProps }) => {
|
||||
const t = useI18n();
|
||||
const viewportRef = useRef<HTMLDivElement | null>(null);
|
||||
const didInitialScrollRef = useRef(false);
|
||||
const [logs, setLogs] = useState<string>("");
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const shouldScrollRef = useRef(true);
|
||||
|
||||
const { data, isPending, isError } = clientApi.docker.logs.useQuery(
|
||||
{ id: innerProps.id, tail: innerProps.tail },
|
||||
// Get initial logs
|
||||
const { data: initialData } = clientApi.docker.logs.useQuery(
|
||||
{ id: innerProps.id, tail: innerProps.tail ?? 200 },
|
||||
{
|
||||
refetchOnWindowFocus: false,
|
||||
retry: 0,
|
||||
refetchInterval: 1000,
|
||||
refetchIntervalInBackground: true,
|
||||
},
|
||||
);
|
||||
|
||||
// Subscribe to streaming logs
|
||||
clientApi.docker.subscribeLogs.useSubscription(
|
||||
{ id: innerProps.id, tail: innerProps.tail ?? 200 },
|
||||
{
|
||||
onData(data) {
|
||||
setLogs((prev) => prev + data);
|
||||
setIsLoading(false);
|
||||
},
|
||||
onError(err) {
|
||||
setError(err.message);
|
||||
setIsLoading(false);
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// Initialize with static logs
|
||||
useEffect(() => {
|
||||
if (didInitialScrollRef.current) return;
|
||||
if (!data?.logs) return;
|
||||
if (initialData?.logs) {
|
||||
setLogs(initialData.logs);
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [initialData]);
|
||||
|
||||
// Auto-scroll to bottom when new logs arrive
|
||||
useEffect(() => {
|
||||
if (!shouldScrollRef.current) return;
|
||||
const viewport = viewportRef.current;
|
||||
if (!viewport) return;
|
||||
viewport.scrollTop = viewport.scrollHeight;
|
||||
didInitialScrollRef.current = true;
|
||||
}, [data?.logs]);
|
||||
|
||||
// Check if user has scrolled up
|
||||
const isAtBottom = viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight < 100;
|
||||
if (isAtBottom) {
|
||||
viewport.scrollTop = viewport.scrollHeight;
|
||||
}
|
||||
}, [logs]);
|
||||
|
||||
// Handle manual scroll to determine auto-scroll behavior
|
||||
const handleScroll = () => {
|
||||
const viewport = viewportRef.current;
|
||||
if (!viewport) return;
|
||||
|
||||
const isAtBottom = viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight < 100;
|
||||
shouldScrollRef.current = isAtBottom;
|
||||
};
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Stack gap="sm">
|
||||
{isPending ? (
|
||||
{isLoading ? (
|
||||
<Group justify="center" py="md">
|
||||
<Loader size="sm" />
|
||||
</Group>
|
||||
) : isError ? (
|
||||
) : error ? (
|
||||
<Text c="red" size="sm">
|
||||
Failed to load logs
|
||||
{error}
|
||||
</Text>
|
||||
) : (
|
||||
<ScrollArea h="75vh" viewportRef={viewportRef}>
|
||||
{data?.logs ? (
|
||||
<ScrollArea h="75vh" viewportRef={viewportRef} onScrollPositionChange={handleScroll}>
|
||||
{logs ? (
|
||||
<pre
|
||||
style={{
|
||||
margin: 0,
|
||||
@@ -58,7 +96,7 @@ export const DockerLogsModal = createModal<DockerLogsModalInnerProps>(({ innerPr
|
||||
overflowWrap: "anywhere",
|
||||
}}
|
||||
>
|
||||
<code>{data.logs}</code>
|
||||
<code>{logs}</code>
|
||||
</pre>
|
||||
) : (
|
||||
<Text size="sm" c="dimmed">
|
||||
|
||||
Reference in new issue
Block a user