'use client' import { useState, useRef } from 'react' import { useTranslations } from 'next-intl' import { Button } from '@/components/ui/button' import { Label } from '@/components/ui/label' import { useToast } from '@/components/ui/use-toast' import { Loader2, Upload, Trash2 } from 'lucide-react' interface LogoUploadProps { logoUrl: string | null onUpdate: (logoUrl: string | null) => void } export function LogoUpload({ logoUrl, onUpdate }: LogoUploadProps) { const t = useTranslations('settings_company') const { toast } = useToast() const [isUploading, setIsUploading] = useState(false) const [isDeleting, setIsDeleting] = useState(false) const [preview, setPreview] = useState(logoUrl) const [isDragging, setIsDragging] = useState(false) const inputRef = useRef(null) const ALLOWED_TYPES = ['image/png', 'image/jpeg', 'image/svg+xml', 'image/webp'] function validateAndUpload(file: File) { if (!ALLOWED_TYPES.includes(file.type)) { toast({ title: t('logo_disallowed_type_title'), description: t('logo_disallowed_type_description'), variant: 'destructive' }) return } if (file.size > 2 * 1024 * 1024) { toast({ title: t('logo_too_large'), variant: 'destructive' }) return } handleUpload(file) } async function handleUpload(file: File) { setIsUploading(true) const formData = new FormData() formData.append('file', file) try { const response = await fetch('/api/settings/logo', { method: 'POST', body: formData, }) const result = await response.json() if (!response.ok) { throw new Error(result.error || t('logo_upload_failed_default')) } setPreview(result.data.logo_url) onUpdate(result.data.logo_url) } catch (error) { toast({ title: t('logo_upload_failed_title'), description: error instanceof Error ? error.message : t('logo_try_again'), variant: 'destructive', }) } setIsUploading(false) } async function handleDelete() { setIsDeleting(true) try { const response = await fetch('/api/settings/logo', { method: 'DELETE' }) if (!response.ok) throw new Error() setPreview(null) onUpdate(null) if (inputRef.current) inputRef.current.value = '' } catch { toast({ title: t('logo_delete_failed'), variant: 'destructive' }) } setIsDeleting(false) } function handleFileChange(e: React.ChangeEvent) { const file = e.target.files?.[0] if (!file) return validateAndUpload(file) } function handleDrop(e: React.DragEvent) { e.preventDefault() setIsDragging(false) const file = e.dataTransfer.files?.[0] if (file) validateAndUpload(file) } function handleDragOver(e: React.DragEvent) { e.preventDefault() setIsDragging(true) } function handleDragLeave(e: React.DragEvent) { e.preventDefault() setIsDragging(false) } return (

{t('logo_heading')}

{t('logo_help')}

{preview ? (
{/* eslint-disable-next-line @next/next/no-img-element */} {t('logo_alt')}
) : ( )}
) }