feat: stream originals with download accounting

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 13:10:13 +02:00
1 parent c400167b8f
commit 8871e4794a
28 files changed
+4081 -8

No files matched your search

+7 -1
View File
@@ -3,6 +3,7 @@ import { notFound } from "next/navigation";
import { AlbumCatalogError, albumCatalog } from "@/modules/albums/catalog";
import { PhotoContactSheet } from "@/modules/albums/catalog-content";
import { loadEditorialDownloadOptions } from "@/modules/downloads/stream";
import { requireEditorial } from "../../access";
@@ -44,6 +45,7 @@ export default async function AlbumPage({ params, searchParams }: AlbumPageProps
<PhotoContactSheet
albumId={result.album.id}
albumName={result.album.albumName}
downloadOptions={result.downloadOptions}
page={result.page}
query={query}
/>
@@ -73,7 +75,11 @@ async function loadAlbumPage({
description: query,
page,
});
return { album, page: results, unavailable: false as const };
const downloadOptions = await loadEditorialDownloadOptions(
editorialId,
results.items.map((asset) => asset.id),
);
return { album, page: results, downloadOptions, unavailable: false as const };
} catch (error) {
if (error instanceof AlbumCatalogError && error.code === "IMMICH_UNAVAILABLE") {
return { album: null, page: null, unavailable: true as const };
+84
View File
@@ -0,0 +1,84 @@
// @vitest-environment jsdom
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, expect, test } from "vitest";
import { UsageHistoryContent } from "./usage-history-content";
afterEach(cleanup);
test("renders every terminal attempt with its immutable usage snapshot", () => {
render(
<UsageHistoryContent
history={{
items: [
{
usageId: "usage-1",
filename: "tor-des-monats.jpg",
albumName: "Heimspiel gegen Nord",
purpose: "Titelgeschichte",
reference: "Ausgabe 8/26",
createdAt: new Date("2026-08-09T10:00:00.000Z"),
billingStatus: "open",
billableCents: 1_500,
attempts: [
{
downloadId: "download-3",
status: "failed_before_delivery",
requestedAt: new Date("2026-08-10T12:00:00.000Z"),
finishedAt: new Date("2026-08-10T12:00:01.000Z"),
transferredBytes: 0,
repeat: true,
billableCents: 0,
},
{
downloadId: "download-2",
status: "interrupted",
requestedAt: new Date("2026-08-10T11:00:00.000Z"),
finishedAt: new Date("2026-08-10T11:00:02.000Z"),
transferredBytes: 40,
repeat: true,
billableCents: 0,
},
{
downloadId: "download-1",
status: "completed",
requestedAt: new Date("2026-08-09T10:00:00.000Z"),
finishedAt: new Date("2026-08-09T10:00:03.000Z"),
transferredBytes: 100,
repeat: false,
billableCents: 1_500,
},
],
},
],
page: 1,
pageSize: 20,
hasNext: true,
}}
/>,
);
expect(screen.getByRole("heading", { name: /tor-des-monats\.jpg/i })).not.toBeNull();
expect(screen.getByText("Heimspiel gegen Nord")).not.toBeNull();
expect(screen.getByText("Titelgeschichte")).not.toBeNull();
expect(screen.getByText("Abgeschlossen")).not.toBeNull();
expect(screen.getByText("Unterbrochen")).not.toBeNull();
expect(screen.getByText("Vor Auslieferung fehlgeschlagen")).not.toBeNull();
expect(screen.getByText(/15,00\s*€/)).not.toBeNull();
expect(screen.getAllByText(/0,00\s*€/)).toHaveLength(2);
expect(screen.getByRole("link", { name: /nächste seite/i }).getAttribute("href")).toBe(
"/redaktion/nutzungen?page=2",
);
});
test("renders a directional empty state", () => {
render(
<UsageHistoryContent
history={{ items: [], page: 1, pageSize: 20, hasNext: false }}
/>,
);
expect(screen.getByText(/noch keine nutzungen/i)).not.toBeNull();
expect(screen.getByRole("link", { name: /zu den alben/i })).not.toBeNull();
});
+40
View File
@@ -0,0 +1,40 @@
import { listEditorialUsageHistory } from "@/modules/downloads/stream";
import { requireEditorial } from "../access";
import { UsageHistoryContent } from "./usage-history-content";
type UsageHistoryPageProps = Readonly<{
searchParams: Promise<{ page?: string | string[] }>;
}>;
export default async function UsagesPage({ searchParams }: UsageHistoryPageProps) {
const [principal, parameters] = await Promise.all([requireEditorial(), searchParams]);
const result = await loadHistory(principal.editorialId, singleValue(parameters.page));
if (result.unavailable) return <HistoryUnavailable />;
return <UsageHistoryContent history={result.history} />;
}
async function loadHistory(editorialId: string, page: string | undefined) {
try {
return {
history: await listEditorialUsageHistory(editorialId, page),
unavailable: false as const,
};
} catch {
return { history: null, unavailable: true as const };
}
}
function HistoryUnavailable() {
return (
<section className="editorial-section editorial-state" aria-labelledby="usage-history-title">
<p className="editorial-kicker">Abrechnung</p>
<h1 id="usage-history-title">Nutzungen momentan nicht erreichbar</h1>
<p role="status">Bitte laden Sie diese Seite in wenigen Minuten erneut.</p>
</section>
);
}
function singleValue(value: string | string[] | undefined): string | undefined {
return typeof value === "string" ? value : undefined;
}
@@ -0,0 +1,120 @@
import Link from "next/link";
import type { UsageHistoryAttempt, UsageHistoryPage } from "@/modules/downloads/stream";
const dateTimeFormatter = new Intl.DateTimeFormat("de-DE", {
dateStyle: "medium",
timeStyle: "short",
});
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">
<p className="editorial-kicker">Abrechnung · Downloadprotokoll</p>
<div className="editorial-title-row">
<h1 id="usage-history-title">Ihre Nutzungen</h1>
<p>Verwendungszwecke, Kosten und Auslieferungsversuche Ihrer Redaktion.</p>
</div>
{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>{formatDateTime(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>
{usage.reference ? (
<div>
<dt>Interne Referenz</dt>
<dd>{usage.reference}</dd>
</div>
) : null}
</dl>
{usage.attempts.length === 0 ? (
<p className="usage-attempt-empty">Noch kein abgeschlossener Auslieferungsversuch.</p>
) : (
<ol className="usage-attempts" aria-label={`Auslieferungen für ${usage.filename}`}>
{usage.attempts.map((attempt) => (
<AttemptRow attempt={attempt} key={attempt.downloadId} />
))}
</ol>
)}
</article>
</li>
))}
</ol>
)}
<HistoryPagination history={history} />
</section>
);
}
function AttemptRow({ attempt }: Readonly<{ attempt: UsageHistoryAttempt }>) {
return (
<li>
<time dateTime={attempt.finishedAt.toISOString()}>{formatDateTime(attempt.finishedAt)}</time>
<strong>{statusLabel(attempt.status)}</strong>
<span>{attempt.repeat ? "Wiederholter Abruf" : "Neue Nutzung"}</span>
<span>{byteFormatter.format(attempt.transferredBytes)}</span>
<b>{priceFormatter.format(attempt.billableCents / 100)}</b>
</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 Seite</Link>
) : (
<span />
)}
{history.hasNext ? (
<Link href={`/redaktion/nutzungen?page=${history.page + 1}`}>Nächste Seite</Link>
) : null}
</nav>
);
}
function statusLabel(status: UsageHistoryAttempt["status"]): string {
const labels: Record<UsageHistoryAttempt["status"], string> = {
completed: "Abgeschlossen",
interrupted: "Unterbrochen",
failed_before_delivery: "Vor Auslieferung fehlgeschlagen",
};
return labels[status];
}
function formatDateTime(value: Date): string {
return dateTimeFormatter.format(value);
}