%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/
Upload File :
Create Path :
Current File : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/AdminDashboardPage.tsx

import { useState } from "react";
import { Link } from "react-router-dom";
import Card, { CardBody } from "@/components/ui/Card";
import StatCard from "@/components/dashboard/StatCard";
import { useAdminDashboard } from "@/hooks/useAdminDashboard";
import { formatCompact, formatCredit, formatIDR } from "@/lib/format";
import {
  Bar,
  BarChart,
  CartesianGrid,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";
import type { AdminDailyRow } from "@/types/api";

function AdminChartTooltip({ active, payload }: any) {
  if (!active || !payload?.length) return null;
  const row = payload[0].payload as AdminDailyRow;
  const d = new Date(`${row.date}T00:00:00`);
  const label = Number.isNaN(d.getTime())
    ? row.date
    : new Intl.DateTimeFormat("id-ID", {
        day: "2-digit",
        month: "short",
        year: "numeric",
      }).format(d);
  return (
    <div className="rounded-xl border border-white/[0.08] bg-ink-900 px-3.5 py-2.5 text-xs shadow-modal">
      <p className="mb-1.5 font-semibold text-white">{label}</p>
      <div className="space-y-1">
        {payload.map((p: any) => {
          const v = typeof p.value === "number" ? p.value : Number(p.value);
          return (
            <p
              key={p.dataKey}
              className="flex items-center justify-between gap-6 text-slate-400"
            >
              <span className="flex items-center gap-1.5">
                <span
                  className="h-2 w-2 rounded-full"
                  style={{ background: p.color || p.fill }}
                />
                {p.name}
              </span>
              <span className="font-semibold tabular-nums text-white">
                {Number.isFinite(v) ? v.toLocaleString("id-ID") : String(p.value)}
              </span>
            </p>
          );
        })}
      </div>
    </div>
  );
}

const quickLinks = [
  {
    to: "/admin/users",
    label: "Pengguna",
    desc: "Daftar & kelola akun",
    icon: "M15 19.128a9.38 9.38 0 002.625.372 9.337 9.337 0 004.121-.952 4.125 4.125 0 00-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 018.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0111.964-3.07M12 6.375a3.375 3.375 0 11-6.75 0 3.375 3.375 0 016.75 0zm8.25 2.25a2.625 2.625 0 11-5.25 0 2.625 2.625 0 015.25 0z",
  },
  {
    to: "/admin/usage",
    label: "Penggunaan",
    desc: "Pemakaian token per user",
    icon: "M3 13.125C3 12.504 3.504 12 4.125 12h2.25c.621 0 1.125.504 1.125 1.125v6.75C7.5 20.496 6.996 21 6.375 21h-2.25A1.125 1.125 0 013 19.875v-6.75zM9.75 9.75c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125v10.125c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 01-1.125-1.125V9.75zM16.5 6v0c0-.621.504-1.125 1.125-1.125h2.25C20.496 4.875 21 5.379 21 6v12.75c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 01-1.125-1.125V6z",
  },
  {
    to: "/admin/topup",
    label: "Verifikasi Top Up",
    desc: "Tandai pembayaran QRIS",
    icon: "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",
  },
];

export default function AdminDashboardPage() {
  const [days, setDays] = useState(30);
  const periodLabel = days === 1 ? "hari ini" : `${days} hari`;
  const { data, loading, error } = useAdminDashboard(days);
  const daily = data?.daily ?? [];

  const iconProps = {
    className: "h-5 w-5",
    fill: "none",
    viewBox: "0 0 24 24",
    stroke: "currentColor",
    strokeWidth: 1.7,
  } as const;

  const s = data?.summary;

  return (
    <div className="space-y-6">
      <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">
            Dashboard Admin
          </h2>
          <p className="mt-1 text-sm text-slate-500">
            Ringkasan platform: pengguna, penggunaan, dan pendapatan.
          </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" },
            { v: 90, label: "90 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>

      {error && (
        <div className="rounded-xl border border-red-500/25 bg-red-500/10 px-4 py-3 text-sm text-red-300">
          {error}
        </div>
      )}

      {loading && !s && (
        <div className="grid grid-cols-2 gap-3 sm:gap-4 sm:grid-cols-2 xl:grid-cols-4">
          {[...Array(8)].map((_, i) => (
            <div
              key={i}
              className="h-24 animate-pulse rounded-2xl border border-white/[0.04] bg-white/[0.02] sm:h-28"
            />
          ))}
        </div>
      )}

      {s && (
        <>
          <div className="grid grid-cols-2 gap-3 sm:gap-4 sm:grid-cols-2 xl:grid-cols-3">
            <StatCard
              label="Total Pengguna"
              value={Number(s.total_users)}
              accent="pink"
              hint={`${Number(s.active_users)} aktif`}
              icon={
                <svg {...iconProps}>
                  <path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    d="M15 19.128a9.38 9.38 0 002.625.372 9.337 9.337 0 004.121-.952 4.125 4.125 0 00-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 018.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0111.964-3.07M12 6.375a3.375 3.375 0 11-6.75 0 3.375 3.375 0 016.75 0zm8.25 2.25a2.625 2.625 0 11-5.25 0 2.625 2.625 0 015.25 0z"
                  />
                </svg>
              }
            />
            <StatCard
              label="Requests"
              value={Number(s.requests)}
              accent="fuchsia"
              hint={periodLabel}
              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={Number(s.input_tokens)}
              accent="rose"
              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={Number(s.cached_tokens)}
              accent="hot"
              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={Number(s.output_tokens)}
              accent="pink"
              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>
              }
            />
            <StatCard
              label="Total Token"
              value={
                Number(s.input_tokens) +
                Number(s.cached_tokens) +
                Number(s.output_tokens)
              }
              accent="rose"
              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>
              }
            />
          </div>

          <div className="grid grid-cols-2 gap-3 sm:gap-4 sm:grid-cols-2 xl:grid-cols-4">
            <Card className="p-4 sm:p-5">
              <p className="text-xs font-medium text-slate-400">
                Pendapatan Top Up
              </p>
              <p className="mt-2 text-lg font-bold tracking-tight tabular-nums text-white sm:text-2xl lg:text-3xl">
                {formatIDR(Number(s.revenue_topup))}
              </p>
              <p className="mt-1 text-[11px] text-slate-500">
                Total order berstatus terbayar
              </p>
            </Card>
            <Card className="p-4 sm:p-5">
              <p className="text-xs font-medium text-slate-400">
                Biaya Pemakaian Token
              </p>
              <p className="mt-2 text-lg font-bold tracking-tight tabular-nums text-white sm:text-2xl lg:text-3xl">
                {formatIDR(Number(s.cost_usage))}
              </p>
              <p className="mt-1 text-[11px] text-slate-500">
                {periodLabel} (berdasarkan pricing)
              </p>
            </Card>
            <Card className="p-4 sm:p-5">
              <p className="text-xs font-medium text-slate-400">
                Order Menunggu
              </p>
              <p className="mt-2 text-lg font-bold tracking-tight tabular-nums text-white sm:text-2xl lg:text-3xl">
                {Number(s.pending_orders)}
              </p>
              <p className="mt-1 text-[11px] text-slate-500">
                Perlu verifikasi admin
              </p>
            </Card>
            <Card className="p-4 sm:p-5">
              <p className="text-xs font-medium text-slate-400">
                Total Saldo Wallet
              </p>
              <p className="mt-2 text-lg font-bold tracking-tight tabular-nums text-white sm:text-2xl lg:text-3xl">
                {formatCredit(s.wallet_balance)}
              </p>
              <p className="mt-1 text-[11px] text-slate-500">
                Akumulasi saldo semua pengguna
              </p>
            </Card>
          </div>

          {/* Grafik tren: token harian */}
          <Card>
            <div className="border-b border-white/[0.06] px-5 py-4">
              <p className="text-sm font-semibold text-white">Tren Token Harian</p>
              <p className="mt-0.5 text-xs text-slate-500">
                Komposisi input, cached & output token per hari ({periodLabel})
              </p>
            </div>
            <div className="p-5">
              {daily.length === 0 ? (
                <div className="flex h-64 items-center justify-center text-sm text-slate-500">
                  Belum ada data pada rentang ini.
                </div>
              ) : (
                <>
                  <div className="mb-4 flex items-center gap-5 text-xs text-slate-400">
                    <span className="flex items-center gap-2">
                      <span className="inline-block h-2 w-2 rounded-full bg-blue-500" />
                      Input
                    </span>
                    <span className="flex items-center gap-2">
                      <span className="inline-block h-2 w-2 rounded-full bg-sky-400" />
                      Cached
                    </span>
                    <span className="flex items-center gap-2">
                      <span className="inline-block h-2 w-2 rounded-full bg-blue-300" />
                      Output
                    </span>
                  </div>
                  <div className="h-64 w-full">
                    <ResponsiveContainer width="100%" height="100%">
                      <BarChart
                        data={daily}
                        margin={{ top: 4, right: 4, left: 0, bottom: 0 }}
                        barCategoryGap={0}
                      >
                        <defs>
                          <linearGradient id="admTokIn" x1="0" y1="0" x2="0" y2="1">
                            <stop offset="0%" stopColor="#3b82f6" />
                            <stop offset="100%" stopColor="#1d4ed8" />
                          </linearGradient>
                          <linearGradient id="admTokCached" x1="0" y1="0" x2="0" y2="1">
                            <stop offset="0%" stopColor="#38bdf8" />
                            <stop offset="100%" stopColor="#0284c7" />
                          </linearGradient>
                          <linearGradient id="admTokOut" x1="0" y1="0" x2="0" y2="1">
                            <stop offset="0%" stopColor="#93c5fd" />
                            <stop offset="100%" stopColor="#60a5fa" />
                          </linearGradient>
                        </defs>
                        <CartesianGrid strokeDasharray="3 3" stroke="rgba(255,255,255,0.05)" vertical={false} />
                        <XAxis
                          dataKey="date"
                          tickFormatter={(v: string) => {
                            const d = new Date(`${v}T00:00:00`);
                            return Number.isNaN(d.getTime())
                              ? v
                              : new Intl.DateTimeFormat("id-ID", {
                                  day: "2-digit",
                                  month: "short",
                                }).format(d);
                          }}
                          tick={{ fill: "#475569", fontSize: 10 }}
                          axisLine={false}
                          tickLine={false}
                          minTickGap={24}
                        />
                        <YAxis
                          tick={{ fill: "#475569", fontSize: 11 }}
                          tickFormatter={formatCompact}
                          axisLine={false}
                          tickLine={false}
                        />
                        <Tooltip
                          content={<AdminChartTooltip />}
                          cursor={{ fill: "rgba(255,255,255,0.04)", radius: 6 }}
                        />
                        <Bar dataKey="input_tokens" name="Input" stackId="tok" fill="url(#admTokIn)" radius={[0, 0, 0, 0]} maxBarSize={22} />
                        <Bar dataKey="cached_tokens" name="Cached" stackId="tok" fill="url(#admTokCached)" maxBarSize={22} />
                        <Bar dataKey="output_tokens" name="Output" stackId="tok" fill="url(#admTokOut)" radius={[5, 5, 0, 0]} maxBarSize={22} />
                      </BarChart>
                    </ResponsiveContainer>
                  </div>
                </>
              )}
            </div>
          </Card>

          {/* Grafik tren: pendapatan & request */}
          <Card>
            <div className="border-b border-white/[0.06] px-5 py-4">
              <p className="text-sm font-semibold text-white">
                Pendapatan & Permintaan
              </p>
              <p className="mt-0.5 text-xs text-slate-500">
                Pendapatan top-up vs biaya pemakaian & request per hari ({periodLabel})
              </p>
            </div>
            <div className="p-5">
              {daily.length === 0 ? (
                <div className="flex h-64 items-center justify-center text-sm text-slate-500">
                  Belum ada data pada rentang ini.
                </div>
              ) : (
                <>
                  <div className="mb-4 flex items-center gap-5 text-xs text-slate-400">
                    <span className="flex items-center gap-2">
                      <span className="inline-block h-2 w-2 rounded-full bg-blue-500" />
                      Requests
                    </span>
                    <span className="flex items-center gap-2">
                      <span className="inline-block h-2 w-2 rounded-sm bg-emerald-400" />
                      Pendapatan top-up
                    </span>
                    <span className="flex items-center gap-2">
                      <span className="inline-block h-2 w-2 rounded-sm bg-amber-400" />
                      Biaya pemakaian
                    </span>
                  </div>
                  <div className="h-64 w-full">
                    <ResponsiveContainer width="100%" height="100%">
                      <BarChart
                        data={daily}
                        margin={{ top: 4, right: 4, left: 0, bottom: 0 }}
                        barGap={2}
                        barCategoryGap={0}
                      >
                        <CartesianGrid strokeDasharray="3 3" stroke="rgba(255,255,255,0.05)" vertical={false} />
                        <XAxis
                          dataKey="date"
                          tickFormatter={(v: string) => {
                            const d = new Date(`${v}T00:00:00`);
                            return Number.isNaN(d.getTime())
                              ? v
                              : new Intl.DateTimeFormat("id-ID", {
                                  day: "2-digit",
                                  month: "short",
                                }).format(d);
                          }}
                          tick={{ fill: "#475569", fontSize: 10 }}
                          axisLine={false}
                          tickLine={false}
                          minTickGap={24}
                        />
                        <YAxis
                          yAxisId="req"
                          tick={{ fill: "#475569", fontSize: 11 }}
                          tickFormatter={formatCompact}
                          axisLine={false}
                          tickLine={false}
                        />
                        <YAxis
                          yAxisId="money"
                          orientation="right"
                          tick={{ fill: "#475569", fontSize: 11 }}
                          tickFormatter={(v: number) => formatCompact(v)}
                          axisLine={false}
                          tickLine={false}
                        />
                        <Tooltip
                          content={<AdminChartTooltip />}
                          cursor={{ fill: "rgba(255,255,255,0.04)", radius: 6 }}
                        />
                        <Bar
                          yAxisId="req"
                          dataKey="requests"
                          name="Requests"
                          fill="url(#admTokIn)"
                          radius={[5, 5, 0, 0]}
                          maxBarSize={18}
                        />
                        <Bar
                          yAxisId="money"
                          dataKey="revenue_topup"
                          name="Pendapatan top-up"
                          fill="#34d399"
                          radius={[5, 5, 0, 0]}
                          maxBarSize={18}
                        />
                        <Bar
                          yAxisId="money"
                          dataKey="cost_usage"
                          name="Biaya pemakaian"
                          fill="#fbbf24"
                          radius={[5, 5, 0, 0]}
                          maxBarSize={18}
                        />
                      </BarChart>
                    </ResponsiveContainer>
                  </div>
                </>
              )}
            </div>
          </Card>

          <Card>
            <CardBody>
              <p className="text-sm font-semibold text-white">
                Navigasi cepat
              </p>
              <div className="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-3">
                {quickLinks.map((l) => (
                  <Link
                    key={l.to}
                    to={l.to}
                    className="group flex items-center gap-3 rounded-xl border border-white/[0.06] bg-white/[0.02] p-4 transition hover:bg-white/[0.04] hover:border-white/[0.12]"
                  >
                    <div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg border border-white/[0.06] bg-white/[0.04] text-brand-300">
                      <svg {...iconProps}>
                        <path
                          strokeLinecap="round"
                          strokeLinejoin="round"
                          d={l.icon}
                        />
                      </svg>
                    </div>
                    <div className="min-w-0">
                      <p className="text-sm font-medium text-white group-hover:text-brand-200">
                        {l.label}
                      </p>
                      <p className="truncate text-xs text-slate-500">
                        {l.desc}
                      </p>
                    </div>
                  </Link>
                ))}
              </div>
            </CardBody>
          </Card>
        </>
      )}

      <div className="panel px-5 py-4 text-xs text-slate-500">
        💡 <span className="text-slate-400">Catatan:</span> angka penggunaan
        mengikuti filter periode ({days} hari).{" "}
        <span className="text-slate-400">Pendapatan</span> dan{" "}
        <span className="text-slate-400">saldo wallet</span> adalah nilai
        akumulatif sepanjang waktu.
      </div>
    </div>
  );
}

Zerion Mini Shell 1.0