import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { __ } from '@/utils/i18n'; import { ImagePlus, X } from 'lucide-react'; import { useCallback, useRef, useState } from 'react'; const MAX_FILE_SIZE = 500 * 1024; // 500KB const MAX_DIMENSIONS = 500; export interface CustomBankData { name: string; logo: File | null; logoPreview: string | null; } interface CustomBankFormProps { defaultName?: string; onCancel: () => void; onChange: (data: CustomBankData) => void; value: CustomBankData; } export function CustomBankForm({ defaultName = '', onCancel, onChange, value, }: CustomBankFormProps) { const [error, setError] = useState(null); const fileInputRef = useRef(null); const validateImage = useCallback((file: File): Promise => { return new Promise((resolve) => { if (file.size > MAX_FILE_SIZE) { resolve( `File size must be less than ${MAX_FILE_SIZE / 1024}KB`, ); return; } const img = new Image(); const objectUrl = URL.createObjectURL(file); img.onload = () => { URL.revokeObjectURL(objectUrl); if (img.width !== img.height) { resolve('Image must be square (same width and height)'); return; } if (img.width > MAX_DIMENSIONS) { resolve( `Image dimensions must be ${MAX_DIMENSIONS}x${MAX_DIMENSIONS} pixels or smaller`, ); return; } resolve(null); }; img.onerror = () => { URL.revokeObjectURL(objectUrl); resolve('Invalid image file'); }; img.src = objectUrl; }); }, []); const handleFileChange = useCallback( async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; setError(null); if (!file) { onChange({ ...value, logo: null, logoPreview: null }); return; } const validationError = await validateImage(file); if (validationError) { setError(validationError); e.target.value = ''; return; } const preview = URL.createObjectURL(file); onChange({ ...value, logo: file, logoPreview: preview }); }, [onChange, validateImage, value], ); const handleNameChange = useCallback( (e: React.ChangeEvent) => { onChange({ ...value, name: e.target.value }); }, [onChange, value], ); return ( <>

{__('Square images only (max')} {MAX_DIMENSIONS}x{MAX_DIMENSIONS} {__('px) /')} {MAX_FILE_SIZE / 1024} {__('KB max.')}

{error && (

{error}

)}
); }