"use client"; import { useState, useEffect } from "react"; import { getAiAssistantAuth, aiAssistantApi } from "@/lib/api/aiAssistant"; import type { UsageSummary, ApiKeyItem, AdminStats } from "@/lib/api/aiAssistant"; import { BarChart3, Key, Zap, TrendingUp, Loader2, AlertCircle, Users, Cpu } from "lucide-react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Progress } from "@/components/ui/progress"; import { toast } from "sonner"; export default function AiAssistantDashboardPage() { const auth = getAiAssistantAuth(); const user = auth?.user; const isAdmin = user?.role === "admin"; const [loading, setLoading] = useState(true); const [usage, setUsage] = useState(null); const [apiKeys, setApiKeys] = useState([]); const [stats, setStats] = useState(null); useEffect(() => { loadData(); }, []); const loadData = async () => { setLoading(true); try { const usageRes = await aiAssistantApi.get("/usage"); setUsage(usageRes.data?.data ?? null); const keysRes = await aiAssistantApi.get("/api-keys"); setApiKeys(keysRes.data?.data ?? []); if (isAdmin) { const statsRes = await aiAssistantApi.get("/admin/stats"); setStats(statsRes.data?.data ?? null); } } catch { toast.error("데이터를 불러오는데 실패했습니다."); } finally { setLoading(false); } }; if (loading) { return (
); } const monthlyTokens = usage?.usage?.monthly?.totalTokens ?? 0; const monthlyLimit = usage?.limit?.monthly ?? 0; const usagePercent = monthlyLimit > 0 ? Math.round((monthlyTokens / monthlyLimit) * 100) : 0; return (

대시보드

안녕하세요, {user?.name || user?.email}님!

오늘 사용량
{(usage?.usage?.today?.tokens ?? 0).toLocaleString()}

토큰

이번 달 사용량
{monthlyTokens.toLocaleString()}

/ {monthlyLimit.toLocaleString()} 토큰

{usagePercent}% 사용

오늘 요청 수
{(usage?.usage?.today?.requests ?? 0).toLocaleString()}

활성 API 키
{apiKeys.filter((k) => k.status === "active").length}

{isAdmin && stats && ( 시스템 현황 전체 시스템 통계
전체 사용자

{stats.users?.total ?? 0}

활성 사용자

{stats.users?.active ?? 0}

전체 API 키

{stats.apiKeys?.total ?? 0}

활성 프로바이더

{stats.providers?.active ?? 0}

)} 내 API 키 발급받은 API 키 목록 {apiKeys.length === 0 ? (

API 키가 없습니다.

새 키를 발급받으세요.

) : (
{apiKeys.slice(0, 5).map((key) => (

{key.name}

{key.keyPrefix}...

{key.status === "active" ? "활성" : "비활성"}
))}
)}
); }