%PDF- %PDF-
Mini Shell

Mini Shell

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

import { useEffect, useState } from "react";
import { Outlet, useLocation, useNavigate, useMatch } from "react-router-dom";
import { useAuth } from "@/contexts/AuthContext";
import { useHealth } from "@/hooks/useHealth";
import Sidebar from "@/components/layout/Sidebar";
import MobileNav from "@/components/layout/MobileNav";

const pageTitles: Record<string, { title: string; subtitle: string }> = {
  "/": { title: "Dashboard", subtitle: "Ringkasan aktivitas gateway" },
  "/keys": { title: "API Keys", subtitle: "Kelola kunci API" },
  "/usage": { title: "Penggunaan", subtitle: "Pemakaian request & token" },
  "/models": {
    title: "Model",
    subtitle: "Katalog & test model di gateway KURNIADEV AI",
  },
  "/docs": {
    title: "API Documentation",
    subtitle: "Panduan integrasi chat completions",
  },
  "/topup": { title: "Top Up", subtitle: "Isi saldo wallet via QRIS" },
  "/profile": {
    title: "Profil",
    subtitle: "Informasi akun & edit profil",
  },
  "/admin": { title: "Dashboard Admin", subtitle: "Ringkasan platform" },
  "/admin/service-status": {
    title: "Service Status",
    subtitle: "Status kuota & koneksi provider",
  },
  "/admin/users": { title: "Pengguna", subtitle: "Daftar & kelola akun" },
  "/admin/analytics": {
    title: "Analisa User",
    subtitle: "Pemakaian, biaya & billing per pengguna",
  },
  "/admin/usage": {
    title: "Penggunaan",
    subtitle: "Pemakaian token per pengguna",
  },
  "/admin/topup": {
    title: "Verifikasi Top Up",
    subtitle: "Konfirmasi pembayaran QRIS",
  },
  "/admin/qris": {
    title: "QRIS",
    subtitle: "Konfigurasi QRIS dinamis untuk top up",
  },
};

function StatusChip() {
  const { online, checking } = useHealth();

  return (
    <div
      className={`hidden items-center gap-2 rounded-full border px-3 py-1.5 text-[11px] font-medium transition-colors sm:flex ${
        online
          ? "border-emerald-400/20 bg-emerald-500/10 text-emerald-300"
          : "border-red-400/20 bg-red-500/10 text-red-300"
      }`}
    >
      <span
        className={`relative flex h-2 w-2 ${
          checking ? "animate-pulse" : ""
        }`}
      >
        <span
          className={`absolute inline-flex h-full w-full rounded-full opacity-60 ${
            online ? "bg-emerald-400" : "bg-red-400"
          }`}
        />
        <span
          className={`relative inline-flex h-2 w-2 rounded-full ${
            online ? "bg-emerald-400" : "bg-red-400"
          }`}
        />
      </span>
      {checking ? "Memeriksa…" : online ? "Layanan online" : "Layanan offline"}
    </div>
  );
}

export default function AppLayout() {
  const { user, logout } = useAuth();
  const navigate = useNavigate();
  const location = useLocation();
  const isDashboard = useMatch("/dashboard");
  const [menuOpen, setMenuOpen] = useState(false);
  const [drawerOpen, setDrawerOpen] = useState(false);

  // Tutup drawer saat pindah halaman.
  useEffect(() => {
    setDrawerOpen(false);
  }, [location.pathname]);

  // Tutup drawer via tombol Escape.
  useEffect(() => {
    if (!drawerOpen) return;
    const onKey = (e: KeyboardEvent) => {
      if (e.key === "Escape") setDrawerOpen(false);
    };
    document.addEventListener("keydown", onKey);
    return () => document.removeEventListener("keydown", onKey);
  }, [drawerOpen]);

  // Kunci scroll body saat drawer terbuka.
  useEffect(() => {
    if (drawerOpen) {
      document.body.style.overflow = "hidden";
      return () => {
        document.body.style.overflow = "";
      };
    }
  }, [drawerOpen]);

  const meta = isDashboard
    ? pageTitles["/"]
    : location.pathname.startsWith("/admin/users/")
      ? { title: "Detail Pengguna", subtitle: "Profil & akses AI" }
      : pageTitles[location.pathname] ?? {
          title: "KURNIADEV AI",
          subtitle: "AI Gateway",
        };

  const goProfile = () => {
    setMenuOpen(false);
    navigate("/profile");
  };

  const handleLogout = async () => {
    setDrawerOpen(false);
    await logout();
    navigate("/login");
  };

  return (
    <div className="relative min-h-screen bg-ink">
      {/* Latar: pattern dot+garis + grain + glow pink halus */}
      <div className="pointer-events-none fixed inset-0 z-0">
        <div className="absolute inset-0 bg-pattern" />
        <div className="absolute inset-0 bg-grain opacity-[0.02]" />
        <div className="absolute inset-0 bg-[radial-gradient(ellipse_55%_40%_at_50%_-10%,rgba(59,130,246,0.07),transparent)]" />
      </div>

      <Sidebar />

      {/* Drawer navigasi mobile (slide dari kiri) */}
      <div
        className={`fixed inset-0 z-40 lg:hidden ${
          drawerOpen ? "" : "pointer-events-none"
        }`}
        aria-hidden={!drawerOpen}
      >
        <div
          onClick={() => setDrawerOpen(false)}
          className={`absolute inset-0 bg-ink-950/70 backdrop-blur-sm transition-opacity duration-200 ${
            drawerOpen ? "opacity-100" : "opacity-0"
          }`}
        />
        <div
          className={`absolute inset-y-0 left-0 flex w-72 max-w-[85vw] flex-col border-r border-white/[0.08] bg-ink-900 shadow-2xl transition-transform duration-200 ease-out ${
            drawerOpen ? "translate-x-0" : "-translate-x-full"
          }`}
        >
          <MobileNav onClose={() => setDrawerOpen(false)} onLogout={handleLogout} />
        </div>
      </div>

      <div className="relative z-10 lg:pl-64">
        {/* Topbar */}
        <header className="sticky top-0 z-20 flex h-16 items-center justify-between border-b border-white/[0.06] bg-ink/80 px-4 backdrop-blur-xl sm:px-8">
          <div className="flex min-w-0 items-center gap-2.5">
            {/* Hamburger — buka drawer nav (mobile saja) */}
            <button
              type="button"
              onClick={() => setDrawerOpen(true)}
              aria-label="Buka menu navigasi"
              className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg border border-white/[0.06] bg-white/[0.03] text-slate-300 transition hover:bg-white/[0.06] hover:text-white lg:hidden"
            >
              <svg
                className="h-5 w-5"
                fill="none"
                viewBox="0 0 24 24"
                stroke="currentColor"
                strokeWidth={1.8}
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5"
                />
              </svg>
            </button>
            <div className="min-w-0">
              <h1 className="truncate text-sm font-semibold text-white sm:text-base">
                {meta.title}
              </h1>
              <p className="hidden truncate text-[11px] text-slate-500 sm:block">
                {meta.subtitle}
              </p>
            </div>
          </div>

          <div className="flex items-center gap-2 sm:gap-3">
            <StatusChip />

            {user && (
              <div className="relative">
                <button
                  onClick={() => setMenuOpen((v) => !v)}
                  aria-label="Menu pengguna"
                  aria-expanded={menuOpen}
                  className="flex items-center gap-2.5 rounded-full border border-white/[0.06] bg-white/[0.03] py-1 pl-1 pr-2.5 transition hover:bg-white/[0.06] sm:pr-3.5"
                >
                  <div className="flex h-7 w-7 items-center justify-center rounded-full bg-ink-700 text-[11px] font-semibold text-white">
                    {user.name.charAt(0).toUpperCase()}
                  </div>
                  <span className="hidden max-w-[120px] truncate text-xs font-medium text-slate-300 sm:block">
                    {user.name}
                  </span>
                  <svg
                    className={`hidden h-3.5 w-3.5 text-slate-500 transition-transform sm:block ${
                      menuOpen ? "rotate-180" : ""
                    }`}
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                    strokeWidth={2}
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      d="M19.5 8.25l-7.5 7.5-7.5-7.5"
                    />
                  </svg>
                </button>

                {menuOpen && (
                  <>
                    <div
                      className="fixed inset-0 z-30"
                      onClick={() => setMenuOpen(false)}
                    />
                    <div className="absolute right-0 top-full z-40 mt-2 w-56 overflow-hidden rounded-xl border border-white/[0.08] bg-ink-800/95 shadow-2xl backdrop-blur-xl">
                      <div className="border-b border-white/[0.06] px-4 py-3">
                        <p className="truncate text-sm font-semibold text-white">
                          {user.name}
                        </p>
                        <p className="truncate text-[11px] text-slate-500">
                          {user.email}
                        </p>
                      </div>
                      <div className="p-1.5">
                        <button
                          onClick={goProfile}
                          className="flex w-full items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-slate-300 transition hover:bg-white/[0.05] hover:text-white"
                        >
                          <svg
                            className="h-4 w-4 text-slate-500"
                            fill="none"
                            viewBox="0 0 24 24"
                            stroke="currentColor"
                            strokeWidth={1.7}
                          >
                            <path
                              strokeLinecap="round"
                              strokeLinejoin="round"
                              d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z"
                            />
                          </svg>
                          Profil
                        </button>
                        <button
                          onClick={handleLogout}
                          className="flex w-full items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-red-300 transition hover:bg-red-500/10"
                        >
                          <svg
                            className="h-4 w-4"
                            fill="none"
                            viewBox="0 0 24 24"
                            stroke="currentColor"
                            strokeWidth={1.7}
                          >
                            <path
                              strokeLinecap="round"
                              strokeLinejoin="round"
                              d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15m3 0l3-3m0 0l-3-3m3 3H9"
                            />
                          </svg>
                          Keluar
                        </button>
                      </div>
                    </div>
                  </>
                )}
              </div>
            )}
          </div>
        </header>

        {/* Konten utama */}
        <main className="px-4 py-5 sm:px-8 sm:py-8">
          <Outlet />
        </main>
      </div>
    </div>
  );
}

Zerion Mini Shell 1.0