Files
Epicnabbo-Catalogus-Updated…/src/app/admin/radio/api-keys/page.tsx
T
openhands 928544100b Convert admin pages to new Tailwind design pattern
- Replace 'card' class with 'admin-card' (gradient cards, dark mode support)
- Replace 'muted' class with consistent Tailwind muted states
- Standardize page headers with gradient backgrounds and Lucide icons
- Convert all inline styles to Tailwind classes
- Add admin-page CSS helpers (admin-card, admin-filter-bar, admin-section-title)
- Apply pattern to all 42 admin pages (was only 6 before)
- Zero TypeScript errors, production builds successfully

Co-authored-by: openhands <[email protected]>
2026-07-02 16:05:35 +02:00

232 lines
8.4 KiB
TypeScript

import Link from "next/link";
import { createApiKey, deleteApiKey, toggleApiKey } from "@/actions/admin-radio-api-keys";
import { StatusCard } from "@/components/admin/dashboard";
import { prisma } from "@/lib/prisma";
import { Key } from "lucide-react";
export const dynamic = "force-dynamic";
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 formatDate(d: Date | null): string {
return d ? d.toISOString().slice(0, 16).replace("T", " ") : "—";
}
/** Show only the first 8 chars of the secret; the rest stays masked. */
function maskKey(key: string): string {
return key.length > 8 ? `${key.slice(0, 8)}…` : key;
}
export default async function AdminRadioApiKeysPage() {
let keys: ApiKey[] = [];
let dbError = false;
try {
keys = await prisma.radioApiKeys
.findMany({
select: {
id: true,
name: true,
key: true,
allowedIps: true,
rateLimit: true,
lastUsedAt: true,
expiresAt: true,
isActive: true,
createdAt: true,
},
orderBy: [{ isActive: "desc" }, { id: "desc" }],
take: 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 (
<main>
<nav className="text-xs text-gray-500 dark:text-gray-400 mb-2">
<Link href="/admin/radio">Radio</Link> ·{" "}
<Link href="/admin/radio/settings">Settings</Link> ·{" "}
<Link href="/admin/radio/api-keys">API Keys</Link> ·{" "}
<Link href="/admin/radio/autodj">AutoDJ</Link> ·{" "}
<Link href="/admin/radio/embed">Embed</Link> ·{" "}
<Link href="/admin/radio/points">Points</Link> ·{" "}
<Link href="/admin/radio/monitoring">Monitoring</Link>
</nav>
<section className="mt-6">
<div className="flex items-center gap-3 mb-6">
<div className="w-10 h-10 rounded-xl bg-gradient-to-br from-[var(--color-primary)]/20 to-[var(--color-primary)]/5 grid place-items-center">
<Key size={20} className="text-[var(--color-primary)]" />
</div>
<div>
<h1 className="m-0 text-xl font-extrabold text-[var(--color-text)]">Radio API Keys</h1>
<p className="m-0 text-xs text-[var(--color-text-muted)] mt-0.5">API keys for external radio integrations (<code>radio_api_keys</code>). Keys are generated server-side and shown masked — copy the full value from the database when first issued.</p>
</div>
</div>
{dbError ? (
<div className="admin-card" style={{ marginTop: "1rem" }}>
<p className="text-xs text-gray-500 dark:text-gray-400 m-0">
Could not load API keys (database unavailable).
</p>
</div>
) : (
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
<StatusCard label="Total keys" value={total} icon="🔑" />
<StatusCard
label="Active"
value={activeCount}
state={activeCount > 0 ? "ok" : "neutral"}
icon="✅"
/>
<StatusCard
label="Inactive"
value={inactiveCount}
state={inactiveCount > 0 ? "warn" : "neutral"}
icon="🚫"
/>
</div>
)}
</section>
<section className="mt-6">
<h2>Generate key</h2>
<p className="text-xs text-gray-500 dark:text-gray-400">
The secret key is generated automatically with cryptographic randomness — you do not enter
it.
</p>
<form action={createApiKey} className="admin-card">
<div className="grid cols-2">
<div>
<label htmlFor="new_name" style={{ display: "block", fontWeight: 700 }}>
Name
</label>
<input
id="new_name"
name="name"
placeholder="AzureCast bridge"
required
maxLength={255}
style={{ width: "100%" }}
/>
</div>
<div>
<label htmlFor="new_rateLimit" style={{ display: "block", fontWeight: 700 }}>
Rate limit (requests/min)
</label>
<input
id="new_rateLimit"
name="rateLimit"
type="number"
min={0}
defaultValue={300}
style={{ width: "100%" }}
/>
</div>
<div style={{ gridColumn: "1 / -1" }}>
<label htmlFor="new_allowedIps" style={{ display: "block", fontWeight: 700 }}>
Allowed IPs (optional)
</label>
<input
id="new_allowedIps"
name="allowedIps"
placeholder="comma-separated, blank = any"
maxLength={255}
style={{ width: "100%" }}
/>
</div>
</div>
<div style={{ marginTop: "0.75rem" }}>
<button type="submit" className="btn btn-primary">
Generate key
</button>
</div>
</form>
</section>
<section className="mt-6">
<h2 className="admin-section-title">Existing keys ({total})</h2>
{total === 0 ? (
<div className="admin-empty">No API keys yet — generate one above.</div>
) : (
<div className="overflow-x-auto">
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Key</th>
<th>Allowed IPs</th>
<th>Rate limit</th>
<th>Last used</th>
<th>Expires</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{keys.map((k) => (
<tr key={String(k.id)}>
<td className="text-sm text-gray-500 dark:text-gray-400 whitespace-nowrap">{String(k.id)}</td>
<td>{k.name}</td>
<td>
<code title="Only the first 8 characters are shown">{maskKey(k.key)}</code>
</td>
<td className="text-sm text-gray-500 dark:text-gray-400 whitespace-nowrap">{k.allowedIps || "any"}</td>
<td className="text-sm text-gray-500 dark:text-gray-400 whitespace-nowrap">{k.rateLimit}/min</td>
<td className="text-sm text-gray-500 dark:text-gray-400 whitespace-nowrap">{formatDate(k.lastUsedAt)}</td>
<td className="text-sm text-gray-500 dark:text-gray-400 whitespace-nowrap">{formatDate(k.expiresAt)}</td>
<td>
<span className={`inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full ${k.isActive ? "bg-green-600/20 text-[var(--color-text)]" : "bg-red-500/22 text-[var(--color-text)]"}`}>
{k.isActive ? "active" : "inactive"}
</span>
</td>
<td>
<div style={{ display: "inline-flex", gap: "0.4rem" }}>
<form action={toggleApiKey}>
<input type="hidden" name="id" value={String(k.id)} />
<button type="submit" className="btn">
{k.isActive ? "Deactivate" : "Activate"}
</button>
</form>
<form action={deleteApiKey}>
<input type="hidden" name="id" value={String(k.id)} />
<button type="submit" className="btn btn-danger">
Delete
</button>
</form>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</section>
</main>
);
}