"use client"; import { useQuery } from "@tanstack/react-query"; import { useRef, useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; export function LogViewer() { const logRef = useRef(null); const [autoRefresh, setAutoRefresh] = useState(false); const [lines, setLines] = useState(100); const [hasLoaded, setHasLoaded] = useState(false); const autoScrollRef = useRef(true); const { data, refetch, isFetching, isError, error } = useQuery<{ logs: string }>({ queryKey: ["logs", lines], queryFn: async () => { const res = await fetch(`/api/logs?lines=${lines}`); if (!res.ok) throw new Error(`Failed to load logs (${res.status})`); return res.json(); }, enabled: hasLoaded, refetchInterval: autoRefresh ? 3000 : false, }); // Auto-scroll only if user is near the bottom useEffect(() => { if (logRef.current && autoScrollRef.current) { logRef.current.scrollTop = logRef.current.scrollHeight; } }, [data]); const handleScroll = () => { if (!logRef.current) return; const { scrollTop, scrollHeight, clientHeight } = logRef.current; autoScrollRef.current = scrollHeight - scrollTop - clientHeight < 80; }; // Fetch on mount useEffect(() => { setHasLoaded(true); }, []); return (
Console Logs Server output from journalctl
          {isError
            ? `Failed to load logs: ${error instanceof Error ? error.message : "unknown error"}`
            : data?.logs || (isFetching ? "Loading logs..." : "No logs available.")}
        
); }