- add POST /import, an import page and the import_yaml mcp tool; items are validated up front and imported in dependency order - switch list page import buttons to the central import - write scenario exports as separate --- yaml documents per entity - give each test app its own files dir to avoid cross-suite races - use node: prefix for node built-in imports - bump version to 1.18.0
117 lines
3.7 KiB
TypeScript
117 lines
3.7 KiB
TypeScript
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<HTMLInputElement>(null);
|
||
// List pages hand over the result of an import they ran
|
||
const [results, setResults] = useState<FileResult[]>(
|
||
() => (location.state as { results?: FileResult[] } | null)?.results ?? [],
|
||
);
|
||
const [importing, setImporting] = useState(false);
|
||
|
||
const handleFiles = async (e: ChangeEvent<HTMLInputElement>) => {
|
||
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<Row>[] = [
|
||
{ key: 'fileName', header: t('import.col_file'), render: (r) => r.fileName },
|
||
{
|
||
key: 'kind',
|
||
header: t('import.col_kind'),
|
||
width: 120,
|
||
render: (r) => <Badge variant="neutral">{t(`import.kind_${r.kind}`)}</Badge>,
|
||
},
|
||
{ 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}` : <span className={styles.muted}>–</span>,
|
||
},
|
||
];
|
||
|
||
return (
|
||
<div>
|
||
<div className={styles.pageToolbar}>
|
||
<Breadcrumbs items={[{ label: t('import.title'), icon: <Upload size={14} /> }]} />
|
||
<div className={styles.toolbarActions}>
|
||
<input
|
||
ref={fileInputRef}
|
||
type="file"
|
||
accept=".yaml,.yml,.json"
|
||
multiple
|
||
style={{ display: 'none' }}
|
||
onChange={handleFiles}
|
||
/>
|
||
<Button size="sm" onClick={() => fileInputRef.current?.click()} disabled={importing}>
|
||
<Upload size={14} />
|
||
{t('import.action_choose')}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
|
||
<Card>
|
||
<p>{t('import.description')}</p>
|
||
<p className={styles.muted}>{t('import.description_validation')}</p>
|
||
</Card>
|
||
|
||
{rows.length > 0 && (
|
||
<div className={styles.stepsSection}>
|
||
<h2 className={styles.sectionHeading}>
|
||
{t('import.imported_count', { count: rows.length })}
|
||
</h2>
|
||
<Table<Row>
|
||
columns={columns}
|
||
data={rows}
|
||
rowKey={(r) => r.rowKey}
|
||
loading={false}
|
||
emptyMessage=""
|
||
onRowClick={(r) => navigate(importedItemPath(r))}
|
||
/>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|