'use client' import { useTranslation } from '../../../i18n/useTranslation' import { useEffect, useRef, useState } from 'react' import useContractManagement from '../hooks/useContractManagement' const LOGO_ACCEPTED_TYPES = ['image/png', 'image/jpeg', 'image/webp', 'image/svg+xml'] const MAX_LOGO_BYTES = 1024 * 1024 type CompanySettingsForm = { company_name: string company_street: string company_postal_city: string company_country: string company_logo_base64: string | null company_logo_mime_type: string | null } function fileToBase64Payload(file: File) { return new Promise<{ base64: string; mimeType: string }>((resolve, reject) => { const reader = new FileReader() reader.onload = () => { const result = typeof reader.result === 'string' ? reader.result : '' const match = result.match(/^data:(image\/[a-zA-Z0-9.+-]+);base64,(.+)$/) if (!match) { reject(new Error('invalid_data_url')) return } resolve({ mimeType: match[1], base64: match[2] }) } reader.onerror = () => reject(new Error('read_failed')) reader.readAsDataURL(file) }) } export default function CompanySettingsPanel() { const { t } = useTranslation() const { getCompanySettings, updateCompanySettings } = useContractManagement() const logoInputRef = useRef(null) const [form, setForm] = useState({ company_name: '', company_street: '', company_postal_city: '', company_country: '', company_logo_base64: null, company_logo_mime_type: null, }) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [saved, setSaved] = useState(false) const [saveError, setSaveError] = useState('') const [logoError, setLogoError] = useState('') useEffect(() => { getCompanySettings() .then((data) => { setForm({ company_name: data.company_name || '', company_street: data.company_street || '', company_postal_city: data.company_postal_city || '', company_country: data.company_country || '', company_logo_base64: data.company_logo_base64 || null, company_logo_mime_type: data.company_logo_mime_type || null, }) }) .catch(() => {}) .finally(() => setLoading(false)) }, [getCompanySettings]) const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target setForm((prev) => ({ ...prev, [name]: value })) setSaved(false) } const handleLogoChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0] setLogoError('') if (!file) return if (!LOGO_ACCEPTED_TYPES.includes(file.type)) { setLogoError(t('autofix.k2bd38d5e')) if (logoInputRef.current) logoInputRef.current.value = '' return } if (file.size > MAX_LOGO_BYTES) { setLogoError(t('autofix.k394b7f42')) if (logoInputRef.current) logoInputRef.current.value = '' return } try { const { base64, mimeType } = await fileToBase64Payload(file) setForm((prev) => ({ ...prev, company_logo_base64: base64, company_logo_mime_type: mimeType, })) setSaved(false) } catch { setLogoError(t('autofix.k8a1d4c20')) } finally { if (logoInputRef.current) logoInputRef.current.value = '' } } const handleRemoveLogo = () => { setForm((prev) => ({ ...prev, company_logo_base64: null, company_logo_mime_type: null, })) setLogoError('') setSaved(false) if (logoInputRef.current) logoInputRef.current.value = '' } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setSaving(true) setSaved(false) setSaveError('') try { await updateCompanySettings(form) setSaved(true) setTimeout(() => setSaved(false), 3000) } catch { setSaveError(t('autofix.k95a16b2b')) } finally { setSaving(false) } } const logoPreviewSrc = form.company_logo_base64 ? `data:${form.company_logo_mime_type || 'image/png'};base64,${form.company_logo_base64}` : null if (loading) { return (
{t('autofix.k81a1b900')}
) } return (

{t('autofix.k0198ce13')}

{t('autofix.k03d7361d')}

{t('autofix.k1c2b0975')}

{logoPreviewSrc && ( )}
{logoPreviewSrc ? ( {t('autofix.k0198ce13')} ) : (
{t('autofix.k432b8a12')}
)}
{logoError &&
{logoError}
}

{t('autofix.kaa8bbc8e')}

{t('autofix.k15bea9bb')}

{saveError &&
{saveError}
}
{saved && {t('autofix.ka29ac729')}}
) }