"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { ScrollArea } from "@/components/ui/scroll-area" import { RefreshCw, Trash2, Server, FileText, Database, CheckCircle, XCircle, Loader2 } from "lucide-react" import { getApiBaseUrl } from "../../lib/utils"; import { Toaster, toast } from "sonner"; interface CacheStatus { files: number; status: string; total_size: number; } interface LogEntry { time: string; level: string; msg: string; attrs?: Record; } export default function AdminPage() { const [cacheStatus, setCacheStatus] = useState(null) const [logs, setLogs] = useState([]) const [isLoading, setIsLoading] = useState(false) const [isClearingCache, setIsClearingCache] = useState(false) const [status, setStatus] = useState<{ type: "success" | "error" | "info" | null message: string }>({ type: null, message: "" }) const fetchCacheStatus = async () => { try { const response = await fetch(`${getApiBaseUrl()}/d/cache/status`) if (response.ok) { const data = await response.json() setCacheStatus(data) } else { toast.error("Failed to fetch cache status") } } catch (error) { toast.error("Network error while fetching cache status") } } const fetchLogs = async () => { try { const response = await fetch(`${getApiBaseUrl()}/d/logs`) if (response.ok) { const data = await response.json() setLogs(data.logs || []) } else { toast.error("Failed to fetch logs") } } catch (error) { toast.error("Network error while fetching logs") } } const refreshData = async () => { setIsLoading(true) try { await Promise.all([fetchCacheStatus(), fetchLogs()]) toast.success("Data refreshed successfully") } catch (error) { toast.error("Failed to refresh data") } finally { setIsLoading(false) } } const clearCache = async () => { setIsClearingCache(true) toast.loading("Clearing cache...") try { const response = await fetch(`${getApiBaseUrl()}/d/cache/delete`, { method: "DELETE" }) if (response.ok) { toast.success("Cache cleared successfully") await fetchCacheStatus() } else { const errorData = await response.json() toast.error(errorData.error || "Failed to clear cache") } } catch (error) { toast.error("Network error while clearing cache") } finally { setIsClearingCache(false) } } const formatFileSize = (bytes: number) => { if (!bytes) return "0 Bytes" const k = 1024 const sizes = ["Bytes", "KB", "MB", "GB"] const i = Math.floor(Math.log(bytes) / Math.log(k)) return Number.parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i] } const getLogLevelColor = (level: string) => { switch (level.toLowerCase()) { case "error": return "bg-red-100/80 text-red-800 border-red-200/60" case "warn": return "bg-amber-100/80 text-amber-800 border-amber-200/60" case "info": return "bg-blue-100/80 text-blue-800 border-blue-200/60" default: return "bg-slate-100/80 text-slate-800 border-slate-200/60" } } useEffect(() => { refreshData() }, []) return (
{/* Header */}

Admin Dashboard

Manage cache and monitor system logs

{/* Status Message */} {status.type && (
{status.type === "success" && } {status.type === "error" && } {status.type === "info" && } {status.message}
)}
{/* Cache Status */}
Cache Status
Current cache information and management
{cacheStatus ? (
{formatFileSize(cacheStatus.total_size)}
Cache Size
{cacheStatus.files}
Files
Status {cacheStatus.status}
) : (
No cache data available.
)}
{/* Logs */}
Logs
{logs.length > 0 ? (
    {logs.map((log, idx) => (
  • {new Date(log.time).toLocaleString()} {log.level}
    {log.msg}
    {log.attrs && (
    {JSON.stringify(log.attrs, null, 2)}
    )}
  • ))}
) : (
No logs available.
)}
) }