"use client"; import { useEffect, useMemo, useState } from "react"; import { PageHead } from "../../_ui/page-head"; import { CoverArt } from "../../_ui/cover-art"; import { AlbumModal } from "../../_ui/album-modal"; import { categoryOf, TAB_ORDER, type Category } from "../../_ui/categories"; type Release = { id: string; rgMbid: string | null; album: string; primaryType: string | null; secondaryTypes: string[]; firstReleaseDate: string | null; monitored: boolean; state: string; currentQualityClass: number | null; }; type Detail = { id: string; name: string; autoMonitorFuture: boolean; showAllTypes: boolean; releases: Release[] }; function badge(r: Release): { label: string; cls: string } { if (r.currentQualityClass !== null) return { label: `Have q${r.currentQualityClass}`, cls: "chip done" }; if (r.monitored) return { label: r.state === "wanted" ? "Wanted" : r.state, cls: "chip working" }; return { label: "Dormant", cls: "chip" }; } export function DiscographyClient({ id }: { id: string }) { const [detail, setDetail] = useState(null); const [tab, setTab] = useState<"All" | Category>("Albums"); const [openAlbum, setOpenAlbum] = useState(null); async function refresh() { const res = await fetch(`/api/artists/${id}?all=true`); setDetail(res.ok ? await res.json() : null); } useEffect(() => { refresh(); }, [id]); async function toggleMonitor(r: Release) { await fetch(`/api/releases/${r.id}`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ monitored: !r.monitored }), }); refresh(); } async function searchNow(r: Release) { await fetch(`/api/releases/${r.id}/search`, { method: "POST" }); refresh(); } const counts = useMemo(() => { const c: Record = {}; detail?.releases.forEach((r) => { c[categoryOf(r)] = (c[categoryOf(r)] ?? 0) + 1; }); return c; }, [detail]); if (!detail) return

Loading…

; const tabs: ("All" | Category)[] = ["All", ...TAB_ORDER.filter((t) => counts[t])]; const visible = tab === "All" ? detail.releases : detail.releases.filter((r) => categoryOf(r) === tab); return (
{tabs.map((t) => ( ))}
    {visible.map((r) => { const b = badge(r); return (
  • {b.label}
  • ); })}
{openAlbum ? ( setOpenAlbum(null)} album={{ rgMbid: openAlbum.rgMbid, album: openAlbum.album, artist: detail.name, year: openAlbum.firstReleaseDate?.slice(0, 4) ?? null, type: openAlbum.primaryType, }} status={{badge(openAlbum).label}} actions={ } /> ) : null}
); }