%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/AdminModelsPage.tsx |
import { useState } from "react";
import Button from "@/components/ui/Button";
import Card, { CardBody, CardHeader } from "@/components/ui/Card";
import Badge from "@/components/ui/Badge";
import Modal from "@/components/ui/Modal";
import Input from "@/components/ui/Input";
import { useAdminModels } from "@/hooks/useAdminModels";
import { formatCompact, normalizePriceInput } from "@/lib/format";
import type { AdminModelRow } from "@/types/api";
function priceIDR(v: string | number): string {
const n = Number(v);
if (!Number.isFinite(n) || n === 0) return "0";
return `${n.toLocaleString("id-ID")}`;
}
type ForcedVisibility = boolean | null;
function forcedValue(m: AdminModelRow): ForcedVisibility {
if (Number(m.forced_visibility) === 1) return true;
if (Number(m.forced_visibility) === 0) return false;
return null;
}
/** Terlihat/tidak oleh pengguna akhir (paksaan menang atas is_hidden/status). */
function isEffectivelyHidden(m: AdminModelRow): boolean {
const f = forcedValue(m);
if (f === true) return false;
if (f === false) return true;
return Number(m.is_hidden) === 1;
}
/** Kontrol visibilitas 3 kondisi: Otomatis / Paksa Tampil / Paksa Sembunyi. */
function VisibilityControl({
value,
onChange,
disabled,
}: {
value: ForcedVisibility;
onChange: (v: ForcedVisibility) => void;
disabled?: boolean;
}) {
const options: { v: ForcedVisibility; label: string; cls: string }[] = [
{ v: null, label: "Otomatis", cls: "bg-white/[0.08] text-slate-200" },
{ v: true, label: "Paksa Tampil", cls: "bg-emerald-500/20 text-emerald-300" },
{ v: false, label: "Paksa Sembunyi", cls: "bg-red-500/20 text-red-300" },
];
return (
<div className="inline-flex items-center gap-1 rounded-lg border border-white/[0.06] bg-white/[0.02] p-1">
{options.map((o) => (
<button
key={String(o.v)}
type="button"
disabled={disabled}
onClick={() => onChange(o.v)}
className={`rounded-md px-2 py-1 text-[10px] font-semibold whitespace-nowrap transition disabled:opacity-40 ${
value === o.v ? o.cls : "text-slate-500 hover:text-slate-300"
}`}
>
{o.label}
</button>
))}
</div>
);
}
export default function AdminModelsPage() {
const { models, loading, error, patch } = useAdminModels();
const [q, setQ] = useState("");
const [editing, setEditing] = useState<AdminModelRow | null>(null);
const [form, setForm] = useState({ inp: "", out: "", cache: "" });
const [saving, setSaving] = useState(false);
const [formError, setFormError] = useState<string | null>(null);
const [working, setWorking] = useState<string | null>(null);
const filtered = models.filter(
(m) =>
!q.trim() ||
m.model.toLowerCase().includes(q.trim().toLowerCase()),
);
const openEdit = (m: AdminModelRow) => {
setEditing(m);
setForm({
inp: normalizePriceInput(m.input_price_per_1m),
out: normalizePriceInput(m.output_price_per_1m),
cache: normalizePriceInput(m.cache_price_per_1m),
});
setFormError(null);
};
const handleSave = async () => {
const inp = Number(form.inp);
const out = Number(form.out);
const cache = Number(form.cache);
if (
!Number.isFinite(inp) || inp < 0 ||
!Number.isFinite(out) || out < 0 ||
!Number.isFinite(cache) || cache < 0
) {
setFormError("Harga harus angka ≥ 0.");
return;
}
if (!editing) return;
setSaving(true);
setFormError(null);
try {
await patch(editing.model, {
input_price_per_1m: String(inp),
output_price_per_1m: String(out),
cache_price_per_1m: String(cache),
});
setEditing(null);
} catch (e) {
setFormError(e instanceof Error ? e.message : "Gagal menyimpan harga");
} finally {
setSaving(false);
}
};
const setForced = async (m: AdminModelRow, v: ForcedVisibility) => {
setWorking(m.model);
try {
await patch(m.model, { forced_visibility: v });
} catch {
/* abaikan */
} finally {
setWorking(null);
}
};
return (
<>
<Card>
<CardHeader
title="Katalog Model"
subtitle="Kelola harga & visibilitas (paksa tampil/sembunyi; status otomatis dari health check)"
action={
<input
type="search"
placeholder="Cari model…"
value={q}
onChange={(e) => setQ(e.target.value)}
className="input-solid w-full sm:w-56"
/>
}
/>
<CardBody className="p-0">
{error && (
<div className="p-5 text-sm text-red-300">Gagal memuat: {error}</div>
)}
{loading ? (
<div className="space-y-3 p-5">
{[...Array(5)].map((_, i) => (
<div
key={i}
className="h-12 animate-pulse rounded-xl bg-white/[0.02]"
/>
))}
</div>
) : filtered.length === 0 ? (
<div className="px-5 py-10 text-center text-sm text-slate-500">
Tidak ada model yang cocok.
</div>
) : (
<>
{/* ── Mobile: kartu model ── */}
<div className="divide-y divide-white/[0.04] md:hidden">
{filtered.map((m) => (
<div
key={m.model}
className={`p-4 ${isEffectivelyHidden(m) ? "opacity-60" : ""}`}
>
<div className="flex items-start justify-between gap-2">
<div className="flex min-w-0 flex-wrap items-center gap-2">
<span className="font-mono text-sm font-medium text-slate-100">
{m.model}
</span>
</div>
<div className="flex shrink-0 items-center gap-1.5">
{m.status === "maintenance" ? (
<Badge tone="danger">Maintenance</Badge>
) : m.status === "active" ? (
<Badge tone="success">Aktif</Badge>
) : (
<Badge tone="default">{m.status}</Badge>
)}
{forcedValue(m) === true && (
<Badge tone="info">Paksa tampil</Badge>
)}
{forcedValue(m) === false && (
<Badge tone="danger">Paksa sembunyi</Badge>
)}
</div>
</div>
<div className="mt-2 grid grid-cols-3 gap-2 text-xs">
<div className="rounded-lg border border-white/[0.06] bg-white/[0.02] px-2.5 py-1.5">
<p className="text-[10px] text-slate-500">Input/1M</p>
<p className="tabular-nums text-slate-300">
{priceIDR(m.input_price_per_1m)}
</p>
</div>
<div className="rounded-lg border border-white/[0.06] bg-white/[0.02] px-2.5 py-1.5">
<p className="text-[10px] text-slate-500">Output/1M</p>
<p className="tabular-nums text-slate-300">
{priceIDR(m.output_price_per_1m)}
</p>
</div>
<div className="rounded-lg border border-white/[0.06] bg-white/[0.02] px-2.5 py-1.5">
<p className="text-[10px] text-slate-500">Cache/1M</p>
<p className="tabular-nums text-slate-400">
{priceIDR(m.cache_price_per_1m)}
</p>
</div>
</div>
<div className="mt-2 flex items-center gap-3 text-[11px] text-slate-500">
<span>
Konteks{" "}
<span className="tabular-nums text-slate-400">
{m.context_window
? formatCompact(m.context_window)
: "—"}
</span>
</span>
<span>
Uptime{" "}
<span className="tabular-nums text-slate-400">
{Number(m.uptime).toLocaleString("id-ID")}%
</span>
</span>
</div>
<div className="mt-3 flex flex-col items-start gap-2 sm:flex-row sm:items-center sm:justify-between">
<VisibilityControl
value={forcedValue(m)}
disabled={working === m.model}
onChange={(v) => setForced(m, v)}
/>
<Button
variant="ghost"
size="sm"
onClick={() => openEdit(m)}
>
Edit Harga
</Button>
</div>
</div>
))}
</div>
{/* ── Desktop: tabel ── */}
<div className="hidden overflow-x-auto md:block">
<table className="w-full min-w-[1180px] text-left text-sm">
<thead>
<tr className="border-b border-white/[0.06] bg-white/[0.02] text-[11px] uppercase tracking-wider text-slate-500">
<th className="px-5 py-3 font-semibold">Model</th>
<th className="px-4 py-3 text-right font-semibold">
Input /1M
</th>
<th className="px-4 py-3 text-right font-semibold">
Output /1M
</th>
<th className="px-4 py-3 text-right font-semibold">
Cache /1M
</th>
<th className="px-4 py-3 text-right font-semibold">
Konteks
</th>
<th className="px-4 py-3 text-right font-semibold">
Uptime
</th>
<th className="px-4 py-3 font-semibold">Status</th>
<th className="px-4 py-3 font-semibold">Visibilitas</th>
<th className="px-5 py-3 text-right font-semibold">Aksi</th>
</tr>
</thead>
<tbody>
{filtered.map((m) => (
<tr
key={m.model}
className={`border-b border-white/[0.04] transition-colors last:border-0 hover:bg-white/[0.02] ${
isEffectivelyHidden(m) ? "opacity-60" : ""
}`}
>
<td className="px-5 py-3.5">
<div className="flex items-center gap-2.5">
<span className="font-mono text-sm font-medium text-slate-100">
{m.model}
</span>
</div>
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-300">
{priceIDR(m.input_price_per_1m)}
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-300">
{priceIDR(m.output_price_per_1m)}
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-400">
{priceIDR(m.cache_price_per_1m)}
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-400">
{m.context_window ? formatCompact(m.context_window) : "—"}
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-400">
{Number(m.uptime).toLocaleString("id-ID")}%
</td>
<td className="px-4 py-3.5">
<div className="flex flex-col items-start gap-1.5">
{m.status === "maintenance" ? (
<Badge tone="danger">Maintenance</Badge>
) : m.status === "active" ? (
<Badge tone="success">Aktif</Badge>
) : (
<Badge tone="default">{m.status}</Badge>
)}
<VisibilityControl
value={forcedValue(m)}
disabled={working === m.model}
onChange={(v) => setForced(m, v)}
/>
</div>
</td>
<td className="px-5 py-3.5 text-right">
<Button
variant="ghost"
size="sm"
onClick={() => openEdit(m)}
>
Edit Harga
</Button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</>
)}
</CardBody>
</Card>
<Modal
open={Boolean(editing)}
onClose={() => setEditing(null)}
title="Edit Harga Model"
subtitle={editing ? editing.model : ""}
>
<div className="space-y-4">
<Input
label="Input (IDR / 1M token)"
type="number"
min={0}
step={100}
value={form.inp}
onChange={(e) => setForm({ ...form, inp: e.target.value })}
/>
<Input
label="Output (IDR / 1M token)"
type="number"
min={0}
step={100}
value={form.out}
onChange={(e) => setForm({ ...form, out: e.target.value })}
/>
<Input
label="Cache (IDR / 1M token)"
type="number"
min={0}
step={100}
value={form.cache}
onChange={(e) => setForm({ ...form, cache: e.target.value })}
/>
{formError && (
<div className="rounded-xl border border-red-500/25 bg-red-500/10 px-4 py-3 text-sm text-red-300">
{formError}
</div>
)}
<div className="flex justify-end gap-2 pt-1">
<Button variant="ghost" onClick={() => setEditing(null)}>
Batal
</Button>
<Button isLoading={saving} onClick={handleSave}>
Simpan Harga
</Button>
</div>
</div>
</Modal>
</>
);
}