import { useRef, useState } from 'react'; import { clsx } from 'clsx'; import { Upload, X } from 'lucide-react'; // Datei → zentriert quadratisch zugeschnittene, auf `size` verkleinerte WebP-Data-URL. async function fileToSquareDataUrl(file: File, size = 256): Promise { const dataUrl = await new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result as string); reader.onerror = () => reject(new Error('Datei konnte nicht gelesen werden.')); reader.readAsDataURL(file); }); const img = await new Promise((resolve, reject) => { const i = new Image(); i.onload = () => resolve(i); i.onerror = () => reject(new Error('Bild konnte nicht geladen werden.')); i.src = dataUrl; }); const canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; const ctx = canvas.getContext('2d'); if (!ctx) throw new Error('Canvas nicht verfügbar.'); // Center-Crop (cover): kürzere Kante bestimmt das Quadrat. const s = Math.min(img.width, img.height); const sx = (img.width - s) / 2; const sy = (img.height - s) / 2; ctx.drawImage(img, sx, sy, s, s, 0, 0, size, size); return canvas.toDataURL('image/webp', 0.85); } export function ProjectLogo({ url, name, size = 40, className, }: { url: string | null | undefined; name: string; size?: number; className?: string; }) { if (url) { return ( ); } const initials = name.trim().slice(0, 2).toUpperCase() || '–'; return (
{initials}
); } export function LogoUpload({ value, name, onChange, className, }: { value: string | null; name: string; onChange: (v: string | null) => void; className?: string; }) { const inputRef = useRef(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function handleFile(file: File | undefined) { if (!file) return; setError(null); setBusy(true); try { onChange(await fileToSquareDataUrl(file)); } catch (e) { setError(e instanceof Error ? e.message : 'Upload fehlgeschlagen.'); } finally { setBusy(false); if (inputRef.current) inputRef.current.value = ''; } } return (
{value && ( )}
{error ? ( {error} ) : ( Quadratisch, wird auf 256px verkleinert. )}
handleFile(e.target.files?.[0])} />
); }