From c50774440d55d95b9479ee26025b72bee05c9697 Mon Sep 17 00:00:00 2001 From: Maaxxs <61059039+MaaxxsDev@users.noreply.github.com> Date: Mon, 19 Oct 2026 18:48:35 +0200 Subject: [PATCH] Prozentanzeige beim Bilder-Hochladen Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL --- src/app/admin/upload/route.ts | 13 ++++++++++ src/components/admin/ImageManager.tsx | 37 ++++++++++++++++++++++----- src/styles/admin.css | 2 ++ 3 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/app/admin/upload/route.ts diff --git a/src/app/admin/upload/route.ts b/src/app/admin/upload/route.ts new file mode 100644 index 0000000..7139741 --- /dev/null +++ b/src/app/admin/upload/route.ts @@ -0,0 +1,13 @@ +import { uploadImages } from "@/lib/upload"; + +// Eigener Endpunkt statt Server Action, damit der Browser den Upload-Fortschritt melden kann. +export async function POST(req: Request) { + let fd: FormData; + try { + fd = await req.formData(); + } catch { + return Response.json({ urls: [], error: "Die Bilder konnten nicht gelesen werden." }, { status: 400 }); + } + const res = await uploadImages(fd); + return Response.json(res, { status: res.error && res.urls.length === 0 ? 400 : 200 }); +} diff --git a/src/components/admin/ImageManager.tsx b/src/components/admin/ImageManager.tsx index b13c0ac..15726f2 100644 --- a/src/components/admin/ImageManager.tsx +++ b/src/components/admin/ImageManager.tsx @@ -1,23 +1,41 @@ "use client"; /* eslint-disable @next/next/no-img-element */ import { useRef, useState } from "react"; -import { uploadImages } from "@/lib/upload"; import { Icon } from "../Icon"; +// XMLHttpRequest statt fetch, weil nur so der Upload-Fortschritt abrufbar ist. +function send(fd: FormData, onProgress: (p: number) => void): Promise<{ urls: string[]; error?: string }> { + return new Promise((resolve) => { + const xhr = new XMLHttpRequest(); + xhr.open("POST", "/admin/upload"); + xhr.responseType = "json"; + xhr.upload.onprogress = (e) => { + if (e.lengthComputable) onProgress(Math.min(100, Math.round((e.loaded / e.total) * 100))); + }; + xhr.onload = () => { + const body = xhr.response as { urls?: string[]; error?: string } | null; + if (body && Array.isArray(body.urls)) resolve({ urls: body.urls, error: body.error }); + else resolve({ urls: [], error: xhr.status === 413 ? "Die Bilder sind zu groß." : "Hochladen fehlgeschlagen. Bitte versuche es erneut." }); + }; + xhr.onerror = () => resolve({ urls: [], error: "Hochladen fehlgeschlagen. Bitte prüfe deine Verbindung." }); + xhr.send(fd); + }); +} + export function ImageManager({ name, initial, multiple = true }: { name: string; initial: string[]; multiple?: boolean }) { const [images, setImages] = useState(initial); - const [busy, setBusy] = useState(false); + const [progress, setProgress] = useState(null); const [error, setError] = useState(null); const input = useRef(null); async function onFiles(files: FileList | null) { if (!files?.length) return; - setBusy(true); + setProgress(0); setError(null); const fd = new FormData(); for (const f of Array.from(files)) fd.append("files", f); - const res = await uploadImages(fd); - setBusy(false); + const res = await send(fd, setProgress); + setProgress(null); if (res.error) setError(res.error); setImages((cur) => (multiple ? [...cur, ...res.urls] : res.urls.slice(0, 1).length ? res.urls.slice(0, 1) : cur)); if (input.current) input.current.value = ""; @@ -48,8 +66,13 @@ export function ImageManager({ name, initial, multiple = true }: { name: string; {(multiple || images.length === 0) && ( )} diff --git a/src/styles/admin.css b/src/styles/admin.css index 9b0f2fe..fb89afc 100644 --- a/src/styles/admin.css +++ b/src/styles/admin.css @@ -169,3 +169,5 @@ a.ad-kpi:hover { border-color: var(--line-strong); } /* Felder nebeneinander: kein zusätzlicher Abstand oben, sonst sitzt das rechte Feld tiefer */ .ad-form .sm-form-row .sm-field + .sm-field { margin-top: 0; } +.ad-upload__bar { display: block; width: 80px; height: 4px; margin: var(--space-1) auto 0; border-radius: 2px; background: var(--line); overflow: hidden; } +.ad-upload__bar > span { display: block; height: 100%; background: var(--gold); transition: width 150ms linear; }