import { useRef, useState } from 'react' import { CircleCheck, FileUp, Loader2 } from 'lucide-react' import { toast } from 'sonner' import { FormError } from '@/components/form-field' import { Button } from '@/components/ui/button' import { useDebtImport } from '@/lib/debt' import { formatDate, formatMoney } from '@/lib/format' import { formatMonth } from '@/lib/reports' import { t } from '@/lib/strings' import { cn } from '@/lib/utils' import type { DebtImportReport } from '@/types/DebtImportReport' const s = t.debt /** Borç verisi dosyası → ön izleme → onay (aynı dosya iki kez eklenmez) */ export function ImportSection() { const input = useRef(null) const run = useDebtImport() const [json, setJson] = useState(null) const [report, setReport] = useState(null) const [error, setError] = useState() async function choose(f: File) { setError(undefined) setReport(null) try { const parsed: unknown = JSON.parse(await f.text()) setJson(parsed) setReport(await run.mutateAsync({ json: parsed, commit: false })) } catch (e) { setError(e instanceof SyntaxError ? 'Dosya okunamadı: geçerli bir JSON değil.' : String(e)) } } async function commit() { try { const r = await run.mutateAsync({ json, commit: true }) setReport(r) toast.success(s.imported) } catch (e) { setError(e instanceof Error ? e.message : String(e)) } } const fresh = report?.rows.filter((r) => r.status === 'new' || r.status === 'update').length ?? 0 const label = (kind: string, l: string) => kind === 'snapshot' ? formatDate(l) : kind === 'target' ? formatMonth(l) : l return (

{s.importHint}

{ const f = e.target.files?.[0] if (f) void choose(f) e.target.value = '' }} /> {report && (
{report.errors.length > 0 && (
    {report.errors.map((e) => (
  • {e}
  • ))}
)}
{report.rows.map((r, i) => ( ))}
{s.kinds[r.kind]} {label(r.kind, r.label)} {formatMoney(r.amount)} {s.statuses[r.status]} {r.message &&
{r.message}
}
{report.committed ? (

{s.imported}

) : fresh > 0 ? ( ) : (

{s.nothingNew}

)}
)}
) }