import { desc } from "drizzle-orm"; import { getTranslations } from "next-intl/server"; import { createApiKey, deleteApiKey, toggleApiKey, } from "@/actions/admin-radio-api-keys"; import { StatusCard } from "@/components/admin/dashboard"; import { Button } from "@/components/ui/button"; import { db, RadioApiKeys } from "@/lib/db"; import { formatDate } from "@/lib/format-date"; export const metadata = { title: "Radio API Keys" }; type ApiKey = { id: bigint; name: string; key: string; allowedIps: string | null; rateLimit: number; lastUsedAt: Date | null; expiresAt: Date | null; isActive: boolean; createdAt: Date | null; }; function maskKey(key: string): string { return key.length > 8 ? `${key.slice(0, 8)}\u2026` : key; } export default async function AdminRadioApiKeysPage() { const t = await getTranslations("pages.admin.radio"); let keys: ApiKey[] = []; let dbError = false; try { keys = await db .select({ id: RadioApiKeys.id, name: RadioApiKeys.name, key: RadioApiKeys.key, allowedIps: RadioApiKeys.allowedIps, rateLimit: RadioApiKeys.rateLimit, lastUsedAt: RadioApiKeys.lastUsedAt, expiresAt: RadioApiKeys.expiresAt, isActive: RadioApiKeys.isActive, createdAt: RadioApiKeys.createdAt, }) .from(RadioApiKeys) .orderBy(desc(RadioApiKeys.isActive), desc(RadioApiKeys.id)) .limit(500) .catch(() => { dbError = true; return []; }); } catch { dbError = true; keys = []; } const total = keys.length; const activeCount = keys.filter((k) => k.isActive).length; const inactiveCount = total - activeCount; return (
{dbError ? (

{t("apiKeysForm.dbError")}

) : (
0 ? "ok" : "neutral"} icon="✅" /> 0 ? "warn" : "neutral"} icon="🚫" />
)}

{t("apiKeysForm.generateKey")}

{t("apiKeysForm.generateHint")}

{t("apiKeysForm.existingKeys", { count: total })}

{total === 0 ? (
{t("apiKeysForm.noKeys")}
) : (
{keys.map((k) => ( ))}
# {t("apiKeysForm.colName")} {t("apiKeysForm.colKey")} {t("apiKeysForm.colAllowedIps")} {t("apiKeysForm.colRateLimit")} {t("apiKeysForm.colLastUsed")} {t("apiKeysForm.colExpires")} {t("apiKeysForm.colStatus")} {t("apiKeysForm.colActions")}
{String(k.id)} {k.name} {maskKey(k.key)} {k.allowedIps || t("apiKeysForm.any")} {k.rateLimit}/min {formatDate(k.lastUsedAt, "datetime", "\u2014")} {formatDate(k.expiresAt, "datetime", "\u2014")} {k.isActive ? t("apiKeysForm.activeLabel") : t("apiKeysForm.inactiveLabel")}
)}
); }