181 lines
6.0 KiB
TypeScript
181 lines
6.0 KiB
TypeScript
import Link from "next/link";
|
|
|
|
import { PageHeader } from "@/components/ui/page-header";
|
|
|
|
import { formatBerlinDateTime } from "@/modules/downloads/format";
|
|
import type { UsageHistoryAttempt, UsageHistoryPage } from "@/modules/downloads/stream";
|
|
|
|
const priceFormatter = new Intl.NumberFormat("de-DE", {
|
|
style: "currency",
|
|
currency: "EUR",
|
|
});
|
|
const byteFormatter = new Intl.NumberFormat("de-DE", {
|
|
style: "unit",
|
|
unit: "byte",
|
|
unitDisplay: "short",
|
|
maximumFractionDigits: 0,
|
|
});
|
|
|
|
export function UsageHistoryContent({ history }: Readonly<{ history: UsageHistoryPage }>) {
|
|
return (
|
|
<section className="editorial-section usage-history" aria-labelledby="usage-history-title">
|
|
<PageHeader
|
|
kicker="Abrechnung · Downloadprotokoll"
|
|
title="Ihre Nutzungen"
|
|
titleId="usage-history-title"
|
|
description="Verwendungszwecke, Kosten und Auslieferungsversuche Ihrer Redaktion."
|
|
/>
|
|
|
|
{history.items.length === 0 ? (
|
|
<div className="usage-history-empty">
|
|
<p>Noch keine Nutzungen erfasst. Wählen Sie ein Foto aus einem freigegebenen Album.</p>
|
|
<Link className="quiet-link" href="/redaktion/alben">
|
|
Zu den Alben
|
|
</Link>
|
|
</div>
|
|
) : (
|
|
<ol className="usage-ledger">
|
|
{history.items.map((usage) => (
|
|
<li key={usage.usageId}>
|
|
<article className="usage-ledger-entry">
|
|
<header>
|
|
<p>{formatBerlinDateTime(usage.createdAt)}</p>
|
|
<h2>{usage.filename}</h2>
|
|
</header>
|
|
<dl className="usage-snapshot">
|
|
<div>
|
|
<dt>Album</dt>
|
|
<dd>{usage.albumName}</dd>
|
|
</div>
|
|
<div>
|
|
<dt>Verwendungszweck</dt>
|
|
<dd>{usage.purpose}</dd>
|
|
</div>
|
|
<div>
|
|
<dt>Nutzungspreis</dt>
|
|
<dd>{priceFormatter.format(usage.priceCents / 100)}</dd>
|
|
</div>
|
|
<div>
|
|
<dt>Abrechnungsstatus</dt>
|
|
<dd>{billingStatusLabel(usage.billingStatus)}</dd>
|
|
</div>
|
|
{usage.reference ? (
|
|
<div>
|
|
<dt>Interne Referenz</dt>
|
|
<dd>{usage.reference}</dd>
|
|
</div>
|
|
) : null}
|
|
</dl>
|
|
</article>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
)}
|
|
|
|
<HistoryPagination history={history} />
|
|
|
|
<section className="usage-attempt-history" aria-labelledby="usage-attempt-title">
|
|
<h2 id="usage-attempt-title">Downloadversuche</h2>
|
|
{history.attempts.length === 0 ? (
|
|
<p className="usage-attempt-empty">Noch keine Downloadversuche erfasst.</p>
|
|
) : (
|
|
<ol className="usage-attempts" aria-label="Downloadversuche Ihrer Redaktion">
|
|
{history.attempts.map((attempt) => (
|
|
<AttemptRow attempt={attempt} key={attempt.downloadId} />
|
|
))}
|
|
</ol>
|
|
)}
|
|
</section>
|
|
<AttemptPagination history={history} />
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function AttemptRow({ attempt }: Readonly<{ attempt: UsageHistoryAttempt }>) {
|
|
const displayedAt = attempt.finishedAt ?? attempt.requestedAt;
|
|
return (
|
|
<li>
|
|
<time dateTime={displayedAt.toISOString()}>
|
|
{formatBerlinDateTime(displayedAt)}
|
|
</time>
|
|
<span className="usage-attempt-context">
|
|
<strong>{attempt.filename}</strong>
|
|
<small>
|
|
{attempt.albumName} · {attempt.purpose}
|
|
{attempt.reference ? ` · ${attempt.reference}` : ""}
|
|
</small>
|
|
</span>
|
|
<strong>{statusLabel(attempt.status)}</strong>
|
|
<span>
|
|
{attempt.repeat ? "Wiederholter Abruf" : "Neue Nutzung"} ·{" "}
|
|
{byteFormatter.format(attempt.transferredBytes)}
|
|
</span>
|
|
<span className="usage-attempt-price">
|
|
<b>{priceFormatter.format(attempt.billableCents / 100)}</b>
|
|
<small>Nutzungspreis {priceFormatter.format(attempt.priceCents / 100)}</small>
|
|
</span>
|
|
</li>
|
|
);
|
|
}
|
|
|
|
function HistoryPagination({ history }: Readonly<{ history: UsageHistoryPage }>) {
|
|
if (history.page === 1 && !history.hasNext) return null;
|
|
return (
|
|
<nav aria-label="Seiten der Nutzungshistorie" className="photo-pagination">
|
|
{history.page > 1 ? (
|
|
<Link href={`/redaktion/nutzungen?page=${history.page - 1}`}>
|
|
Vorherige Nutzungsseite
|
|
</Link>
|
|
) : (
|
|
<span />
|
|
)}
|
|
{history.hasNext ? (
|
|
<Link href={`/redaktion/nutzungen?page=${history.page + 1}`}>
|
|
Nächste Nutzungsseite
|
|
</Link>
|
|
) : null}
|
|
</nav>
|
|
);
|
|
}
|
|
|
|
function AttemptPagination({ history }: Readonly<{ history: UsageHistoryPage }>) {
|
|
if (!history.attemptCursor && !history.nextAttemptCursor) return null;
|
|
return (
|
|
<nav aria-label="Seiten der Auslieferungsversuche" className="photo-pagination">
|
|
{history.attemptCursor ? (
|
|
<Link href={`/redaktion/nutzungen?page=${history.page}`}>Neueste Auslieferungen</Link>
|
|
) : (
|
|
<span />
|
|
)}
|
|
{history.nextAttemptCursor ? (
|
|
<Link
|
|
href={`/redaktion/nutzungen?page=${history.page}&attemptCursor=${encodeURIComponent(history.nextAttemptCursor)}`}
|
|
>
|
|
Ältere Auslieferungen
|
|
</Link>
|
|
) : null}
|
|
</nav>
|
|
);
|
|
}
|
|
|
|
function statusLabel(status: UsageHistoryAttempt["status"]): string {
|
|
const labels: Record<UsageHistoryAttempt["status"], string> = {
|
|
authorized: "Autorisiert",
|
|
streaming: "Wird ausgeliefert",
|
|
completed: "Abgeschlossen",
|
|
interrupted: "Unterbrochen",
|
|
failed_before_delivery: "Vor Auslieferung fehlgeschlagen",
|
|
};
|
|
return labels[status];
|
|
}
|
|
|
|
function billingStatusLabel(status: UsageHistoryPage["items"][number]["billingStatus"]): string {
|
|
const labels: Record<UsageHistoryPage["items"][number]["billingStatus"], string> = {
|
|
open: "Offen",
|
|
billed: "Abgerechnet",
|
|
reopened: "Wieder geöffnet",
|
|
voided: "Storniert",
|
|
};
|
|
return labels[status];
|
|
}
|