"use client"; import { useRef, useState, type ChangeEvent } from "react"; import { useI18n } from "@/i18n/i18n-provider"; interface AdminVideoFieldProps { /** Formularfeld-Name, unter dem der aktuelle Videopfad beim Speichern des * umgebenden
mitgeschickt wird (siehe admin/actions/content.ts). */ name: string; label: string; initialValue: string; } /** * Video-Upload-Feld fuer admin/inhalte (Content-Registry-Feldtyp "video", * aktuell nur home.hero.video). Analog zu AdminImageField: laedt die Datei * sofort per fetch() an app/api/admin/upload-video hoch und haelt den * zurueckgegebenen Pfad in einem versteckten Formularfeld — das eigentliche * Speichern passiert weiterhin ueber das umgebende * . */ export function AdminVideoField({ name, label, initialValue }: AdminVideoFieldProps) { const { t } = useI18n(); const [value, setValue] = useState(initialValue); const [status, setStatus] = useState<"idle" | "uploading" | "error">("idle"); const [errorMessage, setErrorMessage] = useState(""); const inputRef = useRef(null); async function handleFileChange(event: ChangeEvent) { const file = event.target.files?.[0]; if (!file) return; setStatus("uploading"); setErrorMessage(""); try { const body = new FormData(); body.append("file", file); const response = await fetch("/api/admin/upload-video", { method: "POST", body }); const data: { path?: string; error?: string } = await response.json(); if (!response.ok || !data.path) { throw new Error(data.error ?? t.adminVideoField.uploadError); } setValue(data.path); setStatus("idle"); } catch (error: unknown) { setStatus("error"); setErrorMessage(error instanceof Error ? error.message : t.adminVideoField.uploadError); } finally { if (inputRef.current) inputRef.current.value = ""; } } return (
{label}
{value ? (
); }