import Link from 'next/link'; import { prisma } from '@/lib/prisma'; import { StatusCard } from '@/components/admin/dashboard'; import { Clock } from 'lucide-react'; import { getTranslations } from "next-intl/server"; export const dynamic = 'force-dynamic'; type History = { id: bigint; userId: bigint; showName: string | null; startedAt: Date; endedAt: Date | null; listenersCount: number; notes: string | null; }; function formatDate(d: Date | null): string { return d ? d.toISOString().slice(0, 16).replace('T', ' ') : '\u2014'; } function formatDuration(start: Date, end: Date | null): string { if (!end) return '\u2014'; const ms = end.getTime() - start.getTime(); if (ms <= 0) return '\u2014'; const mins = Math.round(ms / 60000); const h = Math.floor(mins / 60); const m = mins % 60; return h > 0 ? `${h}h ${m}m` : `${m}m`; } export default async function AdminRadioHistoryPage() { const t = await getTranslations("pages.admin.radio"); let history: History[] = []; let names = new Map(); let dbError = false; try { history = await prisma.radioHistory.findMany({ select: { id: true, userId: true, showName: true, startedAt: true, endedAt: true, listenersCount: true, notes: true, }, orderBy: { startedAt: 'desc' }, take: 100, }); const ids = Array.from(new Set(history.map((h) => Number(h.userId)))).filter( (n) => Number.isFinite(n) && n > 0, ); if (ids.length > 0) { const users = await prisma.user.findMany({ where: { id: { in: ids } }, select: { id: true, username: true }, }); names = new Map(users.map((u) => [String(u.id), u.username])); } } catch { dbError = true; history = []; } const liveCount = history.filter((h) => !h.endedAt).length; const peakListeners = history.reduce( (max, h) => (h.listenersCount > max ? h.listenersCount : max), 0, ); const uniqueDjs = new Set(history.map((h) => String(h.userId))).size; return (

{t("historyPage.title")}

{t("historyPage.subtitle")}

{dbError ? (

{t("historyPage.dbError")}

) : (
0 ? 'ok' : 'neutral'} icon="\u{1F534}" />
)} {!dbError && history.length === 0 ? (
{t("historyPage.empty")}
) : null} {history.length > 0 ? (

{t("historyPage.showLog", { count: history.length })}

{history.map((h) => ( ))}
ID {t("historyPage.colDj")} {t("historyPage.colShow")} {t("historyPage.colStarted")} {t("historyPage.colEnded")} {t("historyPage.colAirtime")} {t("historyPage.colListeners")} {t("historyPage.colNotes")}
{String(h.id)} {names.get(String(h.userId)) ?? `#${String(h.userId)}`} {h.showName ?? '\u2014'} {formatDate(h.startedAt)} {h.endedAt ? ( {formatDate(h.endedAt)} ) : ( {t("historyPage.onAir")} )} {formatDuration(h.startedAt, h.endedAt)} {h.listenersCount} {h.notes ?? '\u2014'}
) : null}
); }