%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/ |
| Current File : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/DashboardPage.tsx |
import { useState } from "react";
import { Link } from "react-router-dom";
import { useDashboard } from "@/hooks/useDashboard";
import { useWallet } from "@/hooks/useWallet";
import Button from "@/components/ui/Button";
import Card, { CardBody, CardHeader } from "@/components/ui/Card";
import StatCard from "@/components/dashboard/StatCard";
import UsageChart from "@/components/dashboard/UsageChart";
import ActivityTable from "@/components/dashboard/ActivityTable";
import { formatCredit } from "@/lib/format";
function Skeleton({ className = "" }: { className?: string }) {
return (
<div
className={`animate-pulse rounded-2xl border border-white/[0.04] bg-white/[0.02] ${className}`}
/>
);
}
const iconProps = {
className: "h-5 w-5",
fill: "none",
viewBox: "0 0 24 24",
stroke: "currentColor",
strokeWidth: 1.7,
} as const;
export default function DashboardPage() {
const [days, setDays] = useState(30);
const { data, loading, error, refresh } = useDashboard(days);
const { data: wallet } = useWallet();
if (error) {
return (
<Card className="p-8 text-center">
<p className="text-sm text-red-300">Gagal memuat dashboard: {error}</p>
<button
onClick={refresh}
className="mt-4 rounded-xl border border-white/[0.08] bg-white/[0.03] px-4 py-2 text-sm font-medium text-white transition hover:bg-white/[0.06]"
>
Coba lagi
</button>
</Card>
);
}
if (loading || !data) {
return (
<div className="space-y-6">
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
{[...Array(4)].map((_, i) => (
<Skeleton key={i} className="h-28" />
))}
</div>
<Skeleton className="h-80" />
<Skeleton className="h-64" />
</div>
);
}
const { summary, daily, activity } = data;
return (
<div className="space-y-6">
{/* Header halaman */}
<div className="flex flex-wrap items-center justify-between gap-4">
<div>
<h2 className="text-xl font-bold tracking-tight text-white sm:text-2xl">
Halo, {data.account.name.split(" ")[0]} 👋
</h2>
<p className="mt-1 text-sm text-slate-500">
Ringkasan aktivitas gateway{" "}
{days === 1 ? "hari ini" : `${days} hari terakhir`}.
</p>
</div>
<div className="flex items-center gap-1 rounded-xl border border-white/[0.06] bg-white/[0.03] p-1">
{[
{ v: 1, label: "Hari ini" },
{ v: 7, label: "7 hari" },
{ v: 30, label: "30 hari" },
].map((o) => (
<button
key={o.v}
onClick={() => setDays(o.v)}
className={`rounded-lg px-3.5 py-1.5 text-xs font-medium transition-colors ${
days === o.v
? "bg-white/[0.08] text-white"
: "text-slate-500 hover:text-slate-300"
}`}
>
{o.label}
</button>
))}
</div>
</div>
{/* Kartu wallet */}
<Card className="flex flex-wrap items-center justify-between gap-4 p-5">
<div className="flex items-center gap-4">
<div className="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl border border-white/[0.06] bg-white/[0.04] text-brand-300">
<svg {...iconProps} className="h-6 w-6">
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.25 8.25h19.5m-16.5.75h12a2.25 2.25 0 012.25 2.25v3a2.25 2.25 0 01-2.25 2.25h-12A2.25 2.25 0 012.25 14.25v-3a2.25 2.25 0 012.25-2.25z"
/>
</svg>
</div>
<div>
<p className="text-xs font-medium text-slate-400">Saldo Wallet</p>
<p className="mt-0.5 text-2xl font-bold tracking-tight tabular-nums text-white">
{wallet ? formatCredit(wallet.wallet.balance) : "—"}
</p>
</div>
</div>
<Link to="/topup">
<Button>
<svg
className="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2.2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 4.5v15m7.5-7.5h-15"
/>
</svg>
Top Up
</Button>
</Link>
</Card>
{/* Kartu statistik */}
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
<StatCard
label="Total Requests"
value={summary.requests}
accent="pink"
hint="Permintaan API"
icon={
<svg {...iconProps}>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M8.25 6.75h12M8.25 12h12m-12 5.25h12M3.75 6.75h.007v.008H3.75V6.75zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM3.75 12h.007v.008H3.75V12zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm-.375 5.25h.007v.008H3.75v-.008zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
}
/>
<StatCard
label="Input Tokens"
value={summary.input_tokens}
accent="fuchsia"
icon={
<svg {...iconProps}>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.813 15.904L9 18.75l-.813-2.846a4.5 4.5 0 00-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 003.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 003.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 00-3.09 3.09zM18.259 8.715L18 9.75l-.259-1.035a3.375 3.375 0 00-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 002.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 002.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 00-2.456 2.456z"
/>
</svg>
}
/>
<StatCard
label="Cached Tokens"
value={summary.cached_tokens}
accent="rose"
icon={
<svg {...iconProps}>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7l4.029-1.523a.75.75 0 01.93.539v.011a.75.75 0 01-.539.93l-4.029 1.523a8.25 8.25 0 01-13.803 3.7l-3.182-3.182v-.001z"
/>
</svg>
}
/>
<StatCard
label="Output Tokens"
value={summary.output_tokens}
accent="hot"
icon={
<svg {...iconProps}>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M3.75 13.5l10.5-11.25L12 10.5h8.25L9.75 21.75 12 13.5H3.75z"
/>
</svg>
}
/>
</div>
{/* Grafik tren */}
<Card>
<CardHeader
title="Tren Penggunaan Harian"
subtitle="Requests vs output tokens per hari"
/>
<CardBody>
<UsageChart data={daily} />
</CardBody>
</Card>
{/* Aktivitas terbaru */}
<Card>
<CardHeader
title="Aktivitas Terbaru"
subtitle="20 request terakhir menggunakan API key Anda"
/>
<CardBody className="p-0">
<ActivityTable items={activity} />
</CardBody>
</Card>
</div>
);
}