"use client"; import { useRef, useState, type FormEvent } from "react"; import { Alert, Button } from "@sakaniui/react"; import { IconDownload } from "@sakaniui/react/icons"; import { formatBerlinDate } from "@/modules/downloads/format"; import type { DownloadDialogUsage } from "@/modules/downloads/stream"; type UsageDialogProps = Readonly<{ albumId: string; assetId: string; filename: string; newUsagePriceCents: number; priorUsages: DownloadDialogUsage[]; }>; type IntentResponse = { downloadId: string; fileUrl: string; priceCents: number; billable: boolean; }; const priceFormatter = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR", }); export function UsageDialog({ albumId, assetId, filename, newUsagePriceCents, priorUsages, }: UsageDialogProps) { const dialogRef = useRef(null); const firstChoiceRef = useRef(null); const idempotencyKeyRef = useRef(null); const initialChoice = priorUsages[0] ? `repeat:${priorUsages[0].id}` : "new_usage"; const [choice, setChoice] = useState(initialChoice); const [purpose, setPurpose] = useState(""); const [pending, setPending] = useState(false); const [error, setError] = useState(null); function openDialog() { setChoice(initialChoice); setPurpose(""); setPending(false); setError(null); idempotencyKeyRef.current = null; dialogRef.current?.showModal(); requestAnimationFrame(() => firstChoiceRef.current?.focus()); } function closeDialog() { if (!pending) dialogRef.current?.close(); } function resetSubmission() { idempotencyKeyRef.current = null; setError(null); } async function submit(event: FormEvent) { event.preventDefault(); if (pending) return; setPending(true); setError(null); idempotencyKeyRef.current ??= crypto.randomUUID(); const payload = choice.startsWith("repeat:") ? { kind: "repeat_download" as const, usageId: choice.slice("repeat:".length), idempotencyKey: idempotencyKeyRef.current, } : { kind: "new_usage" as const, assetId, albumId, purpose, idempotencyKey: idempotencyKeyRef.current, }; try { const response = await fetch("/api/download-intents", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), }); if (!response.ok) { const code = await readErrorCode(response); setError(downloadErrorMessage(code, response.status)); return; } const intent = (await response.json()) as IntentResponse; if (!isLocalDownloadUrl(intent.fileUrl, intent.downloadId)) { throw new Error("invalid download url"); } const link = document.createElement("a"); link.href = intent.fileUrl; link.download = filename; link.click(); dialogRef.current?.close(); idempotencyKeyRef.current = null; } catch { // Network failure / response not JSON — the server-code cases are // handled above; this is only a transport-level failure. setError("Der Download konnte nicht vorbereitet werden (Netzwerkfehler). Versuchen Sie es erneut."); } finally { setPending(false); } } const isNewUsage = choice === "new_usage"; return ( <> { if (pending) event.preventDefault(); }} onClose={() => { setError(null); idempotencyKeyRef.current = null; }} ref={dialogRef} >

Download · {filename}

Nutzung auswählen

Ordnen Sie das Original einer bestehenden oder einer neuen Nutzung zu.

Abrechnung {priorUsages.map((usage, index) => ( ))}