import type { MigrationTask } from "#lib/api/migrator.ts"; import type { MigrationItem } from "#lib/components/import/MigrationStep.svelte"; type MigrationRowDraft = { id?: string | number; name: string; migration: { status: "idle" | "submitting" | "error" | "success"; error?: string; task?: { taskId: string; name: string }; jobId?: string; jobStatus?: string; jobError?: string; repoDid?: string; }; }; type DraftInput = | readonly MigrationRowDraft[] | Record | null | undefined; export const migrationRows = ( tasks?: readonly MigrationTask[] | null, drafts?: DraftInput ): MigrationItem[] => { const rows: MigrationItem[] = []; const seenNames = new Set(); const seenJobIds = new Set(); const draftList: MigrationRowDraft[] = Array.isArray(drafts) ? drafts : drafts && typeof drafts === "object" ? Object.entries(drafts).flatMap(([key, d]) => (d ? [{ ...d, id: d.id ?? key }] : [])) : []; for (const draft of draftList) { if (!draft || !draft.migration || draft.migration.status === "idle") continue; const id = draft.id !== undefined ? draft.id : draft.migration.status === "success" && draft.migration.jobId ? draft.migration.jobId : draft.name; seenNames.add(draft.name); if (draft.migration.status === "success" && draft.migration.jobId) { seenJobIds.add(draft.migration.jobId); } rows.push({ id, name: draft.name, migration: draft.migration }); } for (const task of tasks ?? []) { for (const job of task.jobs ?? []) { if (seenJobIds.has(job.id) || seenNames.has(job.name)) { continue; } seenJobIds.add(job.id); seenNames.add(job.name); rows.push({ id: job.id, name: job.name, migration: { status: "success", task: { taskId: task.id, name: job.name }, jobId: job.id, jobStatus: job.status, jobError: job.error, repoDid: job.repoDid } }); } } return rows; };