mc-dashboard/components/LogViewer.tsx
hurkicorgi 7ada9ec7d9 Bundle analyzer, task run-now/toggle/weekly, error reporter, OG image, a11y
- Bundle analyzer: @next/bundle-analyzer wired through next.config.ts,
  new `bun run analyze` script (ANALYZE=true next build).
- Scheduled tasks gain:
  - enabled flag round-tripped via a #DISABLED prefix on the crontab line
    (preserves the mc-task marker + payload for re-enable).
  - PATCH /api/schedule/tasks to toggle enabled.
  - POST /api/schedule/tasks/run to execute a task immediately via
    the same buildCommand used for the cron line (60s timeout, kills
    child on client abort).
  - Weekly preset in the UI (day-of-week selector), broader aria-labels
    on all form selects. Human-readable cron renders "Tue at 04:30"
    and next-run calc accounts for weekly.
  - Hover-reveal Run now / Enable / Disable / Remove actions; disabled
    tasks render at 60% opacity with a "disabled" badge and no next-run.
- /api/errors: minimal append-only JSONL reporter (200ms throttle, UA
  and IP captured, fields length-bounded). Falls back to a stable
  path under /home/minecraft/logs/ and never fails the client on
  logging errors.
- ErrorReporter client (production-only) listens to window.error and
  unhandledrejection, fingerprint-dedups via a bounded in-memory set,
  sends with keepalive so unloads still flush.
- app/opengraph-image.tsx: 1200x630 dynamic PNG with live status dot
  (green/red), player count, address. 60s memo on the status probe.
- A11y: aria-labels on log-line count select, scheduled-restart hour
  and minute selects, copy-server-address button (plus focus-visible
  ring). ModManager selected-mod pill upgraded to role=button with a
  real accessible name and a proper × glyph.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 06:08:48 -06:00

222 lines
7.3 KiB
TypeScript

"use client";
import { useQuery } from "@tanstack/react-query";
import { useMemo, useRef, useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
type Level = "ERROR" | "WARN" | "INFO" | "DEBUG" | "OTHER";
const LEVEL_ORDER: Level[] = ["ERROR", "WARN", "INFO", "DEBUG", "OTHER"];
const levelClass: Record<Level, string> = {
ERROR: "text-red-300",
WARN: "text-amber-300",
INFO: "text-muted-foreground",
DEBUG: "text-blue-300/70",
OTHER: "text-muted-foreground/70",
};
function detectLevel(line: string): Level {
if (/\b(ERROR|FATAL|SEVERE|Exception|Traceback)\b/i.test(line)) return "ERROR";
if (/\b(WARN|WARNING)\b/i.test(line)) return "WARN";
if (/\bINFO\b/i.test(line)) return "INFO";
if (/\bDEBUG\b/i.test(line)) return "DEBUG";
return "OTHER";
}
export function LogViewer() {
const logRef = useRef<HTMLDivElement>(null);
const [autoRefresh, setAutoRefresh] = useState(false);
const [lines, setLines] = useState(100);
const [search, setSearch] = useState("");
const [levelFilter, setLevelFilter] = useState<Set<Level>>(
new Set(LEVEL_ORDER)
);
const [autoScroll, setAutoScroll] = useState(true);
const [copied, setCopied] = 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();
},
refetchInterval: autoRefresh ? 3000 : false,
});
const parsed = useMemo(() => {
if (!data?.logs) return [] as { line: string; level: Level }[];
return data.logs
.split("\n")
.filter((l) => l.length > 0)
.map((line) => ({ line, level: detectLevel(line) }));
}, [data?.logs]);
const filtered = useMemo(() => {
const q = search.trim().toLowerCase();
return parsed.filter(
(r) =>
levelFilter.has(r.level) &&
(!q || r.line.toLowerCase().includes(q))
);
}, [parsed, search, levelFilter]);
useEffect(() => {
if (autoScroll && logRef.current && autoScrollRef.current) {
logRef.current.scrollTop = logRef.current.scrollHeight;
}
}, [filtered, autoScroll]);
const handleScroll = () => {
if (!logRef.current) return;
const { scrollTop, scrollHeight, clientHeight } = logRef.current;
autoScrollRef.current = scrollHeight - scrollTop - clientHeight < 80;
};
const toggleLevel = (lvl: Level) => {
setLevelFilter((prev) => {
const next = new Set(prev);
if (next.has(lvl)) next.delete(lvl);
else next.add(lvl);
return next;
});
};
const copyAll = async () => {
try {
await navigator.clipboard.writeText(filtered.map((r) => r.line).join("\n"));
setCopied(true);
setTimeout(() => setCopied(false), 1500);
} catch {}
};
return (
<Card>
<CardHeader>
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2">
<div>
<CardTitle>Console Logs</CardTitle>
<CardDescription>Server output from journalctl</CardDescription>
</div>
<div className="flex items-center gap-2 flex-wrap">
<select
value={lines}
onChange={(e) => {
setLines(Number(e.target.value));
setTimeout(() => refetch(), 50);
}}
aria-label="Number of log lines to fetch"
className="h-9 rounded-md border border-input bg-muted px-2 text-sm text-foreground focus:outline-none"
>
<option value={50}>50 lines</option>
<option value={100}>100 lines</option>
<option value={200}>200 lines</option>
</select>
<Button
size="sm"
variant={autoRefresh ? "default" : "outline"}
onClick={() => {
setAutoRefresh(!autoRefresh);
if (!autoRefresh) autoScrollRef.current = true;
}}
className={`text-xs ${autoRefresh ? "animate-pulse" : ""}`}
>
{autoRefresh ? "Live" : "Auto"}
</Button>
<Button
size="sm"
variant="secondary"
onClick={() => refetch()}
disabled={isFetching}
>
{isFetching ? "..." : "Refresh"}
</Button>
</div>
</div>
<div className="flex flex-wrap items-center gap-2 pt-3">
<Input
placeholder="Search logs..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="h-8 text-sm max-w-[220px]"
/>
<div className="flex gap-1">
{LEVEL_ORDER.map((lvl) => {
const active = levelFilter.has(lvl);
return (
<button
key={lvl}
onClick={() => toggleLevel(lvl)}
aria-pressed={active}
aria-label={`${active ? "Hide" : "Show"} ${lvl} lines`}
className={`text-[10px] font-semibold uppercase rounded px-2 py-1 border transition ${
active
? `${levelClass[lvl]} border-current/40 bg-muted`
: "text-muted-foreground/40 border-transparent hover:text-muted-foreground"
}`}
>
{lvl}
</button>
);
})}
</div>
<label className="flex items-center gap-1.5 text-xs text-muted-foreground ml-auto">
<input
type="checkbox"
checked={autoScroll}
onChange={(e) => setAutoScroll(e.target.checked)}
className="h-3.5 w-3.5 accent-primary"
/>
Auto-scroll
</label>
<Button size="sm" variant="ghost" onClick={copyAll} className="text-xs h-8">
{copied ? "Copied" : "Copy"}
</Button>
</div>
</CardHeader>
<CardContent>
<div
ref={logRef}
onScroll={handleScroll}
className={`rounded-lg border bg-background p-3 sm:p-4 h-[300px] sm:h-[450px] overflow-y-auto font-mono text-xs leading-relaxed ${
isError ? "border-red-500/30" : "border-border"
}`}
>
{isError ? (
<p className="text-red-300">
Failed to load logs:{" "}
{error instanceof Error ? error.message : "unknown error"}
</p>
) : filtered.length === 0 ? (
<p className="text-muted-foreground">
{isFetching ? "Loading logs..." : "No log lines match the current filter."}
</p>
) : (
filtered.map((r, i) => (
<div
key={i}
className={`whitespace-pre-wrap break-all ${levelClass[r.level]}`}
>
{r.line}
</div>
))
)}
</div>
<p className="text-[10px] text-muted-foreground mt-2 tabular-nums">
{filtered.length} / {parsed.length} lines
</p>
</CardContent>
</Card>
);
}