import { useState, useRef } from 'react'; import type { ImportStatementResponse } from '@family-budget/shared'; import { importStatement } from '../api/import'; import { updateAccount } from '../api/accounts'; interface Props { onClose: () => void; onDone: () => void; } export function ImportModal({ onClose, onDone }: Props) { const [result, setResult] = useState(null); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const [alias, setAlias] = useState(''); const [aliasSaved, setAliasSaved] = useState(false); const fileRef = useRef(null); const handleFileChange = async ( e: React.ChangeEvent, ) => { const file = e.target.files?.[0]; if (!file) return; const name = file.name.toLowerCase(); const type = file.type; const isPdf = type === 'application/pdf' || name.endsWith('.pdf'); const isJson = type === 'application/json' || name.endsWith('.json'); if (!isPdf && !isJson) { setError('Допустимы только файлы PDF или JSON'); return; } setLoading(true); setError(''); setResult(null); try { const resp = await importStatement(file); setResult(resp); } catch (err: unknown) { const msg = err instanceof Error ? err.message : 'Ошибка импорта'; setError(msg); } finally { setLoading(false); } }; const handleSaveAlias = async () => { if (!result || !alias.trim()) return; try { await updateAccount(result.accountId, { alias: alias.trim() }); setAliasSaved(true); } catch { // handled globally } }; return (
e.stopPropagation()}>

Импорт выписки

{error &&
{error}
} {!result && (

Выберите файл выписки (PDF или JSON, формат 1.0)

{loading && (
Импорт...
)}
)} {result && (

Импорт завершён

Счёт {result.accountNumberMasked}
Новый счёт {result.isNewAccount ? 'Да' : 'Нет'}
Импортировано {result.imported}
Дубликатов пропущено {result.duplicatesSkipped}
Всего в файле {result.totalInFile}
{result.isNewAccount && !aliasSaved && (
setAlias(e.target.value)} maxLength={50} />
)} {aliasSaved && (
Алиас сохранён
)}
)}
{result ? ( ) : ( )}
); }