import { useState, useEffect, useRef } from "react"; import { Trash2, Copy, CircleCheck, FileDown } from "lucide-react"; import { Button } from "@/components/ui/button"; import { logger, type LogEntry } from "@/lib/logger"; import { ExportFailedLogs } from "../../wailsjs/go/main/App"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; const levelColors: Record = { info: "text-blue-500", success: "text-green-500", warning: "text-yellow-500", error: "text-red-500", debug: "text-gray-500", }; function formatTime(date: Date): string { return date.toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit", second: "2-digit", }); } export function DebugLoggerPage() { const [logs, setLogs] = useState(() => logger.getLogs()); const [copied, setCopied] = useState(false); const scrollRef = useRef(null); const failedLogs = logs.filter((log) => log.level === "error"); useEffect(() => { const unsubscribe = logger.subscribe(() => { setLogs(logger.getLogs()); }); return () => { unsubscribe(); }; }, []); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [logs]); const handleClear = () => { logger.clear(); }; const handleCopy = async () => { const logText = logs .map((log) => `[${formatTime(log.timestamp)}] [${log.level}] ${log.message}`) .join("\n"); try { await navigator.clipboard.writeText(logText); setCopied(true); setTimeout(() => setCopied(false), 500); } catch (err) { console.error("Failed to copy logs:", err); } }; const handleExportFailed = async () => { if (failedLogs.length === 0) { toast.info("No failed logs to export"); return; } const lines = [ `Failed Logs Report - ${new Date().toLocaleString()}`, "-".repeat(50), "", ...failedLogs.map((log) => `[${formatTime(log.timestamp)}] [${log.level}] ${log.message}`), "", ]; try { const path = await ExportFailedLogs(lines.join("\n")); if (path) { toast.success(`Exported ${failedLogs.length} failed log${failedLogs.length === 1 ? "" : "s"}`); } } catch (error) { console.error("Failed to export logs:", error); toast.error(`Failed to export logs: ${error}`); } }; return (

Debug Logs

{logs.length === 0 ? (

no logs yet...

) : (logs.map((log, i) => (
[{formatTime(log.timestamp)}] [{log.level}] {log.message}
)))}
); }