import { useRef, useState, type ChangeEvent } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Upload } from 'lucide-react'; import { usePageTitle } from '../../hooks/usePageTitle'; import { importedItemPath } from '../../hooks/useImportFile'; import { imports } from '../../api'; import type { ImportedItem, ImportResult } from '../../api'; import { Badge, Breadcrumbs, Button, Card, Table, type TableColumn } from '../../ui'; import styles from '../Page.module.css'; interface FileResult { fileName: string; result: ImportResult; } type Row = ImportedItem & { fileName: string; rowKey: string }; export function ImportPage() { const { t } = useTranslation(); usePageTitle(t('import.title')); const navigate = useNavigate(); const location = useLocation(); const fileInputRef = useRef(null); // List pages hand over the result of an import they ran const [results, setResults] = useState( () => (location.state as { results?: FileResult[] } | null)?.results ?? [], ); const [importing, setImporting] = useState(false); const handleFiles = async (e: ChangeEvent) => { const files = Array.from(e.target.files ?? []); e.target.value = ''; if (files.length === 0) return; setImporting(true); const next: FileResult[] = []; try { for (const file of files) { try { next.push({ fileName: file.name, result: await imports.yaml(await file.text()) }); } catch { // error toast is already emitted by the API client; nothing from // this file was imported, continue with the next one } } } finally { setResults(next); setImporting(false); } }; const rows: Row[] = results.flatMap(({ fileName, result }) => result.imported.map((item, i) => ({ ...item, fileName, rowKey: `${fileName}-${i}` })), ); const columns: TableColumn[] = [ { key: 'fileName', header: t('import.col_file'), render: (r) => r.fileName }, { key: 'kind', header: t('import.col_kind'), width: 120, render: (r) => {t(`import.kind_${r.kind}`)}, }, { key: 'name', header: t('import.col_name'), render: (r) => r.name }, { key: 'version', header: t('import.col_version'), width: 90, render: (r) => r.version != null ? `v${r.version}` : –, }, ]; return (
}]} />

{t('import.description')}

{t('import.description_validation')}

{rows.length > 0 && (

{t('import.imported_count', { count: rows.length })}

columns={columns} data={rows} rowKey={(r) => r.rowKey} loading={false} emptyMessage="" onRowClick={(r) => navigate(importedItemPath(r))} />
)}
); }