"use client";
import { Card, CardContent, CardHeader, CardTitle } from "@kandev/ui/card";
import { Button } from "@kandev/ui/button";
import { Spinner } from "@kandev/ui/spinner";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table";
import { Badge } from "@kandev/ui/badge";
import { IconCopy, IconDownload, IconFileText, IconRefresh } from "@tabler/icons-react";
import { useLogFiles } from "@/hooks/domains/system/use-log-files";
import { useLogTail } from "@/hooks/domains/system/use-log-tail";
import { buildLogDownloadUrl } from "@/lib/api/domains/system-api";
import { formatBytes } from "@/lib/utils/format-bytes";
import { useActionFeedback, type ActionFeedbackState } from "@/hooks/use-action-feedback";
import { ActionButtonContent } from "./action-button-content";
function formatTimestamp(iso: string): string {
if (!iso) return "-";
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return iso;
return d.toLocaleString();
}
function TailContent({
tail,
loading,
inMemoryOnly,
}: {
tail: string[];
loading: boolean;
inMemoryOnly: boolean;
}) {
if (loading && tail.length === 0) {
return (
Loading log...
);
}
if (tail.length === 0) {
return (
No recent log activity captured yet. Logs will appear as Kandev does work.
);
}
return (
{inMemoryOnly && (
Showing the in-memory log buffer (last ~2000 entries). Kandev is currently logging to the
terminal, not to a file - file rotation is disabled. Set logging.outputPath{" "}
in config.yaml to a file path to enable downloadable log files.
)}
{tail.join("\n")}
);
}
function buildTailFilename(): string {
// YYYY-MM-DDTHH-MM-SS to mirror lumberjack's rotation naming, minus the
// colons (which are invalid on Windows). The user can rename freely.
const stamp = new Date().toISOString().replace(/:/g, "-").replace(/\..+$/, "");
return `kandev-tail-${stamp}.log`;
}
function downloadTailAsFile(tail: string[]) {
if (typeof window === "undefined" || tail.length === 0) return;
const blob = new Blob([tail.join("\n") + "\n"], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = buildTailFilename();
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
function TailHeader({
tail,
current,
refreshState,
copyState,
onRefresh,
onCopy,
}: {
tail: string[];
current: ReturnType["files"][number] | undefined;
refreshState: ActionFeedbackState;
copyState: ActionFeedbackState;
onRefresh: () => void;
onCopy: () => void;
}) {
const hasTail = tail.length > 0;
return (