import { useRef, useState } from "react"; import { FileText, Loader2, Trash2, Upload } from "lucide-react"; import { adminApi, AdminApiError } from "../../api/adminApi"; interface PublicationFilePickerProps { format: "pdf" | "epub"; value: string; onChange: (url: string) => void; } export default function PublicationFilePicker({ format, value, onChange }: PublicationFilePickerProps) { const inputRef = useRef(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const upload = (file?: File) => { if (!file) return; if (!file.name.toLowerCase().endsWith(`.${format}`)) { setError(`Bitte eine ${format.toUpperCase()}-Datei auswählen.`); return; } if (file.size > 10 * 1024 * 1024) { setError("Die Datei darf maximal 10 MB groß sein."); return; } setLoading(true); setError(""); const reader = new FileReader(); reader.onload = async () => { try { const result = await adminApi.uploadPublication(file.name, String(reader.result), format); onChange(result.url); } catch (uploadError) { setError(uploadError instanceof AdminApiError ? uploadError.message : "Upload fehlgeschlagen."); } finally { setLoading(false); } }; reader.onerror = () => { setError("Die Datei konnte nicht gelesen werden."); setLoading(false); }; reader.readAsDataURL(file); }; return (
upload(event.target.files?.[0])} /> {value ? (
{value.split("/").pop()}
) : ( )} {error &&

{error}

}
); }