import { useEffect, useMemo, useState } from "react"; import Alert from "react-bootstrap/Alert"; import type { LogChannel, LogHostType, LogSourceType } from "src/api/backend"; import { Loading } from "src/components"; import { useLogSources, useLogTail } from "src/hooks"; import { T, intl } from "src/locale"; import LogLines from "./LogLines"; const LINES_OPTIONS = [100, 200, 500, 1000]; const LEVEL_OPTIONS = ["INFO", "WARN", "ERROR", "DEBUG", "SUCCESS", "FATAL", "COMPLETE"]; const SEARCH_DEBOUNCE_MS = 300; const HOST_TYPES: { type: LogHostType; labelId: string }[] = [ { type: "proxy", labelId: "proxy-hosts" }, { type: "redirection", labelId: "redirection-hosts" }, { type: "dead", labelId: "dead-hosts" }, { type: "stream", labelId: "streams" }, ]; interface Selection { type: LogSourceType; hostType?: LogHostType; hostId?: number; } const encodeSelection = (selection: Selection): string => { if (selection.type === "host") { return `host:${selection.hostType}:${selection.hostId}`; } return selection.type; }; const decodeSelection = (value: string): Selection => { if (value === "system" || value === "letsencrypt") { return { type: value }; } const [, hostType, hostId] = value.split(":"); return { type: "host", hostType: hostType as LogHostType, hostId: Number(hostId) }; }; const formatBytes = (bytes: number): string => { if (bytes < 1024) { return `${bytes} B`; } const units = ["KB", "MB", "GB"]; let value = bytes / 1024; let unitIndex = 0; while (value >= 1024 && unitIndex < units.length - 1) { value /= 1024; unitIndex++; } return `${value.toFixed(1)} ${units[unitIndex]}`; }; export default function LogViewer() { const sourcesQuery = useLogSources(); const [selection, setSelection] = useState({ type: "system" }); const [channel, setChannel] = useState("access"); const [level, setLevel] = useState(""); const [lines, setLines] = useState(LINES_OPTIONS[1]); const [live, setLive] = useState(true); const [searchInput, setSearchInput] = useState(""); const [search, setSearch] = useState(""); useEffect(() => { const handle = setTimeout(() => setSearch(searchInput.trim()), SEARCH_DEBOUNCE_MS); return () => clearTimeout(handle); }, [searchInput]); const tailQuery = useLogTail({ type: selection.type, hostType: selection.hostType, hostId: selection.hostId, channel: selection.type === "host" ? channel : undefined, lines, level: selection.type === "system" && level ? level : undefined, search: search || undefined, live, }); const hostGroups = useMemo(() => { if (!sourcesQuery.data) { return []; } return HOST_TYPES.map(({ type, labelId }) => ({ type, labelId, options: sourcesQuery.data.hosts[type] || [], })).filter((group) => group.options.length > 0); }, [sourcesQuery.data]); const handleDownload = () => { const content = (tailQuery.data?.lines || []).join("\n"); const blob = new Blob([content], { type: "text/plain" }); const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `${encodeSelection(selection).replace(/:/g, "-")}.log`; a.click(); window.URL.revokeObjectURL(url); }; return (

{typeof tailQuery.data?.size === "number" && (
{formatBytes(tailQuery.data.size)}
)}
{selection.type === "host" && (
)} {selection.type === "system" && (
)}
setSearchInput(e.target.value)} />
{tailQuery.isError && (
{tailQuery.error?.message || "Unknown error"}
)} {tailQuery.data?.truncated && (
)} {sourcesQuery.isLoading || (tailQuery.isLoading && !tailQuery.data) ? (
) : ( )}
); }