import { desc, inArray } from "drizzle-orm"; import { getTranslations } from "next-intl/server"; import { StatusCard } from "@/components/admin/dashboard"; import { db, RadioHistory, User } from "@/lib/db"; import { formatDate } from "@/lib/format-date"; type History = { id: bigint; userId: bigint; showName: string | null; startedAt: Date; endedAt: Date | null; listenersCount: number; notes: string | null; }; 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 db .select({ id: RadioHistory.id, userId: RadioHistory.userId, showName: RadioHistory.showName, startedAt: RadioHistory.startedAt, endedAt: RadioHistory.endedAt, listenersCount: RadioHistory.listenersCount, notes: RadioHistory.notes, }) .from(RadioHistory) .orderBy(desc(RadioHistory.startedAt)) .limit(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 db .select({ id: User.id, username: User.username }) .from(User) .where(inArray(User.id, ids)); 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 (
{dbError ? (

{t("historyPage.dbError")}

) : (
0 ? "ok" : "neutral"} icon="🔴" />
)} {!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, "datetime", "\u2014")} {h.endedAt ? ( {formatDate(h.endedAt, "datetime", "\u2014")} ) : ( {t("historyPage.onAir")} )} {formatDuration(h.startedAt, h.endedAt)} {h.listenersCount} {h.notes ?? "\u2014"}
) : null}
); }