@rebasepro/cms 0.27.0 → 0.28.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collection_editor/serializable_utils.js +5 -2
- package/dist/collection_editor/serializable_utils.js.map +1 -1
- package/dist/collection_editor/ui/collection_editor/EnumForm.d.ts +17 -0
- package/dist/collection_editor/ui/collection_editor/EnumForm.js +31 -17
- package/dist/collection_editor/ui/collection_editor/EnumForm.js.map +1 -1
- package/dist/collection_editor/ui/collection_editor/properties/EnumPropertyField.js +6 -5
- package/dist/collection_editor/ui/collection_editor/properties/EnumPropertyField.js.map +1 -1
- package/dist/components/CollectionTableBinding/internal/popup_field/PopupFormField.js +3 -2
- package/dist/components/CollectionTableBinding/internal/popup_field/PopupFormField.js.map +1 -1
- package/dist/components/CollectionViewBinding/CollectionListViewBinding.js +32 -16
- package/dist/components/CollectionViewBinding/CollectionListViewBinding.js.map +1 -1
- package/dist/components/DetailViewBinding.js +4 -3
- package/dist/components/DetailViewBinding.js.map +1 -1
- package/dist/components/EditViewBinding.js +23 -5
- package/dist/components/EditViewBinding.js.map +1 -1
- package/dist/components/EntityPreviewBinding.js +1 -0
- package/dist/components/EntityPreviewBinding.js.map +1 -1
- package/dist/components/SelectableTable/filters/StringNumberFilterField.js +3 -3
- package/dist/components/SelectableTable/filters/StringNumberFilterField.js.map +1 -1
- package/dist/components/SidePanelBinding.js +7 -6
- package/dist/components/SidePanelBinding.js.map +1 -1
- package/dist/components/app/Scaffold.js +16 -1
- package/dist/components/app/Scaffold.js.map +1 -1
- package/dist/components/history/EntityHistoryView.js +2 -1
- package/dist/components/history/EntityHistoryView.js.map +1 -1
- package/dist/data_export/export/export.js +3 -3
- package/dist/data_export/export/export.js.map +1 -1
- package/dist/data_import/import/ImportCollectionAction.js +10 -12
- package/dist/data_import/import/ImportCollectionAction.js.map +1 -1
- package/dist/data_import/utils/id_column.d.ts +19 -0
- package/dist/data_import/utils/id_column.js +19 -1
- package/dist/data_import/utils/id_column.js.map +1 -1
- package/dist/data_import/utils/transforms.js +24 -7
- package/dist/data_import/utils/transforms.js.map +1 -1
- package/dist/form/EntityForm.d.ts +1 -1
- package/dist/form/EntityForm.js +14 -4
- package/dist/form/EntityForm.js.map +1 -1
- package/dist/form/EntityFormBinding.js +12 -8
- package/dist/form/EntityFormBinding.js.map +1 -1
- package/dist/form/components/FieldBlock.js +2 -1
- package/dist/form/components/FieldBlock.js.map +1 -1
- package/dist/form/field_bindings/KeyValueFieldBinding.js +3 -1
- package/dist/form/field_bindings/KeyValueFieldBinding.js.map +1 -1
- package/dist/form/field_bindings/ReadOnlyFieldBinding.js +0 -2
- package/dist/form/field_bindings/ReadOnlyFieldBinding.js.map +1 -1
- package/dist/form/field_bindings/TextFieldBinding.js +3 -2
- package/dist/form/field_bindings/TextFieldBinding.js.map +1 -1
- package/dist/form/validation.js +12 -1
- package/dist/form/validation.js.map +1 -1
- package/dist/hooks/navigation/url_segments.d.ts +33 -0
- package/dist/hooks/navigation/url_segments.js +56 -0
- package/dist/hooks/navigation/url_segments.js.map +1 -0
- package/dist/hooks/navigation/useBuildUrlController.js +4 -2
- package/dist/hooks/navigation/useBuildUrlController.js.map +1 -1
- package/dist/hooks/useBuildSidePanel.js +6 -5
- package/dist/hooks/useBuildSidePanel.js.map +1 -1
- package/dist/hooks/useHistory.js +4 -3
- package/dist/hooks/useHistory.js.map +1 -1
- package/dist/routes/RebaseRoute.js +20 -30
- package/dist/routes/RebaseRoute.js.map +1 -1
- package/dist/util/navigation_utils.js +4 -3
- package/dist/util/navigation_utils.js.map +1 -1
- package/package.json +9 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"export.js","names":[],"sources":["../../../src/data_export/export/export.ts"],"sourcesContent":["import { Entity, EntityReference, EntityRelation, Properties, Property } from \"@rebasepro/types\";\nimport { type ArrayValuesCount, escapeCsvFormula, getArrayValuesCount, getValueInPath, unescapeCsvFormula } from \"@rebasepro/utils\";\n\ninterface Header {\n key: string;\n label: string;\n}\n\nexport interface DownloadEntitiesExportParams<M extends Record<string, unknown>> {\n data: Entity<M>[];\n additionalData: Record<string, unknown>[] | undefined;\n properties: Properties;\n propertiesOrder: string[] | undefined;\n name: string;\n flattenArrays: boolean;\n additionalHeaders: string[] | undefined;\n exportType: \"csv\" | \"json\";\n dateExportType: \"timestamp\" | \"string\";\n}\n\nexport function downloadEntitiesExport<M extends Record<string, unknown>>({\n data,\n additionalData,\n properties,\n propertiesOrder,\n name,\n flattenArrays,\n additionalHeaders,\n exportType,\n dateExportType\n}: DownloadEntitiesExportParams<M>\n) {\n\n console.debug(\"Downloading export\", {\n dataLength: data.length,\n properties,\n exportType,\n dateExportType\n });\n\n if (exportType === \"csv\") {\n const writer = createCsvExportWriter({\n properties,\n propertiesOrder,\n additionalHeaders,\n flattenArrays,\n dateExportType\n });\n writer.addPage(data, additionalData);\n downloadBlob(writer.finish(), `${name}.csv`, \"text/csv\");\n } else {\n const exportableData = getEntityJsonExportableData(data, additionalData, properties, dateExportType);\n const json = JSON.stringify(exportableData, null, 2);\n downloadBlob([json], `${name}.json`, \"application/json\");\n }\n}\n\nexport function getEntityCSVExportableData(data: Entity<Record<string, unknown>>[],\n additionalData: Record<string, unknown>[] | undefined,\n properties: Properties,\n headers: Header[],\n dateExportType: \"timestamp\" | \"string\"\n) {\n return mergeExportRows(data, additionalData, properties, \"csv\", dateExportType)\n .map((entry) => headers.map((header) => getValueInPath(entry, header.key)));\n}\n\nexport function getEntityJsonExportableData(data: Entity<Record<string, unknown>>[],\n additionalData: Record<string, unknown>[] | undefined,\n properties: Properties,\n dateExportType: \"timestamp\" | \"string\"\n) {\n return mergeExportRows(data, additionalData, properties, \"json\", dateExportType);\n}\n\n/**\n * Each entity as the record its export row is read from: `id`, the processed\n * values, then `additionalData[i]` over them. An `additionalData` longer than\n * `data` adds rows of its own.\n */\nfunction mergeExportRows(data: Entity<Record<string, unknown>>[],\n additionalData: Record<string, unknown>[] | undefined,\n properties: Properties,\n exportType: \"csv\" | \"json\",\n dateExportType: \"timestamp\" | \"string\"\n): Record<string, unknown>[] {\n\n const mergedData: Record<string, unknown>[] = data.map(e => ({\n id: e.id,\n ...processValuesForExport(e.values, properties, exportType, dateExportType)\n }));\n\n if (additionalData) {\n additionalData.forEach((additional, index) => {\n mergedData[index] = { ...mergedData[index],\n...additional };\n });\n }\n\n return mergedData;\n}\n\n/**\n * The columns of an export, in groups: the `id`, then one group per property\n * in `propertiesOrder`, then one per additional header. A property's group is\n * the one that becomes a column per array item (`tags[0]`, `tags[1]`, …) when\n * arrays are flattened and some row holds more than one.\n */\ninterface ExportHeaderGroup {\n /** The property's key, for a property's group. */\n key?: string;\n property?: Property;\n /** The group's columns while its array is not expanded. */\n headers: Header[];\n}\n\nfunction getExportHeaderGroups(properties: Properties,\n propertiesOrder: string[] | undefined,\n additionalHeaders: string[] | undefined): ExportHeaderGroup[] {\n\n const groups: ExportHeaderGroup[] = [{ headers: [{ label: \"id\", key: \"id\" }] }];\n for (const childKey of propertiesOrder ?? Object.keys(properties)) {\n // The first column already is the id. A collection that declares its\n // key as a property named `id` wrote it twice, and the import refuses\n // a file with a duplicate header.\n if (childKey === \"id\") continue;\n const property = properties[childKey];\n if (!property) {\n console.warn(\"Property not found\", childKey, properties);\n continue;\n }\n groups.push({ key: childKey, property: property as Property, headers: getHeaders(property as Property, childKey, \"\") });\n }\n if (additionalHeaders) {\n groups.push(...additionalHeaders.map(h => ({ headers: [{ label: h, key: h }] })));\n }\n return groups;\n}\n\n/** How many columns per item the group's array spreads into, or 0 when it does not. */\nfunction expandedItemCount(group: ExportHeaderGroup, arrayValuesCount: ArrayValuesCount): number {\n if (group.key === undefined) return 0;\n const count = arrayValuesCount[group.key];\n return count > 1 ? count : 0;\n}\n\n/** The group's columns for array item `index`. */\nfunction itemHeaders(group: ExportHeaderGroup, index: number): Header[] {\n return getHeaders(group.property as Property, `${group.key}[${index}]`, \"\");\n}\n\nfunction headersOf(groups: ExportHeaderGroup[], arrayValuesCount: ArrayValuesCount): Header[] {\n return groups.flatMap((group) => {\n const count = expandedItemCount(group, arrayValuesCount);\n return count > 0\n ? Array.from({ length: count }, (_, i) => itemHeaders(group, i)).flat()\n : group.headers;\n });\n}\n\nfunction rowCells(entry: Record<string, unknown>, headers: Header[]): string[] {\n return headers.map((header) => toCSVCell(getValueInPath(entry, header.key)));\n}\n\nexport interface CsvExportWriterParams {\n properties: Properties;\n propertiesOrder: string[] | undefined;\n additionalHeaders: string[] | undefined;\n flattenArrays: boolean;\n dateExportType: \"timestamp\" | \"string\";\n}\n\n/**\n * Writes a CSV export one page of entities at a time.\n *\n * An export used to hold every entity it read, then a copy of every row, then\n * an array of cells per row, then the row strings — about 211 MB at its peak\n * for 100,000 rows × 20 columns, for a 32 MB file. Each page is now turned\n * into its CSV text as it arrives and its entities are let go; what is held\n * is the text itself.\n *\n * The file is the one the in-memory export wrote, byte for byte. What makes\n * that work across pages is `flattenArrays`: the columns an array spreads into\n * (`tags[0]` … `tags[n-1]`) depend on the longest array in *any* row, which\n * an early page cannot know. So a page is written with the columns known when\n * it arrives, and a row also keeps the cells of any array items those columns\n * leave out. At the end, a page written with fewer columns than the file has\n * is re-cut: its cells copied, and an array's swapped for, or extended with,\n * the kept item cells (or empty ones). Usually the longest array is met in the\n * first page and nothing is re-cut. A property key with `.` or `[` in it\n * resolves through other properties when spread, so an export with one keeps\n * its rows' records and writes them at the end, as before.\n */\nexport interface CsvExportWriter {\n /** One page of entities, with their additional-column values index for index. */\n addPage(data: Entity<Record<string, unknown>>[], additionalData?: Record<string, unknown>[]): void;\n /** The whole file, header row first. Once: the pages are handed over. */\n finish(): BlobPart[];\n}\n\n/**\n * A row's cells for the items of one array that its page's columns leave out,\n * kept in case the file has columns for them. The cells of items `0…n-1` in\n * one string, for an array its page did not spread; otherwise one string per\n * item, a hole where an item is not kept or its cells are all empty.\n */\ntype RowItemCells = string | (string | undefined)[];\n\ninterface CsvExportPage {\n /** The rows, `\\r\\n` after each. */\n text: string;\n rows: number;\n /** Per group, the items its array was spread into on this page; 0 when not spread. */\n spread: number[];\n /** By `row * groups + group`, for the rows that hold items `spread` leaves out. */\n items?: (RowItemCells | undefined)[];\n /** The rows' records, when the export cannot be re-cut (see the writer). */\n entries?: Record<string, unknown>[];\n}\n\nexport function createCsvExportWriter({\n properties,\n propertiesOrder,\n additionalHeaders,\n flattenArrays,\n dateExportType\n}: CsvExportWriterParams): CsvExportWriter {\n\n const groups = getExportHeaderGroups(properties, propertiesOrder, additionalHeaders);\n // The columns of item `i` of each group, built once per item, not per row.\n const itemHeaderCache: Header[][][] = groups.map(() => []);\n const headersOfItem = (groupIndex: number, index: number): Header[] =>\n itemHeaderCache[groupIndex][index] ??= itemHeaders(groups[groupIndex], index);\n // Cells per item; a group with none (a map with no properties) spreads\n // into no columns at all.\n const itemWidths = groups.map((group, groupIndex) => group.key === undefined ? 0 : headersOfItem(groupIndex, 0).length);\n const arrayValuesCount: ArrayValuesCount = {};\n // A key the spread column's path would read through some other property.\n const keepEntries = flattenArrays && groups.some((group) => group.key !== undefined && /[.[\\]]/.test(group.key));\n const pages: CsvExportPage[] = [];\n\n return {\n addPage(data, additionalData) {\n if (flattenArrays) {\n // Merged by maximum, which is what one pass over every row\n // computes.\n for (const [key, count] of Object.entries(getArrayValuesCount(data.map(d => d.values)))) {\n arrayValuesCount[key] = Math.max(arrayValuesCount[key] || 0, count);\n }\n }\n const spread = groups.map((group) => expandedItemCount(group, arrayValuesCount));\n const entries = mergeExportRows(data, additionalData, properties, \"csv\", dateExportType);\n if (keepEntries) {\n pages.push({ text: \"\", rows: entries.length, spread, entries });\n return;\n }\n const headers = headersOf(groups, arrayValuesCount);\n const lines: string[] = [];\n let items: CsvExportPage[\"items\"];\n entries.forEach((entry, row) => {\n lines.push(rowCells(entry, headers).join(\",\") + \"\\r\\n\");\n if (!flattenArrays) return;\n const bracketed = bracketedKeyIndices(entry);\n groups.forEach((group, groupIndex) => {\n if (group.key === undefined || itemWidths[groupIndex] === 0) return;\n const written = spread[groupIndex];\n const indices = itemIndicesWithValues(entry, group.key, bracketed)\n .filter((index) => index >= written);\n if (indices.length === 0) return;\n const value = entry[group.key];\n let cells: RowItemCells;\n if (written === 0 && Array.isArray(value) && indices.length === value.length) {\n cells = value.map((_, index) => rowCells(entry, headersOfItem(groupIndex, index)).join(\",\")).join(\",\");\n } else {\n cells = [];\n for (const index of indices) {\n cells[index] = rowCells(entry, headersOfItem(groupIndex, index)).join(\",\");\n }\n }\n (items ??= [])[row * groups.length + groupIndex] = cells;\n });\n });\n pages.push({ text: lines.join(\"\"), rows: entries.length, spread, items });\n },\n\n finish() {\n const counts = flattenArrays ? arrayValuesCount : {};\n const headers = headersOf(groups, counts);\n const parts: BlobPart[] = [entryToCSVRow(headers.map(h => h.label))];\n const spread = groups.map((group) => expandedItemCount(group, counts));\n\n // Handed over a page at a time, so a re-cut page and the text it\n // was cut from are not both held for the whole file.\n for (let page = pages.shift(); page; page = pages.shift()) {\n if (page.entries) {\n parts.push(page.entries.map((entry) => rowCells(entry, headers).join(\",\") + \"\\r\\n\").join(\"\"));\n } else if (page.spread.every((count, groupIndex) => count === spread[groupIndex])) {\n parts.push(page.text);\n } else {\n parts.push(recutPage(page, groups, spread, itemWidths));\n }\n }\n return parts;\n }\n };\n}\n\n/**\n * The record's own keys spelled `key[i]…`, by `key`: `getValueInPath` reads\n * one of those for the cell `key[i]` before anything else. Almost never there.\n */\nfunction bracketedKeyIndices(entry: Record<string, unknown>): Map<string, number[]> | undefined {\n let found: Map<string, number[]> | undefined;\n for (const own of Object.keys(entry)) {\n if (!own.includes(\"[\")) continue;\n const match = /^(.*?)\\[(\\d+)\\]/.exec(own);\n if (!match) continue;\n found ??= new Map();\n const indices = found.get(match[1]) ?? [];\n indices.push(Number(match[2]));\n found.set(match[1], indices);\n }\n return found;\n}\n\nconst NO_INDICES: number[] = [];\n\n/**\n * The array items of `entry[key]` whose spread cells may not all be empty —\n * every other item's are, as `getValueInPath` reads `key[i]` and\n * `key[i].child`: an array's own items, an object's own integer-named keys\n * (read as `key[i]` too), and a key of the record spelled `key[i]…` itself.\n * Over-including is harmless; the cells are computed, not assumed.\n */\nfunction itemIndicesWithValues(entry: Record<string, unknown>,\n key: string,\n bracketed: Map<string, number[]> | undefined): number[] {\n const value = entry[key];\n const extra = bracketed?.get(key);\n if (!extra) {\n // The common cases, without building a set per cell.\n if (Array.isArray(value)) return value.map((_, i) => i);\n if (value === null || typeof value !== \"object\") return NO_INDICES;\n }\n const indices = new Set<number>();\n if (Array.isArray(value)) {\n for (let i = 0; i < value.length; i++) indices.add(i);\n } else if (value !== null && typeof value === \"object\") {\n for (const own of Object.getOwnPropertyNames(value)) {\n const match = /^(\\d+)(?:$|[.[])/.exec(own);\n if (match) indices.add(Number(match[1]));\n }\n }\n extra?.forEach((index) => indices.add(index));\n return [...indices];\n}\n\n/**\n * A page written with fewer columns than the file has, cut back into cells\n * and written again: each array the file spreads further than the page did\n * gets its kept item cells, or empty ones.\n */\nfunction recutPage(page: CsvExportPage, groups: ExportHeaderGroup[], spread: number[], itemWidths: number[]): string {\n const text = page.text;\n const out: string[] = [];\n let pos = 0;\n for (let row = 0; row < page.rows; row++) {\n const cells: string[] = [];\n let first = true;\n groups.forEach((group, groupIndex) => {\n const written = page.spread[groupIndex];\n const itemWidth = itemWidths[groupIndex];\n const width = written > 0 ? written * itemWidth : group.headers.length;\n const groupCells: string[] = [];\n for (let c = 0; c < width; c++) {\n if (!first) pos++; // the comma before every cell but the row's first\n first = false;\n const end = csvCellEnd(text, pos);\n groupCells.push(text.slice(pos, end));\n pos = end;\n }\n const count = spread[groupIndex];\n if (count === written) {\n cells.push(...groupCells);\n return;\n }\n // Spread further than the page was: the items it wrote stay.\n if (written > 0) cells.push(...groupCells);\n const items = page.items?.[row * groups.length + groupIndex];\n const dense = typeof items === \"string\" ? splitCsvCells(items) : undefined;\n for (let i = written; i < count; i++) {\n const itemCells = dense\n ? dense.slice(i * itemWidth, (i + 1) * itemWidth)\n : (typeof items === \"object\" && items[i] !== undefined ? splitCsvCells(items[i] as string) : []);\n for (let c = 0; c < itemWidth; c++) cells.push(itemCells[c] ?? \"\");\n }\n });\n pos += 2; // \"\\r\\n\"\n out.push(cells.join(\",\") + \"\\r\\n\");\n }\n return out.join(\"\");\n}\n\n/** The cells of a run of them joined with commas, as `toCSVCell` wrote them. */\nfunction splitCsvCells(text: string): string[] {\n const cells: string[] = [];\n let pos = 0;\n for (; ;) {\n const end = csvCellEnd(text, pos);\n cells.push(text.slice(pos, end));\n if (end >= text.length) return cells;\n pos = end + 1;\n }\n}\n\n/**\n * Where the cell starting at `start` ends. Every cell `toCSVCell` writes is\n * either empty or quoted with its own quotes doubled, so the next lone quote\n * closes it.\n */\nfunction csvCellEnd(text: string, start: number): number {\n if (text.charCodeAt(start) !== 34 /* \" */) return start;\n let i = start + 1;\n for (; ;) {\n const quote = text.indexOf(\"\\\"\", i);\n if (quote < 0) return text.length;\n if (text.charCodeAt(quote + 1) === 34) {\n i = quote + 2;\n } else {\n return quote + 1;\n }\n }\n}\n\n/**\n * Get headers for property. There could be more than one header per property\n * @param property\n * @param propertyKey\n * @param prefix\n */\nfunction getHeaders(property: Property, propertyKey: string, prefix = \"\"): Header[] {\n const currentKey = prefix ? `${prefix}.${propertyKey}` : propertyKey;\n if (property.type === \"map\" && property.properties) {\n return Object.entries(property.properties)\n .map(([childKey, p]) => getHeaders(p, childKey, currentKey))\n .flat();\n } else {\n return [{\n label: currentKey,\n key: currentKey\n }];\n }\n}\n\n/**\n * Read whatever a `date` column actually arrived as into a `Date`.\n *\n * The driver/WebSocket path revives `{ __type: \"date\" }` into a `Date`; the REST\n * path — which is the one the admin is on — leaves an ISO string, and an epoch\n * number is what a re-imported export carries. Returns `undefined` for anything\n * that is not a date, so the caller can pass the raw value through.\n */\nfunction toExportableDate(inputValue: unknown): Date | undefined {\n if (inputValue instanceof Date) return Number.isNaN(inputValue.getTime()) ? undefined : inputValue;\n if (typeof inputValue === \"number\" && Number.isFinite(inputValue)) return new Date(inputValue);\n if (typeof inputValue === \"string\" && inputValue.trim() !== \"\") {\n const parsed = new Date(inputValue);\n return Number.isNaN(parsed.getTime()) ? undefined : parsed;\n }\n return undefined;\n}\n\n/**\n * The id a relation value points at, for a relation property's value — an\n * `EntityRelation`, or the same thing still in its `{ __type: \"relation\" }`\n * wire form. `undefined` for anything else, so a bare foreign key passes\n * through as the id it already is.\n */\nfunction relationIdForExport(value: unknown): string | number | undefined {\n if (value instanceof EntityRelation) return value.id;\n if (typeof value === \"object\" && value !== null\n && \"__type\" in value && value.__type === \"relation\"\n && \"id\" in value && (typeof value.id === \"string\" || typeof value.id === \"number\")) {\n return value.id;\n }\n return undefined;\n}\n\nfunction processValueForExport(inputValue: unknown,\n property: Property,\n exportType: \"csv\" | \"json\",\n dateExportType: \"timestamp\" | \"string\"\n): unknown {\n\n let value: unknown;\n if (property.type === \"relation\") {\n // A relation is exported as the id it points at — what the import\n // writes back — rather than the view-model object the admin holds,\n // which a CSV cell printed as `[object Object]`. To-many relations\n // are arrays of them.\n const toId = (item: unknown) => relationIdForExport(item) ?? item;\n value = Array.isArray(inputValue) ? inputValue.map(toId) : toId(inputValue);\n } else if (property.type === \"map\" && property.properties) {\n value = processValuesForExport(inputValue as Record<string, unknown>, property.properties as Properties, exportType, dateExportType);\n } else if (property.type === \"array\") {\n if (property.of && Array.isArray(inputValue)) {\n if (Array.isArray(property.of)) {\n value = property.of.map((p, i) => processValueForExport(inputValue[i], p, exportType, dateExportType));\n } else if (property.of.type === \"map\") {\n value = exportType === \"csv\"\n ? inputValue.map((e) => JSON.stringify(e))\n : inputValue.map((e) => processValueForExport(e, property.of as Property, exportType, dateExportType));\n ;\n } else {\n value = inputValue.map((e) => processValueForExport(e, property.of as Property, exportType, dateExportType));\n }\n } else {\n value = inputValue;\n }\n } else if (property.type === \"reference\" && inputValue && typeof inputValue === \"object\" && \"isEntityReference\" in inputValue && typeof (inputValue as EntityReference).isEntityReference === \"function\" && (inputValue as EntityReference).isEntityReference()) {\n const ref = inputValue ? inputValue as EntityReference : undefined;\n value = ref ? ref.fullPath : null;\n } else if (property.type === \"date\") {\n // Not `instanceof Date`: the admin reads over REST, and the REST row\n // pipeline hands dates back as the database wrote them — a string. That\n // made this branch dead on every live export, so both radio options\n // produced identical files. Normalise here, where the declared type says\n // the column is a date, and leave anything unparseable untouched rather\n // than exporting an invalid date as `null`.\n const date = toExportableDate(inputValue);\n value = date\n ? (dateExportType === \"timestamp\" ? date.getTime() : date.toISOString())\n : inputValue;\n } else {\n value = inputValue;\n }\n\n return value;\n}\n\nfunction processValuesForExport<M extends Record<string, unknown>>\n (inputValues: Record<string, unknown>,\n properties: Properties,\n exportType: \"csv\" | \"json\",\n dateExportType: \"timestamp\" | \"string\"\n ): Record<string, unknown> {\n // One accumulator. Merging `{ [key]: value }` objects pairwise copied the\n // whole row once per property — the square of the property count, per row.\n const result: Record<string, unknown> = { ...inputValues };\n for (const [key, property] of Object.entries(properties)) {\n const inputValue = inputValues && (inputValues)[key];\n const updatedValue = processValueForExport(inputValue, property as Property, exportType, dateExportType);\n if (updatedValue === undefined) continue;\n if (key === \"__proto__\") {\n // An own property, as the object spread made it, not the prototype.\n Object.defineProperty(result, key, { value: updatedValue, enumerable: true, writable: true, configurable: true });\n } else {\n result[key] = updatedValue;\n }\n }\n return result;\n}\n\n// One rule for every CSV the product writes — here, the SQL console's export\n// and the server's streamed download: `@rebasepro/utils`' csv helpers.\nexport { escapeCsvFormula, unescapeCsvFormula };\n\nfunction toCSVCell(v: unknown): string {\n if (v === null || v === undefined) return \"\";\n // An array or object (a geopoint, a value no property declares) is written\n // as its JSON: `String()` of an object is `[object Object]`. Date\n // properties are normalised before they get here.\n const s = Array.isArray(v) || (typeof v === \"object\" && !(v instanceof Date))\n ? JSON.stringify(v)\n : String(v);\n return \"\\\"\" + escapeCsvFormula(s).replaceAll(\"\\\"\", \"\\\"\\\"\") + \"\\\"\";\n}\n\nexport function entryToCSVRow(entry: unknown[]) {\n return entry\n .map(toCSVCell)\n .join(\",\") + \"\\r\\n\";\n}\n\nexport function downloadBlob(content: BlobPart[], filename: string, contentType: string) {\n const blob = new Blob(content, { type: contentType });\n const url = URL.createObjectURL(blob);\n const pom = document.createElement(\"a\");\n pom.href = url;\n pom.setAttribute(\"download\", filename);\n pom.click();\n // Unrevoked, the URL kept the whole file in memory until the tab closed —\n // one more copy per export. The download holds its own reference once it\n // has started; the delay covers browsers that start it a moment after\n // `click()` returns.\n setTimeout(() => URL.revokeObjectURL(url), BLOB_URL_LIFETIME_MS);\n}\n\n/** How long an export's object URL outlives the click that downloads it. */\nconst BLOB_URL_LIFETIME_MS = 10_000;\n\nexport function downloadDataAsCsv(data: object[], name: string) {\n if (data.length === 0) return;\n\n const headers = Object.keys(data[0]);\n const csvContent = [\n headers.join(\",\"),\n ...data.map(row => headers.map(header => toCSVCell((row as Record<string, unknown>)[header])).join(\",\"))\n ].join(\"\\r\\n\");\n\n downloadBlob([csvContent], `${name}.csv`, \"text/csv\");\n}\n"],"mappings":";;;AAoBA,SAAgB,uBAA0D,EACtE,MACA,gBACA,YACA,iBACA,MACA,eACA,mBACA,YACA,kBAEF;CAEE,QAAQ,MAAM,sBAAsB;EAChC,YAAY,KAAK;EACjB;EACA;EACA;CACJ,CAAC;CAED,IAAI,eAAe,OAAO;EACtB,MAAM,SAAS,sBAAsB;GACjC;GACA;GACA;GACA;GACA;EACJ,CAAC;EACD,OAAO,QAAQ,MAAM,cAAc;EACnC,aAAa,OAAO,OAAO,GAAG,GAAG,KAAK,OAAO,UAAU;CAC3D,OAAO;EACH,MAAM,iBAAiB,4BAA4B,MAAM,gBAAgB,YAAY,cAAc;EAEnG,aAAa,CADA,KAAK,UAAU,gBAAgB,MAAM,CACpC,CAAI,GAAG,GAAG,KAAK,QAAQ,kBAAkB;CAC3D;AACJ;AAEA,SAAgB,2BAA2B,MACvC,gBACA,YACA,SACA,gBACF;CACE,OAAO,gBAAgB,MAAM,gBAAgB,YAAY,OAAO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AC,CACd,KAAK,GAAG,IAAI;AACrB;AAEA,SAAgB,aAAa,SAAqB,UAAkB,aAAqB;CACrF,MAAM,OAAO,IAAI,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;CACpD,MAAM,MAAM,IAAI,gBAAgB,IAAI;CACpC,MAAM,MAAM,SAAS,cAAc,GAAG;CACtC,IAAI,OAAO;CACX,IAAI,aAAa,YAAY,QAAQ;CACrC,IAAI,MAAM;CAKV,iBAAiB,IAAI,gBAAgB,GAAG,GAAG,oBAAoB;AACnE;;AAGA,IAAM,uBAAuB;AAE7B,SAAgB,kBAAkB,MAAgB,MAAc;CAC5D,IAAI,KAAK,WAAW,GAAG;CAEvB,MAAM,UAAU,OAAO,KAAK,KAAK,EAAE;CAMnC,aAAa,CALM,CACf,QAAQ,KAAK,GAAG,GAChB,GAAG,KAAK,KAAI,QAAO,QAAQ,KAAI,WAAU,UAAW,IAAgC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAC3G,CAAC,CAAC,KAAK,MAEO,CAAU,GAAG,GAAG,KAAK,OAAO,UAAU;AACxD"}
|
|
1
|
+
{"version":3,"file":"export.js","names":[],"sources":["../../../src/data_export/export/export.ts"],"sourcesContent":["import { Entity, EntityReference, EntityRelation, Properties, Property } from \"@rebasepro/types\";\nimport { type ArrayValuesCount, escapeCsvFormula, getArrayValuesCount, getValueInPath, unescapeCsvFormula } from \"@rebasepro/utils\";\n\ninterface Header {\n key: string;\n label: string;\n}\n\nexport interface DownloadEntitiesExportParams<M extends Record<string, unknown>> {\n data: Entity<M>[];\n additionalData: Record<string, unknown>[] | undefined;\n properties: Properties;\n propertiesOrder: string[] | undefined;\n name: string;\n flattenArrays: boolean;\n additionalHeaders: string[] | undefined;\n exportType: \"csv\" | \"json\";\n dateExportType: \"timestamp\" | \"string\";\n}\n\nexport function downloadEntitiesExport<M extends Record<string, unknown>>({\n data,\n additionalData,\n properties,\n propertiesOrder,\n name,\n flattenArrays,\n additionalHeaders,\n exportType,\n dateExportType\n}: DownloadEntitiesExportParams<M>\n) {\n\n console.debug(\"Downloading export\", {\n dataLength: data.length,\n properties,\n exportType,\n dateExportType\n });\n\n if (exportType === \"csv\") {\n const writer = createCsvExportWriter({\n properties,\n propertiesOrder,\n additionalHeaders,\n flattenArrays,\n dateExportType\n });\n writer.addPage(data, additionalData);\n downloadBlob(writer.finish(), `${name}.csv`, \"text/csv\");\n } else {\n const exportableData = getEntityJsonExportableData(data, additionalData, properties, dateExportType);\n const json = JSON.stringify(exportableData, null, 2);\n downloadBlob([json], `${name}.json`, \"application/json\");\n }\n}\n\nexport function getEntityCSVExportableData(data: Entity<Record<string, unknown>>[],\n additionalData: Record<string, unknown>[] | undefined,\n properties: Properties,\n headers: Header[],\n dateExportType: \"timestamp\" | \"string\"\n) {\n return mergeExportRows(data, additionalData, properties, \"csv\", dateExportType)\n .map((entry) => headers.map((header) => getValueInPath(entry, header.key)));\n}\n\nexport function getEntityJsonExportableData(data: Entity<Record<string, unknown>>[],\n additionalData: Record<string, unknown>[] | undefined,\n properties: Properties,\n dateExportType: \"timestamp\" | \"string\"\n) {\n return mergeExportRows(data, additionalData, properties, \"json\", dateExportType);\n}\n\n/**\n * Each entity as the record its export row is read from: `id`, the processed\n * values, then `additionalData[i]` over them. An `additionalData` longer than\n * `data` adds rows of its own.\n */\nfunction mergeExportRows(data: Entity<Record<string, unknown>>[],\n additionalData: Record<string, unknown>[] | undefined,\n properties: Properties,\n exportType: \"csv\" | \"json\",\n dateExportType: \"timestamp\" | \"string\"\n): Record<string, unknown>[] {\n\n const mergedData: Record<string, unknown>[] = data.map(e => ({\n id: e.id,\n ...processValuesForExport(e.values, properties, exportType, dateExportType)\n }));\n\n if (additionalData) {\n additionalData.forEach((additional, index) => {\n mergedData[index] = { ...mergedData[index],\n...additional };\n });\n }\n\n return mergedData;\n}\n\n/**\n * The columns of an export, in groups: the `id`, then one group per property\n * in `propertiesOrder`, then one per additional header. A property's group is\n * the one that becomes a column per array item (`tags[0]`, `tags[1]`, …) when\n * arrays are flattened and some row holds more than one.\n */\ninterface ExportHeaderGroup {\n /** The property's key, for a property's group. */\n key?: string;\n property?: Property;\n /** The group's columns while its array is not expanded. */\n headers: Header[];\n}\n\nfunction getExportHeaderGroups(properties: Properties,\n propertiesOrder: string[] | undefined,\n additionalHeaders: string[] | undefined): ExportHeaderGroup[] {\n\n const groups: ExportHeaderGroup[] = [{ headers: [{ label: \"id\", key: \"id\" }] }];\n for (const childKey of propertiesOrder ?? Object.keys(properties)) {\n // The first column already is the id. A collection that declares its\n // key as a property named `id` wrote it twice, and the import refuses\n // a file with a duplicate header.\n if (childKey === \"id\") continue;\n const property = properties[childKey];\n if (!property) {\n console.warn(\"Property not found\", childKey, properties);\n continue;\n }\n groups.push({ key: childKey, property: property as Property, headers: getHeaders(property as Property, childKey, \"\") });\n }\n if (additionalHeaders) {\n groups.push(...additionalHeaders.map(h => ({ headers: [{ label: h, key: h }] })));\n }\n return groups;\n}\n\n/** How many columns per item the group's array spreads into, or 0 when it does not. */\nfunction expandedItemCount(group: ExportHeaderGroup, arrayValuesCount: ArrayValuesCount): number {\n if (group.key === undefined) return 0;\n const count = arrayValuesCount[group.key];\n return count > 1 ? count : 0;\n}\n\n/** The group's columns for array item `index`. */\nfunction itemHeaders(group: ExportHeaderGroup, index: number): Header[] {\n return getHeaders(group.property as Property, `${group.key}[${index}]`, \"\");\n}\n\nfunction headersOf(groups: ExportHeaderGroup[], arrayValuesCount: ArrayValuesCount): Header[] {\n return groups.flatMap((group) => {\n const count = expandedItemCount(group, arrayValuesCount);\n return count > 0\n ? Array.from({ length: count }, (_, i) => itemHeaders(group, i)).flat()\n : group.headers;\n });\n}\n\nfunction rowCells(entry: Record<string, unknown>, headers: Header[]): string[] {\n return headers.map((header) => toCSVCell(getValueInPath(entry, header.key)));\n}\n\nexport interface CsvExportWriterParams {\n properties: Properties;\n propertiesOrder: string[] | undefined;\n additionalHeaders: string[] | undefined;\n flattenArrays: boolean;\n dateExportType: \"timestamp\" | \"string\";\n}\n\n/**\n * Writes a CSV export one page of entities at a time.\n *\n * An export used to hold every entity it read, then a copy of every row, then\n * an array of cells per row, then the row strings — about 211 MB at its peak\n * for 100,000 rows × 20 columns, for a 32 MB file. Each page is now turned\n * into its CSV text as it arrives and its entities are let go; what is held\n * is the text itself.\n *\n * The file is the one the in-memory export wrote, byte for byte. What makes\n * that work across pages is `flattenArrays`: the columns an array spreads into\n * (`tags[0]` … `tags[n-1]`) depend on the longest array in *any* row, which\n * an early page cannot know. So a page is written with the columns known when\n * it arrives, and a row also keeps the cells of any array items those columns\n * leave out. At the end, a page written with fewer columns than the file has\n * is re-cut: its cells copied, and an array's swapped for, or extended with,\n * the kept item cells (or empty ones). Usually the longest array is met in the\n * first page and nothing is re-cut. A property key with `.` or `[` in it\n * resolves through other properties when spread, so an export with one keeps\n * its rows' records and writes them at the end, as before.\n */\nexport interface CsvExportWriter {\n /** One page of entities, with their additional-column values index for index. */\n addPage(data: Entity<Record<string, unknown>>[], additionalData?: Record<string, unknown>[]): void;\n /** The whole file, header row first. Once: the pages are handed over. */\n finish(): BlobPart[];\n}\n\n/**\n * A row's cells for the items of one array that its page's columns leave out,\n * kept in case the file has columns for them. The cells of items `0…n-1` in\n * one string, for an array its page did not spread; otherwise one string per\n * item, a hole where an item is not kept or its cells are all empty.\n */\ntype RowItemCells = string | (string | undefined)[];\n\ninterface CsvExportPage {\n /** The rows, `\\r\\n` after each. */\n text: string;\n rows: number;\n /** Per group, the items its array was spread into on this page; 0 when not spread. */\n spread: number[];\n /** By `row * groups + group`, for the rows that hold items `spread` leaves out. */\n items?: (RowItemCells | undefined)[];\n /** The rows' records, when the export cannot be re-cut (see the writer). */\n entries?: Record<string, unknown>[];\n}\n\nexport function createCsvExportWriter({\n properties,\n propertiesOrder,\n additionalHeaders,\n flattenArrays,\n dateExportType\n}: CsvExportWriterParams): CsvExportWriter {\n\n const groups = getExportHeaderGroups(properties, propertiesOrder, additionalHeaders);\n // The columns of item `i` of each group, built once per item, not per row.\n const itemHeaderCache: Header[][][] = groups.map(() => []);\n const headersOfItem = (groupIndex: number, index: number): Header[] =>\n itemHeaderCache[groupIndex][index] ??= itemHeaders(groups[groupIndex], index);\n // Cells per item; a group with none (a map with no properties) spreads\n // into no columns at all.\n const itemWidths = groups.map((group, groupIndex) => group.key === undefined ? 0 : headersOfItem(groupIndex, 0).length);\n const arrayValuesCount: ArrayValuesCount = {};\n // A key the spread column's path would read through some other property.\n const keepEntries = flattenArrays && groups.some((group) => group.key !== undefined && /[.[\\]]/.test(group.key));\n const pages: CsvExportPage[] = [];\n\n return {\n addPage(data, additionalData) {\n if (flattenArrays) {\n // Merged by maximum, which is what one pass over every row\n // computes.\n for (const [key, count] of Object.entries(getArrayValuesCount(data.map(d => d.values)))) {\n arrayValuesCount[key] = Math.max(arrayValuesCount[key] || 0, count);\n }\n }\n const spread = groups.map((group) => expandedItemCount(group, arrayValuesCount));\n const entries = mergeExportRows(data, additionalData, properties, \"csv\", dateExportType);\n if (keepEntries) {\n pages.push({ text: \"\", rows: entries.length, spread, entries });\n return;\n }\n const headers = headersOf(groups, arrayValuesCount);\n const lines: string[] = [];\n let items: CsvExportPage[\"items\"];\n entries.forEach((entry, row) => {\n lines.push(rowCells(entry, headers).join(\",\") + \"\\r\\n\");\n if (!flattenArrays) return;\n const bracketed = bracketedKeyIndices(entry);\n groups.forEach((group, groupIndex) => {\n if (group.key === undefined || itemWidths[groupIndex] === 0) return;\n const written = spread[groupIndex];\n const indices = itemIndicesWithValues(entry, group.key, bracketed)\n .filter((index) => index >= written);\n if (indices.length === 0) return;\n const value = entry[group.key];\n let cells: RowItemCells;\n if (written === 0 && Array.isArray(value) && indices.length === value.length) {\n cells = value.map((_, index) => rowCells(entry, headersOfItem(groupIndex, index)).join(\",\")).join(\",\");\n } else {\n cells = [];\n for (const index of indices) {\n cells[index] = rowCells(entry, headersOfItem(groupIndex, index)).join(\",\");\n }\n }\n (items ??= [])[row * groups.length + groupIndex] = cells;\n });\n });\n pages.push({ text: lines.join(\"\"), rows: entries.length, spread, items });\n },\n\n finish() {\n const counts = flattenArrays ? arrayValuesCount : {};\n const headers = headersOf(groups, counts);\n const parts: BlobPart[] = [entryToCSVRow(headers.map(h => h.label))];\n const spread = groups.map((group) => expandedItemCount(group, counts));\n\n // Handed over a page at a time, so a re-cut page and the text it\n // was cut from are not both held for the whole file.\n for (let page = pages.shift(); page; page = pages.shift()) {\n if (page.entries) {\n parts.push(page.entries.map((entry) => rowCells(entry, headers).join(\",\") + \"\\r\\n\").join(\"\"));\n } else if (page.spread.every((count, groupIndex) => count === spread[groupIndex])) {\n parts.push(page.text);\n } else {\n parts.push(recutPage(page, groups, spread, itemWidths));\n }\n }\n return parts;\n }\n };\n}\n\n/**\n * The record's own keys spelled `key[i]…`, by `key`: `getValueInPath` reads\n * one of those for the cell `key[i]` before anything else. Almost never there.\n */\nfunction bracketedKeyIndices(entry: Record<string, unknown>): Map<string, number[]> | undefined {\n let found: Map<string, number[]> | undefined;\n for (const own of Object.keys(entry)) {\n if (!own.includes(\"[\")) continue;\n const match = /^(.*?)\\[(\\d+)\\]/.exec(own);\n if (!match) continue;\n found ??= new Map();\n const indices = found.get(match[1]) ?? [];\n indices.push(Number(match[2]));\n found.set(match[1], indices);\n }\n return found;\n}\n\nconst NO_INDICES: number[] = [];\n\n/**\n * The array items of `entry[key]` whose spread cells may not all be empty —\n * every other item's are, as `getValueInPath` reads `key[i]` and\n * `key[i].child`: an array's own items, an object's own integer-named keys\n * (read as `key[i]` too), and a key of the record spelled `key[i]…` itself.\n * Over-including is harmless; the cells are computed, not assumed.\n */\nfunction itemIndicesWithValues(entry: Record<string, unknown>,\n key: string,\n bracketed: Map<string, number[]> | undefined): number[] {\n const value = entry[key];\n const extra = bracketed?.get(key);\n if (!extra) {\n // The common cases, without building a set per cell.\n if (Array.isArray(value)) return value.map((_, i) => i);\n if (value === null || typeof value !== \"object\") return NO_INDICES;\n }\n const indices = new Set<number>();\n if (Array.isArray(value)) {\n for (let i = 0; i < value.length; i++) indices.add(i);\n } else if (value !== null && typeof value === \"object\") {\n for (const own of Object.getOwnPropertyNames(value)) {\n const match = /^(\\d+)(?:$|[.[])/.exec(own);\n if (match) indices.add(Number(match[1]));\n }\n }\n extra?.forEach((index) => indices.add(index));\n return [...indices];\n}\n\n/**\n * A page written with fewer columns than the file has, cut back into cells\n * and written again: each array the file spreads further than the page did\n * gets its kept item cells, or empty ones.\n */\nfunction recutPage(page: CsvExportPage, groups: ExportHeaderGroup[], spread: number[], itemWidths: number[]): string {\n const text = page.text;\n const out: string[] = [];\n let pos = 0;\n for (let row = 0; row < page.rows; row++) {\n const cells: string[] = [];\n let first = true;\n groups.forEach((group, groupIndex) => {\n const written = page.spread[groupIndex];\n const itemWidth = itemWidths[groupIndex];\n const width = written > 0 ? written * itemWidth : group.headers.length;\n const groupCells: string[] = [];\n for (let c = 0; c < width; c++) {\n if (!first) pos++; // the comma before every cell but the row's first\n first = false;\n const end = csvCellEnd(text, pos);\n groupCells.push(text.slice(pos, end));\n pos = end;\n }\n const count = spread[groupIndex];\n if (count === written) {\n cells.push(...groupCells);\n return;\n }\n // Spread further than the page was: the items it wrote stay.\n if (written > 0) cells.push(...groupCells);\n const items = page.items?.[row * groups.length + groupIndex];\n const dense = typeof items === \"string\" ? splitCsvCells(items) : undefined;\n for (let i = written; i < count; i++) {\n const itemCells = dense\n ? dense.slice(i * itemWidth, (i + 1) * itemWidth)\n : (typeof items === \"object\" && items[i] !== undefined ? splitCsvCells(items[i] as string) : []);\n for (let c = 0; c < itemWidth; c++) cells.push(itemCells[c] ?? \"\");\n }\n });\n pos += 2; // \"\\r\\n\"\n out.push(cells.join(\",\") + \"\\r\\n\");\n }\n return out.join(\"\");\n}\n\n/** The cells of a run of them joined with commas, as `toCSVCell` wrote them. */\nfunction splitCsvCells(text: string): string[] {\n const cells: string[] = [];\n let pos = 0;\n for (; ;) {\n const end = csvCellEnd(text, pos);\n cells.push(text.slice(pos, end));\n if (end >= text.length) return cells;\n pos = end + 1;\n }\n}\n\n/**\n * Where the cell starting at `start` ends. Every cell `toCSVCell` writes is\n * either empty or quoted with its own quotes doubled, so the next lone quote\n * closes it.\n */\nfunction csvCellEnd(text: string, start: number): number {\n if (text.charCodeAt(start) !== 34 /* \" */) return start;\n let i = start + 1;\n for (; ;) {\n const quote = text.indexOf(\"\\\"\", i);\n if (quote < 0) return text.length;\n if (text.charCodeAt(quote + 1) === 34) {\n i = quote + 2;\n } else {\n return quote + 1;\n }\n }\n}\n\n/**\n * Get headers for property. There could be more than one header per property\n * @param property\n * @param propertyKey\n * @param prefix\n */\nfunction getHeaders(property: Property, propertyKey: string, prefix = \"\"): Header[] {\n const currentKey = prefix ? `${prefix}.${propertyKey}` : propertyKey;\n if (property.type === \"map\" && property.properties) {\n return Object.entries(property.properties)\n .map(([childKey, p]) => getHeaders(p, childKey, currentKey))\n .flat();\n } else {\n return [{\n label: currentKey,\n key: currentKey\n }];\n }\n}\n\n/**\n * Read whatever a `date` column actually arrived as into a `Date`.\n *\n * The driver/WebSocket path revives `{ __type: \"date\" }` into a `Date`; the REST\n * path — which is the one the admin is on — leaves an ISO string, and an epoch\n * number is what a re-imported export carries. Returns `undefined` for anything\n * that is not a date, so the caller can pass the raw value through.\n */\nfunction toExportableDate(inputValue: unknown): Date | undefined {\n if (inputValue instanceof Date) return Number.isNaN(inputValue.getTime()) ? undefined : inputValue;\n if (typeof inputValue === \"number\" && Number.isFinite(inputValue)) return new Date(inputValue);\n if (typeof inputValue === \"string\" && inputValue.trim() !== \"\") {\n const parsed = new Date(inputValue);\n return Number.isNaN(parsed.getTime()) ? undefined : parsed;\n }\n return undefined;\n}\n\n/**\n * The id a relation value points at, for a relation property's value — an\n * `EntityRelation`, or the same thing still in its `{ __type: \"relation\" }`\n * wire form. `undefined` for anything else, so a bare foreign key passes\n * through as the id it already is.\n */\nfunction relationIdForExport(value: unknown): string | number | undefined {\n if (value instanceof EntityRelation) return value.id;\n if (typeof value === \"object\" && value !== null\n && \"__type\" in value && value.__type === \"relation\"\n && \"id\" in value && (typeof value.id === \"string\" || typeof value.id === \"number\")) {\n return value.id;\n }\n return undefined;\n}\n\nfunction processValueForExport(inputValue: unknown,\n property: Property,\n exportType: \"csv\" | \"json\",\n dateExportType: \"timestamp\" | \"string\"\n): unknown {\n\n let value: unknown;\n if (property.type === \"relation\") {\n // A relation is exported as the id it points at — what the import\n // writes back — rather than the view-model object the admin holds,\n // which a CSV cell printed as `[object Object]`. To-many relations\n // are arrays of them.\n const toId = (item: unknown) => relationIdForExport(item) ?? item;\n value = Array.isArray(inputValue) ? inputValue.map(toId) : toId(inputValue);\n } else if (property.type === \"map\" && property.properties) {\n // A map with no value is exported as none: read through, a `null` map\n // came out as an object of `null` fields.\n value = inputValue !== null && typeof inputValue === \"object\" && !Array.isArray(inputValue)\n ? processValuesForExport(inputValue as Record<string, unknown>, property.properties as Properties, exportType, dateExportType)\n : inputValue;\n } else if (property.type === \"array\") {\n if (property.of && Array.isArray(inputValue)) {\n if (Array.isArray(property.of)) {\n value = property.of.map((p, i) => processValueForExport(inputValue[i], p, exportType, dateExportType));\n } else if (property.of.type === \"map\") {\n value = exportType === \"csv\"\n ? inputValue.map((e) => JSON.stringify(e))\n : inputValue.map((e) => processValueForExport(e, property.of as Property, exportType, dateExportType));\n ;\n } else {\n value = inputValue.map((e) => processValueForExport(e, property.of as Property, exportType, dateExportType));\n }\n } else {\n value = inputValue;\n }\n } else if (property.type === \"reference\" && inputValue && typeof inputValue === \"object\" && \"isEntityReference\" in inputValue && typeof (inputValue as EntityReference).isEntityReference === \"function\" && (inputValue as EntityReference).isEntityReference()) {\n const ref = inputValue ? inputValue as EntityReference : undefined;\n value = ref ? ref.fullPath : null;\n } else if (property.type === \"date\") {\n // Not `instanceof Date`: the admin reads over REST, and the REST row\n // pipeline hands dates back as the database wrote them — a string. That\n // made this branch dead on every live export, so both radio options\n // produced identical files. Normalise here, where the declared type says\n // the column is a date, and leave anything unparseable untouched rather\n // than exporting an invalid date as `null`.\n const date = toExportableDate(inputValue);\n value = date\n ? (dateExportType === \"timestamp\" ? date.getTime() : date.toISOString())\n : inputValue;\n } else {\n value = inputValue;\n }\n\n return value;\n}\n\nfunction processValuesForExport<M extends Record<string, unknown>>\n (inputValues: Record<string, unknown>,\n properties: Properties,\n exportType: \"csv\" | \"json\",\n dateExportType: \"timestamp\" | \"string\"\n ): Record<string, unknown> {\n // One accumulator. Merging `{ [key]: value }` objects pairwise copied the\n // whole row once per property — the square of the property count, per row.\n const result: Record<string, unknown> = { ...inputValues };\n for (const [key, property] of Object.entries(properties)) {\n const inputValue = inputValues && (inputValues)[key];\n const updatedValue = processValueForExport(inputValue, property as Property, exportType, dateExportType);\n if (updatedValue === undefined) continue;\n if (key === \"__proto__\") {\n // An own property, as the object spread made it, not the prototype.\n Object.defineProperty(result, key, { value: updatedValue, enumerable: true, writable: true, configurable: true });\n } else {\n result[key] = updatedValue;\n }\n }\n return result;\n}\n\n// One rule for every CSV the product writes — here, the SQL console's export\n// and the server's streamed download: `@rebasepro/utils`' csv helpers.\nexport { escapeCsvFormula, unescapeCsvFormula };\n\nfunction toCSVCell(v: unknown): string {\n if (v === null || v === undefined) return \"\";\n // An array or object (a geopoint, a value no property declares) is written\n // as its JSON: `String()` of an object is `[object Object]`. Date\n // properties are normalised before they get here.\n const s = Array.isArray(v) || (typeof v === \"object\" && !(v instanceof Date))\n ? JSON.stringify(v)\n : String(v);\n return \"\\\"\" + escapeCsvFormula(s).replaceAll(\"\\\"\", \"\\\"\\\"\") + \"\\\"\";\n}\n\nexport function entryToCSVRow(entry: unknown[]) {\n return entry\n .map(toCSVCell)\n .join(\",\") + \"\\r\\n\";\n}\n\nexport function downloadBlob(content: BlobPart[], filename: string, contentType: string) {\n const blob = new Blob(content, { type: contentType });\n const url = URL.createObjectURL(blob);\n const pom = document.createElement(\"a\");\n pom.href = url;\n pom.setAttribute(\"download\", filename);\n pom.click();\n // Unrevoked, the URL kept the whole file in memory until the tab closed —\n // one more copy per export. The download holds its own reference once it\n // has started; the delay covers browsers that start it a moment after\n // `click()` returns.\n setTimeout(() => URL.revokeObjectURL(url), BLOB_URL_LIFETIME_MS);\n}\n\n/** How long an export's object URL outlives the click that downloads it. */\nconst BLOB_URL_LIFETIME_MS = 10_000;\n\nexport function downloadDataAsCsv(data: object[], name: string) {\n if (data.length === 0) return;\n\n // Every key any row holds, in the order they are first met; the header\n // row is written through the same cell escaping as the values.\n const headers = [...new Set(data.flatMap(row => Object.keys(row)))];\n const csvContent = [\n headers.map(toCSVCell).join(\",\"),\n ...data.map(row => headers.map(header => toCSVCell((row as Record<string, unknown>)[header])).join(\",\"))\n ].join(\"\\r\\n\");\n\n downloadBlob([csvContent], `${name}.csv`, \"text/csv\");\n}\n"],"mappings":";;;AAoBA,SAAgB,uBAA0D,EACtE,MACA,gBACA,YACA,iBACA,MACA,eACA,mBACA,YACA,kBAEF;CAEE,QAAQ,MAAM,sBAAsB;EAChC,YAAY,KAAK;EACjB;EACA;EACA;CACJ,CAAC;CAED,IAAI,eAAe,OAAO;EACtB,MAAM,SAAS,sBAAsB;GACjC;GACA;GACA;GACA;GACA;EACJ,CAAC;EACD,OAAO,QAAQ,MAAM,cAAc;EACnC,aAAa,OAAO,OAAO,GAAG,GAAG,KAAK,OAAO,UAAU;CAC3D,OAAO;EACH,MAAM,iBAAiB,4BAA4B,MAAM,gBAAgB,YAAY,cAAc;EAEnG,aAAa,CADA,KAAK,UAAU,gBAAgB,MAAM,CACpC,CAAI,GAAG,GAAG,KAAK,QAAQ,kBAAkB;CAC3D;AACJ;AAEA,SAAgB,2BAA2B,MACvC,gBACA,YACA,SACA,gBACF;CACE,OAAO,gBAAgB,MAAM,gBAAgB,YAAY,OAAO,cAAc,CAAC,CAC1E,KAAK,UAAU,QAAQ,KAAK,WAAW,eAAe,OAAO,OAAO,GAAG,CAAC,CAAC;AAClF;AAEA,SAAgB,4BAA4B,MACxC,gBACA,YACA,gBACF;CACE,OAAO,gBAAgB,MAAM,gBA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AAK,SAAS,EAAE,MAAM,YAAY,CAAC;CACpD,MAAM,MAAM,IAAI,gBAAgB,IAAI;CACpC,MAAM,MAAM,SAAS,cAAc,GAAG;CACtC,IAAI,OAAO;CACX,IAAI,aAAa,YAAY,QAAQ;CACrC,IAAI,MAAM;CAKV,iBAAiB,IAAI,gBAAgB,GAAG,GAAG,oBAAoB;AACnE;;AAGA,IAAM,uBAAuB;AAE7B,SAAgB,kBAAkB,MAAgB,MAAc;CAC5D,IAAI,KAAK,WAAW,GAAG;CAIvB,MAAM,UAAU,CAAC,GAAG,IAAI,IAAI,KAAK,SAAQ,QAAO,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC;CAMlE,aAAa,CALM,CACf,QAAQ,IAAI,SAAS,CAAC,CAAC,KAAK,GAAG,GAC/B,GAAG,KAAK,KAAI,QAAO,QAAQ,KAAI,WAAU,UAAW,IAAgC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAC3G,CAAC,CAAC,KAAK,MAEO,CAAU,GAAG,GAAG,KAAK,OAAO,UAAU;AACxD"}
|
|
@@ -11,7 +11,7 @@ import { getInferenceType } from "../utils/get_import_inference_type.js";
|
|
|
11
11
|
import { buildHeadersMappingFromData, takesObjectWhole } from "../utils/import_columns.js";
|
|
12
12
|
import { convertImportData } from "../utils/data.js";
|
|
13
13
|
import { ImportConversionProblems } from "../components/ImportConversionProblems.js";
|
|
14
|
-
import { guessIdColumn } from "../utils/id_column.js";
|
|
14
|
+
import { guessIdColumn, remapImportColumn } from "../utils/id_column.js";
|
|
15
15
|
import { DataNewPropertiesMapping } from "../components/DataNewPropertiesMapping.js";
|
|
16
16
|
import React, { useCallback, useEffect, useMemo } from "react";
|
|
17
17
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -99,16 +99,14 @@ function ImportCollectionAction({ collection, path, onAnalyticsEvent }) {
|
|
|
99
99
|
},
|
|
100
100
|
onPropertySelected: (newPropertyKey) => {
|
|
101
101
|
onAnalyticsEvent?.("import_mapping_field_updated");
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
importConfig.setHeadersMapping(newHeadersMapping);
|
|
111
|
-
if (newPropertyKey === importConfig.idColumn) importConfig.setIdColumn(void 0);
|
|
102
|
+
const remapped = remapImportColumn({
|
|
103
|
+
headersMapping: importConfig.headersMapping,
|
|
104
|
+
idColumn: importConfig.idColumn,
|
|
105
|
+
column: importKey,
|
|
106
|
+
targetKey: newPropertyKey
|
|
107
|
+
});
|
|
108
|
+
importConfig.setHeadersMapping(remapped.headersMapping);
|
|
109
|
+
importConfig.setIdColumn(remapped.idColumn);
|
|
112
110
|
}
|
|
113
111
|
});
|
|
114
112
|
}
|
|
@@ -186,8 +184,8 @@ function PropertyTreeSelect({ selectedPropertyKey, properties, onPropertySelecte
|
|
|
186
184
|
}, [selectedProperty]);
|
|
187
185
|
const onSelectValueChange = (value) => {
|
|
188
186
|
if (value === internalIDValue) {
|
|
189
|
-
onIdSelected();
|
|
190
187
|
onPropertySelected(null);
|
|
188
|
+
onIdSelected();
|
|
191
189
|
} else if (value === "__do_not_import") onPropertySelected(null);
|
|
192
190
|
else onPropertySelected(value);
|
|
193
191
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ImportCollectionAction.js","names":[],"sources":["../../../src/data_import/import/ImportCollectionAction.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useMemo } from \"react\";\nimport {\n useAuthController,\n useCustomizationController,\n useSnackbarController\n} from \"@rebasepro/app\";\nimport { getPropertiesWithPropertiesOrder, getPropertyInPath } from \"../../util\";\nimport { Properties, Property, User } from \"@rebasepro/types\";\nimport { CollectionActionsProps, AdminCollection } from \"@rebasepro/cms-types\";\nimport { getFieldConfig } from \"../../components/field_configs\";\nimport { PropertyConfigBadge } from \"../../components/PropertyConfigBadge\";\nimport { useSelectionController } from \"../../components/CollectionViewBinding/useSelectionController\";\nimport { CollectionTableBinding } from \"../../components/CollectionTableBinding/CollectionTableBinding\";\nimport { useCollectionRegistryController } from \"../../hooks\";\nimport {\n Button,\n cls,\n defaultBorderMixin,\n Dialog,\n DialogActions,\n DialogContent,\n DialogTitle,\n IconButton,\n iconSize,\n Select,\n SelectItem,\n Tooltip,\n Typography,\n UploadIcon\n} from \"@rebasepro/ui\";\nimport { buildEntityPropertiesFromData } from \"@rebasepro/inference\";\nimport { useImportConfig } from \"../hooks\";\nimport { convertImportData, getInferenceType, ImportConversionProblem } from \"../utils\";\nimport { guessIdColumn } from \"../utils/id_column\";\nimport { buildHeadersMappingFromData, takesObjectWhole } from \"../utils/import_columns\";\nimport { DataNewPropertiesMapping } from \"../components/DataNewPropertiesMapping\";\nimport { ImportFileUpload } from \"../components/ImportFileUpload\";\nimport { ImportSaveInProgress } from \"../components/ImportSaveInProgress\";\nimport { ImportConversionProblems } from \"../components/ImportConversionProblems\";\nimport { ImportConfig } from \"../types\";\n\ntype ImportState = \"initial\" | \"mapping\" | \"preview\" | \"import_data_saving\";\n\nexport function ImportCollectionAction<M extends Record<string, unknown>, USER extends User>({\n collection,\n path,\n onAnalyticsEvent\n}: CollectionActionsProps<M, USER> & {\n onAnalyticsEvent?: (event: string, params?: any) => void;\n}\n) {\n\n const snackbarController = useSnackbarController();\n\n const [open, setOpen] = React.useState(false);\n\n const [step, setStep] = React.useState<ImportState>(\"initial\");\n\n const importConfig = useImportConfig();\n\n const handleClickOpen = useCallback(() => {\n setOpen(true);\n onAnalyticsEvent?.(\"import_open\");\n setStep(\"initial\");\n }, [onAnalyticsEvent]);\n\n const handleClose = useCallback(() => {\n setOpen(false);\n }, [setOpen]);\n\n const onMappingComplete = useCallback(() => {\n onAnalyticsEvent?.(\"import_mapping_complete\");\n setStep(\"preview\");\n }, [onAnalyticsEvent]);\n\n const onPreviewComplete = useCallback(() => {\n onAnalyticsEvent?.(\"import_data_save\");\n setStep(\"import_data_saving\");\n }, [onAnalyticsEvent]);\n\n const onDataAdded = async (data: object[]) => {\n importConfig.setImportData(data);\n\n if (data.length > 0) {\n const originProperties = await buildEntityPropertiesFromData(data, getInferenceType);\n importConfig.setOriginProperties(originProperties as Properties);\n\n const headersMapping = buildHeadersMappingFromData(data, collection?.properties);\n importConfig.setHeadersMapping(headersMapping);\n importConfig.setIdColumn(guessIdColumn(Object.keys(headersMapping), headersMapping, collection?.properties));\n }\n setTimeout(() => {\n onAnalyticsEvent?.(\"import_data_added\");\n setStep(\"mapping\");\n }, 100);\n };\n\n // Built once per collection: every column's picker lists these, and a\n // list built afresh on each render redrew every picker on every remap.\n const properties = useMemo(\n () => getPropertiesWithPropertiesOrder(collection.properties, collection.propertiesOrder as Extract<keyof M, string>[]),\n [collection.properties, collection.propertiesOrder]\n );\n\n const propertiesAndLevel = useMemo(() => Object.entries(properties)\n .flatMap(([key, property]) => getPropertiesAndLevel(key, property, 0)), [properties]);\n const propertiesOrder = (collection.propertiesOrder ?? Object.keys(collection.properties)) as Extract<keyof M, string>[];\n\n return <>\n\n <Tooltip title={\"Import\"}\n asChild={true}>\n <IconButton aria-label={\"Import\"}\n size={\"small\"}\n onClick={handleClickOpen}>\n <UploadIcon size={iconSize.small}/>\n </IconButton>\n </Tooltip>\n\n <Dialog open={open}\n fullWidth={step !== \"initial\"}\n fullHeight={step !== \"initial\"}\n maxWidth={step === \"initial\" ? \"lg\" : \"7xl\"}>\n\n <DialogTitle variant={\"h6\"} hidden={step === \"preview\"}>Import data</DialogTitle>\n\n <DialogContent className={\"h-full flex flex-col gap-4 my-4\"} fullHeight={step === \"preview\"}>\n\n {step === \"initial\" && <>\n <Typography variant={\"body2\"}>Upload a CSV, Excel or JSON file and map it to your existing\n schema</Typography>\n <ImportFileUpload onDataAdded={onDataAdded}/>\n </>}\n\n {step === \"mapping\" && <>\n <DataNewPropertiesMapping importConfig={importConfig}\n destinationProperties={properties}\n buildPropertyView={({\n isIdColumn,\n property,\n propertyKey,\n importKey\n }) => {\n return <PropertyTreeSelect\n selectedPropertyKey={propertyKey ?? \"\"}\n properties={properties}\n propertiesAndLevel={propertiesAndLevel}\n isIdColumn={isIdColumn}\n onIdSelected={() => {\n importConfig.setIdColumn(importKey);\n }}\n onPropertySelected={(newPropertyKey) => {\n\n onAnalyticsEvent?.(\"import_mapping_field_updated\");\n const newHeadersMapping: Record<string, string | null> = Object.entries(importConfig.headersMapping)\n .map(([currentImportKey, currentPropertyKey]) => {\n if (currentPropertyKey === newPropertyKey) {\n return { [currentImportKey]: null };\n }\n if (currentImportKey === importKey) {\n return { [currentImportKey]: newPropertyKey };\n }\n return { [currentImportKey]: currentPropertyKey };\n })\n .reduce((acc, curr) => ({ ...acc,\n...curr }), {});\n importConfig.setHeadersMapping(newHeadersMapping as Record<string, string>);\n\n if (newPropertyKey === importConfig.idColumn) {\n importConfig.setIdColumn(undefined);\n }\n\n }}\n />;\n }}/>\n </>}\n\n {step === \"preview\" && <ImportDataPreview importConfig={importConfig}\n properties={properties}\n propertiesOrder={propertiesOrder}/>}\n\n {step === \"import_data_saving\" && importConfig &&\n <ImportSaveInProgress importConfig={importConfig}\n collection={collection as AdminCollection}\n path={path}\n onImportSuccess={(importedCollection) => {\n handleClose();\n snackbarController.open({\n type: \"info\",\n message: \"Data imported successfully\"\n });\n }}\n />}\n\n </DialogContent>\n <DialogActions>\n\n {step === \"mapping\" && <Button\n onClick={() => setStep(\"initial\")}\n variant={\"text\"}>\n Back\n </Button>}\n\n {step === \"preview\" && <Button\n onClick={() => setStep(\"mapping\")}\n variant={\"text\"}>\n Back\n </Button>}\n\n <Button onClick={handleClose}\n variant={\"text\"}>\n Cancel\n </Button>\n\n {step === \"mapping\" && <Button variant=\"filled\"\n color={\"primary\"}\n onClick={onMappingComplete}>\n Next\n </Button>}\n\n {step === \"preview\" && <Button variant=\"filled\"\n color={\"primary\"}\n onClick={onPreviewComplete}>\n Save data\n </Button>}\n\n </DialogActions>\n </Dialog>\n\n </>;\n}\n\nconst internalIDValue = \"__internal_id__\";\n\nfunction PropertyTreeSelect({\n selectedPropertyKey,\n properties,\n onPropertySelected,\n onIdSelected,\n propertiesAndLevel,\n isIdColumn\n}: {\n selectedPropertyKey: string | null;\n properties: Record<string, Property>;\n onPropertySelected: (propertyKey: string | null) => void;\n onIdSelected: () => void;\n propertiesAndLevel: PropertyAndLevel[];\n isIdColumn?: boolean;\n}) {\n\n const selectedProperty = selectedPropertyKey ? getPropertyInPath(properties, selectedPropertyKey) : null;\n\n const renderValue = useCallback((selectedPropertyKey: string) => {\n\n if (selectedPropertyKey === internalIDValue) {\n return <Typography variant={\"body2\"} className={\"p-4\"}>Use this column as ID</Typography>;\n }\n\n if (!selectedPropertyKey || !selectedProperty) {\n return <Typography variant={\"body2\"} color=\"disabled\" className={\"p-4\"}>Do not import this\n property</Typography>;\n }\n\n return <PropertySelectEntry propertyKey={selectedPropertyKey}\n property={selectedProperty as Property}/>;\n }, [selectedProperty]);\n\n const onSelectValueChange = (value: string) => {\n if (value === internalIDValue) {\n onIdSelected();\n onPropertySelected(null);\n } else if (value === \"__do_not_import\") {\n onPropertySelected(null);\n } else {\n onPropertySelected(value);\n }\n };\n\n // The same elements on every render: a remap re-renders every picker, and\n // fresh elements made each of them redraw its whole list.\n const propertyItems = useMemo(() => propertiesAndLevel.map(({\n property,\n level,\n propertyKey\n }) => {\n // A map with fields is filled from a column per field; a\n // key-value map, like a geopoint, takes a column whole.\n return <SelectItem value={propertyKey}\n key={propertyKey}\n textValue={property.name || propertyKey}\n disabled={property.type === \"map\" && !takesObjectWhole(property)}>\n <PropertySelectEntry propertyKey={propertyKey}\n property={property}\n level={level}/>\n </SelectItem>;\n }), [propertiesAndLevel]);\n\n return <Select value={isIdColumn ? internalIDValue : (selectedPropertyKey ?? undefined)}\n fullWidth={true}\n onValueChange={onSelectValueChange}\n renderValue={renderValue}>\n {DO_NOT_IMPORT_ITEM}\n {USE_AS_ID_ITEM}\n {propertyItems}\n </Select>;\n}\n\nconst DO_NOT_IMPORT_ITEM = <SelectItem value={\"__do_not_import\"}>\n <Typography variant={\"body2\"} color={\"disabled\"} className={\"p-4\"}>Do not import this property</Typography>\n</SelectItem>;\n\nconst USE_AS_ID_ITEM = <SelectItem value={internalIDValue}>\n <Typography variant={\"body2\"} className={\"p-4\"}>Use this column as ID</Typography>\n</SelectItem>;\n\ntype PropertyAndLevel = {\n property: Property,\n level: number,\n propertyKey: string\n};\n\nfunction getPropertiesAndLevel(key: string, property: Property, level: number): PropertyAndLevel[] {\n const properties: PropertyAndLevel[] = [];\n properties.push({\n property,\n level,\n propertyKey: key\n });\n if (property.type === \"map\" && property.properties) {\n Object.entries(property.properties).forEach(([childKey, value]) => {\n properties.push(...getPropertiesAndLevel(`${key}.${childKey}`, value as Property, level + 1));\n });\n }\n return properties;\n}\n\nexport function PropertySelectEntry({\n propertyKey,\n property,\n level = 0\n}: {\n propertyKey: string;\n property: Property;\n level?: number;\n}) {\n\n const { propertyConfigs } = useCustomizationController();\n const widget = getFieldConfig(property, propertyConfigs);\n\n return <div\n className=\"flex flex-row w-full text-start items-center h-full\">\n\n {new Array(level).fill(0).map((_, index) =>\n <div className={cls(defaultBorderMixin, \"ml-8 border-l h-12\")} key={index}/>)}\n\n <div className={\"m-4\"}>\n <Tooltip title={widget?.name}>\n <PropertyConfigBadge propertyConfig={widget}/>\n </Tooltip>\n </div>\n\n <div className={\"flex flex-col grow p-2 pl-2\"}>\n <Typography variant=\"body1\"\n component=\"span\"\n className=\"grow pr-2\">\n {property.name\n ? property.name\n : \"\\u00a0\"\n }\n </Typography>\n\n <Typography className=\" pr-2\"\n variant={\"body2\"}\n component=\"span\"\n color=\"secondary\">\n {propertyKey}\n </Typography>\n </div>\n\n </div>;\n\n}\n\nexport function ImportDataPreview<M extends Record<string, unknown>>({\n importConfig,\n properties,\n propertiesOrder\n}: {\n importConfig: ImportConfig,\n properties: Properties,\n propertiesOrder: Extract<keyof M, string>[],\n}) {\n const authController = useAuthController();\n const collectionRegistry = useCollectionRegistryController();\n const [problems, setProblems] = React.useState<ImportConversionProblem[]>([]);\n useEffect(() => {\n const converted = convertImportData(\n authController,\n collectionRegistry,\n importConfig.importData,\n importConfig.idColumn,\n importConfig.headersMapping,\n properties,\n \"TEMP_PATH\",\n importConfig.defaultValues\n );\n importConfig.setEntities(converted.entities);\n setProblems(converted.problems);\n }, []);\n\n const selectionController = useSelectionController();\n\n return <div className={\"flex flex-col h-full w-full\"}>\n <ImportConversionProblems problems={problems}/>\n <div className={\"flex-1 min-h-0\"}>\n <CollectionTableBinding\n title={<div>\n <Typography variant={\"subtitle2\"}>Imported data preview</Typography>\n {/* Conditional because it is only true when a column was chosen as\n the id: without one, every row is created and nothing can be\n overwritten. With one, the import asks for an upsert — which it\n did not, back when this sentence was unconditional and false. */}\n <Typography variant={\"caption\"}>\n {importConfig.idColumn\n ? \"Entities with the same id will be overwritten\"\n : \"All rows will be imported as new entities\"}\n </Typography>\n </div>}\n tableController={{\n data: importConfig.entities,\n dataLoading: false,\n noMoreToLoad: false\n }}\n enablePopupIcon={false}\n endAdornment={<div className={\"h-12\"}/>}\n filterable={false}\n sortable={false}\n openEntityMode={\"full_screen\"}\n selectionController={selectionController}\n properties={properties}/>\n </div>\n </div>;\n\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,uBAA6E,EACzF,YACA,MACA,oBAIF;CAEE,MAAM,qBAAqB,sBAAsB;CAEjD,MAAM,CAAC,MAAM,WAAW,MAAM,SAAS,KAAK;CAE5C,MAAM,CAAC,MAAM,WAAW,MAAM,SAAsB,SAAS;CAE7D,MAAM,eAAe,gBAAgB;CAErC,MAAM,kBAAkB,kBAAkB;EACtC,QAAQ,IAAI;EACZ,mBAAmB,aAAa;EAChC,QAAQ,SAAS;CACrB,GAAG,CAAC,gBAAgB,CAAC;CAErB,MAAM,cAAc,kBAAkB;EAClC,QAAQ,KAAK;CACjB,GAAG,CAAC,OAAO,CAAC;CAEZ,MAAM,oBAAoB,kBAAkB;EACxC,mBAAmB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.js","names":[],"sources":["../../../src/data_import/import/ImportCollectionAction.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useMemo } from \"react\";\nimport {\n useAuthController,\n useCustomizationController,\n useSnackbarController\n} from \"@rebasepro/app\";\nimport { getPropertiesWithPropertiesOrder, getPropertyInPath } from \"../../util\";\nimport { Properties, Property, User } from \"@rebasepro/types\";\nimport { CollectionActionsProps, AdminCollection } from \"@rebasepro/cms-types\";\nimport { getFieldConfig } from \"../../components/field_configs\";\nimport { PropertyConfigBadge } from \"../../components/PropertyConfigBadge\";\nimport { useSelectionController } from \"../../components/CollectionViewBinding/useSelectionController\";\nimport { CollectionTableBinding } from \"../../components/CollectionTableBinding/CollectionTableBinding\";\nimport { useCollectionRegistryController } from \"../../hooks\";\nimport {\n Button,\n cls,\n defaultBorderMixin,\n Dialog,\n DialogActions,\n DialogContent,\n DialogTitle,\n IconButton,\n iconSize,\n Select,\n SelectItem,\n Tooltip,\n Typography,\n UploadIcon\n} from \"@rebasepro/ui\";\nimport { buildEntityPropertiesFromData } from \"@rebasepro/inference\";\nimport { useImportConfig } from \"../hooks\";\nimport { convertImportData, getInferenceType, ImportConversionProblem } from \"../utils\";\nimport { guessIdColumn, remapImportColumn } from \"../utils/id_column\";\nimport { buildHeadersMappingFromData, takesObjectWhole } from \"../utils/import_columns\";\nimport { DataNewPropertiesMapping } from \"../components/DataNewPropertiesMapping\";\nimport { ImportFileUpload } from \"../components/ImportFileUpload\";\nimport { ImportSaveInProgress } from \"../components/ImportSaveInProgress\";\nimport { ImportConversionProblems } from \"../components/ImportConversionProblems\";\nimport { ImportConfig } from \"../types\";\n\ntype ImportState = \"initial\" | \"mapping\" | \"preview\" | \"import_data_saving\";\n\nexport function ImportCollectionAction<M extends Record<string, unknown>, USER extends User>({\n collection,\n path,\n onAnalyticsEvent\n}: CollectionActionsProps<M, USER> & {\n onAnalyticsEvent?: (event: string, params?: any) => void;\n}\n) {\n\n const snackbarController = useSnackbarController();\n\n const [open, setOpen] = React.useState(false);\n\n const [step, setStep] = React.useState<ImportState>(\"initial\");\n\n const importConfig = useImportConfig();\n\n const handleClickOpen = useCallback(() => {\n setOpen(true);\n onAnalyticsEvent?.(\"import_open\");\n setStep(\"initial\");\n }, [onAnalyticsEvent]);\n\n const handleClose = useCallback(() => {\n setOpen(false);\n }, [setOpen]);\n\n const onMappingComplete = useCallback(() => {\n onAnalyticsEvent?.(\"import_mapping_complete\");\n setStep(\"preview\");\n }, [onAnalyticsEvent]);\n\n const onPreviewComplete = useCallback(() => {\n onAnalyticsEvent?.(\"import_data_save\");\n setStep(\"import_data_saving\");\n }, [onAnalyticsEvent]);\n\n const onDataAdded = async (data: object[]) => {\n importConfig.setImportData(data);\n\n if (data.length > 0) {\n const originProperties = await buildEntityPropertiesFromData(data, getInferenceType);\n importConfig.setOriginProperties(originProperties as Properties);\n\n const headersMapping = buildHeadersMappingFromData(data, collection?.properties);\n importConfig.setHeadersMapping(headersMapping);\n importConfig.setIdColumn(guessIdColumn(Object.keys(headersMapping), headersMapping, collection?.properties));\n }\n setTimeout(() => {\n onAnalyticsEvent?.(\"import_data_added\");\n setStep(\"mapping\");\n }, 100);\n };\n\n // Built once per collection: every column's picker lists these, and a\n // list built afresh on each render redrew every picker on every remap.\n const properties = useMemo(\n () => getPropertiesWithPropertiesOrder(collection.properties, collection.propertiesOrder as Extract<keyof M, string>[]),\n [collection.properties, collection.propertiesOrder]\n );\n\n const propertiesAndLevel = useMemo(() => Object.entries(properties)\n .flatMap(([key, property]) => getPropertiesAndLevel(key, property, 0)), [properties]);\n const propertiesOrder = (collection.propertiesOrder ?? Object.keys(collection.properties)) as Extract<keyof M, string>[];\n\n return <>\n\n <Tooltip title={\"Import\"}\n asChild={true}>\n <IconButton aria-label={\"Import\"}\n size={\"small\"}\n onClick={handleClickOpen}>\n <UploadIcon size={iconSize.small}/>\n </IconButton>\n </Tooltip>\n\n <Dialog open={open}\n fullWidth={step !== \"initial\"}\n fullHeight={step !== \"initial\"}\n maxWidth={step === \"initial\" ? \"lg\" : \"7xl\"}>\n\n <DialogTitle variant={\"h6\"} hidden={step === \"preview\"}>Import data</DialogTitle>\n\n <DialogContent className={\"h-full flex flex-col gap-4 my-4\"} fullHeight={step === \"preview\"}>\n\n {step === \"initial\" && <>\n <Typography variant={\"body2\"}>Upload a CSV, Excel or JSON file and map it to your existing\n schema</Typography>\n <ImportFileUpload onDataAdded={onDataAdded}/>\n </>}\n\n {step === \"mapping\" && <>\n <DataNewPropertiesMapping importConfig={importConfig}\n destinationProperties={properties}\n buildPropertyView={({\n isIdColumn,\n property,\n propertyKey,\n importKey\n }) => {\n return <PropertyTreeSelect\n selectedPropertyKey={propertyKey ?? \"\"}\n properties={properties}\n propertiesAndLevel={propertiesAndLevel}\n isIdColumn={isIdColumn}\n onIdSelected={() => {\n importConfig.setIdColumn(importKey);\n }}\n onPropertySelected={(newPropertyKey) => {\n\n onAnalyticsEvent?.(\"import_mapping_field_updated\");\n const remapped = remapImportColumn({\n headersMapping: importConfig.headersMapping,\n idColumn: importConfig.idColumn,\n column: importKey,\n targetKey: newPropertyKey\n });\n importConfig.setHeadersMapping(remapped.headersMapping);\n importConfig.setIdColumn(remapped.idColumn);\n\n }}\n />;\n }}/>\n </>}\n\n {step === \"preview\" && <ImportDataPreview importConfig={importConfig}\n properties={properties}\n propertiesOrder={propertiesOrder}/>}\n\n {step === \"import_data_saving\" && importConfig &&\n <ImportSaveInProgress importConfig={importConfig}\n collection={collection as AdminCollection}\n path={path}\n onImportSuccess={(importedCollection) => {\n handleClose();\n snackbarController.open({\n type: \"info\",\n message: \"Data imported successfully\"\n });\n }}\n />}\n\n </DialogContent>\n <DialogActions>\n\n {step === \"mapping\" && <Button\n onClick={() => setStep(\"initial\")}\n variant={\"text\"}>\n Back\n </Button>}\n\n {step === \"preview\" && <Button\n onClick={() => setStep(\"mapping\")}\n variant={\"text\"}>\n Back\n </Button>}\n\n <Button onClick={handleClose}\n variant={\"text\"}>\n Cancel\n </Button>\n\n {step === \"mapping\" && <Button variant=\"filled\"\n color={\"primary\"}\n onClick={onMappingComplete}>\n Next\n </Button>}\n\n {step === \"preview\" && <Button variant=\"filled\"\n color={\"primary\"}\n onClick={onPreviewComplete}>\n Save data\n </Button>}\n\n </DialogActions>\n </Dialog>\n\n </>;\n}\n\nconst internalIDValue = \"__internal_id__\";\n\nfunction PropertyTreeSelect({\n selectedPropertyKey,\n properties,\n onPropertySelected,\n onIdSelected,\n propertiesAndLevel,\n isIdColumn\n}: {\n selectedPropertyKey: string | null;\n properties: Record<string, Property>;\n onPropertySelected: (propertyKey: string | null) => void;\n onIdSelected: () => void;\n propertiesAndLevel: PropertyAndLevel[];\n isIdColumn?: boolean;\n}) {\n\n const selectedProperty = selectedPropertyKey ? getPropertyInPath(properties, selectedPropertyKey) : null;\n\n const renderValue = useCallback((selectedPropertyKey: string) => {\n\n if (selectedPropertyKey === internalIDValue) {\n return <Typography variant={\"body2\"} className={\"p-4\"}>Use this column as ID</Typography>;\n }\n\n if (!selectedPropertyKey || !selectedProperty) {\n return <Typography variant={\"body2\"} color=\"disabled\" className={\"p-4\"}>Do not import this\n property</Typography>;\n }\n\n return <PropertySelectEntry propertyKey={selectedPropertyKey}\n property={selectedProperty as Property}/>;\n }, [selectedProperty]);\n\n const onSelectValueChange = (value: string) => {\n if (value === internalIDValue) {\n // The column's property is let go first, which also lets go of\n // whichever column was the id; then this one becomes it.\n onPropertySelected(null);\n onIdSelected();\n } else if (value === \"__do_not_import\") {\n onPropertySelected(null);\n } else {\n onPropertySelected(value);\n }\n };\n\n // The same elements on every render: a remap re-renders every picker, and\n // fresh elements made each of them redraw its whole list.\n const propertyItems = useMemo(() => propertiesAndLevel.map(({\n property,\n level,\n propertyKey\n }) => {\n // A map with fields is filled from a column per field; a\n // key-value map, like a geopoint, takes a column whole.\n return <SelectItem value={propertyKey}\n key={propertyKey}\n textValue={property.name || propertyKey}\n disabled={property.type === \"map\" && !takesObjectWhole(property)}>\n <PropertySelectEntry propertyKey={propertyKey}\n property={property}\n level={level}/>\n </SelectItem>;\n }), [propertiesAndLevel]);\n\n return <Select value={isIdColumn ? internalIDValue : (selectedPropertyKey ?? undefined)}\n fullWidth={true}\n onValueChange={onSelectValueChange}\n renderValue={renderValue}>\n {DO_NOT_IMPORT_ITEM}\n {USE_AS_ID_ITEM}\n {propertyItems}\n </Select>;\n}\n\nconst DO_NOT_IMPORT_ITEM = <SelectItem value={\"__do_not_import\"}>\n <Typography variant={\"body2\"} color={\"disabled\"} className={\"p-4\"}>Do not import this property</Typography>\n</SelectItem>;\n\nconst USE_AS_ID_ITEM = <SelectItem value={internalIDValue}>\n <Typography variant={\"body2\"} className={\"p-4\"}>Use this column as ID</Typography>\n</SelectItem>;\n\ntype PropertyAndLevel = {\n property: Property,\n level: number,\n propertyKey: string\n};\n\nfunction getPropertiesAndLevel(key: string, property: Property, level: number): PropertyAndLevel[] {\n const properties: PropertyAndLevel[] = [];\n properties.push({\n property,\n level,\n propertyKey: key\n });\n if (property.type === \"map\" && property.properties) {\n Object.entries(property.properties).forEach(([childKey, value]) => {\n properties.push(...getPropertiesAndLevel(`${key}.${childKey}`, value as Property, level + 1));\n });\n }\n return properties;\n}\n\nexport function PropertySelectEntry({\n propertyKey,\n property,\n level = 0\n}: {\n propertyKey: string;\n property: Property;\n level?: number;\n}) {\n\n const { propertyConfigs } = useCustomizationController();\n const widget = getFieldConfig(property, propertyConfigs);\n\n return <div\n className=\"flex flex-row w-full text-start items-center h-full\">\n\n {new Array(level).fill(0).map((_, index) =>\n <div className={cls(defaultBorderMixin, \"ml-8 border-l h-12\")} key={index}/>)}\n\n <div className={\"m-4\"}>\n <Tooltip title={widget?.name}>\n <PropertyConfigBadge propertyConfig={widget}/>\n </Tooltip>\n </div>\n\n <div className={\"flex flex-col grow p-2 pl-2\"}>\n <Typography variant=\"body1\"\n component=\"span\"\n className=\"grow pr-2\">\n {property.name\n ? property.name\n : \"\\u00a0\"\n }\n </Typography>\n\n <Typography className=\" pr-2\"\n variant={\"body2\"}\n component=\"span\"\n color=\"secondary\">\n {propertyKey}\n </Typography>\n </div>\n\n </div>;\n\n}\n\nexport function ImportDataPreview<M extends Record<string, unknown>>({\n importConfig,\n properties,\n propertiesOrder\n}: {\n importConfig: ImportConfig,\n properties: Properties,\n propertiesOrder: Extract<keyof M, string>[],\n}) {\n const authController = useAuthController();\n const collectionRegistry = useCollectionRegistryController();\n const [problems, setProblems] = React.useState<ImportConversionProblem[]>([]);\n useEffect(() => {\n const converted = convertImportData(\n authController,\n collectionRegistry,\n importConfig.importData,\n importConfig.idColumn,\n importConfig.headersMapping,\n properties,\n \"TEMP_PATH\",\n importConfig.defaultValues\n );\n importConfig.setEntities(converted.entities);\n setProblems(converted.problems);\n }, []);\n\n const selectionController = useSelectionController();\n\n return <div className={\"flex flex-col h-full w-full\"}>\n <ImportConversionProblems problems={problems}/>\n <div className={\"flex-1 min-h-0\"}>\n <CollectionTableBinding\n title={<div>\n <Typography variant={\"subtitle2\"}>Imported data preview</Typography>\n {/* Conditional because it is only true when a column was chosen as\n the id: without one, every row is created and nothing can be\n overwritten. With one, the import asks for an upsert — which it\n did not, back when this sentence was unconditional and false. */}\n <Typography variant={\"caption\"}>\n {importConfig.idColumn\n ? \"Entities with the same id will be overwritten\"\n : \"All rows will be imported as new entities\"}\n </Typography>\n </div>}\n tableController={{\n data: importConfig.entities,\n dataLoading: false,\n noMoreToLoad: false\n }}\n enablePopupIcon={false}\n endAdornment={<div className={\"h-12\"}/>}\n filterable={false}\n sortable={false}\n openEntityMode={\"full_screen\"}\n selectionController={selectionController}\n properties={properties}/>\n </div>\n </div>;\n\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,uBAA6E,EACzF,YACA,MACA,oBAIF;CAEE,MAAM,qBAAqB,sBAAsB;CAEjD,MAAM,CAAC,MAAM,WAAW,MAAM,SAAS,KAAK;CAE5C,MAAM,CAAC,MAAM,WAAW,MAAM,SAAsB,SAAS;CAE7D,MAAM,eAAe,gBAAgB;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/types";
|
|
|
14
14
|
* is left for the user to choose in the mapping step.
|
|
15
15
|
*/
|
|
16
16
|
export declare function guessIdColumn(headers: string[], headersMapping: Record<string, string | null>, properties?: Properties): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The mapping and id column after the person importing picks where one column
|
|
19
|
+
* goes, in that column's row of the mapping step: a property (`targetKey`), or
|
|
20
|
+
* nothing (`null`, "Do not import this property").
|
|
21
|
+
*
|
|
22
|
+
* A property holds one column, so a column that held `targetKey` before lets
|
|
23
|
+
* it go. And a column picked for a property, or for nothing, is no longer the
|
|
24
|
+
* id: the row's select shows "Use this column as ID" while it is, and the
|
|
25
|
+
* conversion takes the id column out of the values, so the pick was dropped.
|
|
26
|
+
*/
|
|
27
|
+
export declare function remapImportColumn({ headersMapping, idColumn, column, targetKey }: {
|
|
28
|
+
headersMapping: Record<string, string | null>;
|
|
29
|
+
idColumn: string | undefined;
|
|
30
|
+
column: string;
|
|
31
|
+
targetKey: string | null;
|
|
32
|
+
}): {
|
|
33
|
+
headersMapping: Record<string, string | null>;
|
|
34
|
+
idColumn: string | undefined;
|
|
35
|
+
};
|
|
@@ -21,7 +21,25 @@ function guessIdColumn(headers, headersMapping, properties) {
|
|
|
21
21
|
const mappedProperty = mappedKey ? properties?.[mappedKey] : void 0;
|
|
22
22
|
if (mappedProperty && "isId" in mappedProperty && mappedProperty.isId) return firstKey;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The mapping and id column after the person importing picks where one column
|
|
26
|
+
* goes, in that column's row of the mapping step: a property (`targetKey`), or
|
|
27
|
+
* nothing (`null`, "Do not import this property").
|
|
28
|
+
*
|
|
29
|
+
* A property holds one column, so a column that held `targetKey` before lets
|
|
30
|
+
* it go. And a column picked for a property, or for nothing, is no longer the
|
|
31
|
+
* id: the row's select shows "Use this column as ID" while it is, and the
|
|
32
|
+
* conversion takes the id column out of the values, so the pick was dropped.
|
|
33
|
+
*/
|
|
34
|
+
function remapImportColumn({ headersMapping, idColumn, column, targetKey }) {
|
|
35
|
+
const remapped = {};
|
|
36
|
+
for (const [currentColumn, currentKey] of Object.entries(headersMapping)) remapped[currentColumn] = currentColumn === column ? targetKey : targetKey !== null && currentKey === targetKey ? null : currentKey;
|
|
37
|
+
return {
|
|
38
|
+
headersMapping: remapped,
|
|
39
|
+
idColumn: idColumn === column ? void 0 : idColumn
|
|
40
|
+
};
|
|
41
|
+
}
|
|
24
42
|
//#endregion
|
|
25
|
-
export { guessIdColumn };
|
|
43
|
+
export { guessIdColumn, remapImportColumn };
|
|
26
44
|
|
|
27
45
|
//# sourceMappingURL=id_column.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id_column.js","names":[],"sources":["../../../src/data_import/utils/id_column.ts"],"sourcesContent":["import { Properties } from \"@rebasepro/types\";\n\n/**\n * The column an import uses as the row id before the user touches the mapping.\n *\n * The guess decides something destructive: the chosen column is taken out of\n * the values and sent as the id of an upsert, so every row overwrites the record\n * that already has that id. A header that merely *contained* \"id\" or \"key\" used\n * to qualify, so a file starting with `width`, `video` or `provider` wrote its\n * widths over the records whose ids they happened to equal.\n *\n * Only the first column is a candidate — the one the export writes the id to —\n * and only when its name says it is the id: the header is `id` (in any case),\n * or it maps onto a property the collection declares with `isId`. Anything else\n * is left for the user to choose in the mapping step.\n */\nexport function guessIdColumn(headers: string[],\n headersMapping: Record<string, string | null>,\n properties?: Properties): string | undefined {\n const firstKey = headers[0];\n if (firstKey === undefined) return undefined;\n\n if (firstKey.trim().toLowerCase() === \"id\") return firstKey;\n\n const mappedKey = headersMapping[firstKey];\n const mappedProperty = mappedKey ? properties?.[mappedKey] : undefined;\n if (mappedProperty && \"isId\" in mappedProperty && mappedProperty.isId) return firstKey;\n\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,SAAgB,cAAc,SAC1B,gBACA,YAA6C;CAC7C,MAAM,WAAW,QAAQ;CACzB,IAAI,aAAa,KAAA,GAAW,OAAO,KAAA;CAEnC,IAAI,SAAS,KAAK,CAAC,CAAC,YAAY,MAAM,MAAM,OAAO;CAEnD,MAAM,YAAY,eAAe;CACjC,MAAM,iBAAiB,YAAY,aAAa,aAAa,KAAA;CAC7D,IAAI,kBAAkB,UAAU,kBAAkB,eAAe,MAAM,OAAO;AAGlF"}
|
|
1
|
+
{"version":3,"file":"id_column.js","names":[],"sources":["../../../src/data_import/utils/id_column.ts"],"sourcesContent":["import { Properties } from \"@rebasepro/types\";\n\n/**\n * The column an import uses as the row id before the user touches the mapping.\n *\n * The guess decides something destructive: the chosen column is taken out of\n * the values and sent as the id of an upsert, so every row overwrites the record\n * that already has that id. A header that merely *contained* \"id\" or \"key\" used\n * to qualify, so a file starting with `width`, `video` or `provider` wrote its\n * widths over the records whose ids they happened to equal.\n *\n * Only the first column is a candidate — the one the export writes the id to —\n * and only when its name says it is the id: the header is `id` (in any case),\n * or it maps onto a property the collection declares with `isId`. Anything else\n * is left for the user to choose in the mapping step.\n */\nexport function guessIdColumn(headers: string[],\n headersMapping: Record<string, string | null>,\n properties?: Properties): string | undefined {\n const firstKey = headers[0];\n if (firstKey === undefined) return undefined;\n\n if (firstKey.trim().toLowerCase() === \"id\") return firstKey;\n\n const mappedKey = headersMapping[firstKey];\n const mappedProperty = mappedKey ? properties?.[mappedKey] : undefined;\n if (mappedProperty && \"isId\" in mappedProperty && mappedProperty.isId) return firstKey;\n\n return undefined;\n}\n\n/**\n * The mapping and id column after the person importing picks where one column\n * goes, in that column's row of the mapping step: a property (`targetKey`), or\n * nothing (`null`, \"Do not import this property\").\n *\n * A property holds one column, so a column that held `targetKey` before lets\n * it go. And a column picked for a property, or for nothing, is no longer the\n * id: the row's select shows \"Use this column as ID\" while it is, and the\n * conversion takes the id column out of the values, so the pick was dropped.\n */\nexport function remapImportColumn({\n headersMapping,\n idColumn,\n column,\n targetKey\n}: {\n headersMapping: Record<string, string | null>;\n idColumn: string | undefined;\n column: string;\n targetKey: string | null;\n}): { headersMapping: Record<string, string | null>; idColumn: string | undefined } {\n const remapped: Record<string, string | null> = {};\n for (const [currentColumn, currentKey] of Object.entries(headersMapping)) {\n remapped[currentColumn] = currentColumn === column\n ? targetKey\n : targetKey !== null && currentKey === targetKey ? null : currentKey;\n }\n return {\n headersMapping: remapped,\n idColumn: idColumn === column ? undefined : idColumn\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,SAAgB,cAAc,SAC1B,gBACA,YAA6C;CAC7C,MAAM,WAAW,QAAQ;CACzB,IAAI,aAAa,KAAA,GAAW,OAAO,KAAA;CAEnC,IAAI,SAAS,KAAK,CAAC,CAAC,YAAY,MAAM,MAAM,OAAO;CAEnD,MAAM,YAAY,eAAe;CACjC,MAAM,iBAAiB,YAAY,aAAa,aAAa,KAAA;CAC7D,IAAI,kBAAkB,UAAU,kBAAkB,eAAe,MAAM,OAAO;AAGlF;;;;;;;;;;;AAYA,SAAgB,kBAAkB,EAC9B,gBACA,UACA,QACA,aAMgF;CAChF,MAAM,WAA0C,CAAC;CACjD,KAAK,MAAM,CAAC,eAAe,eAAe,OAAO,QAAQ,cAAc,GACnE,SAAS,iBAAiB,kBAAkB,SACtC,YACA,cAAc,QAAQ,eAAe,YAAY,OAAO;CAElE,OAAO;EACH,gBAAgB;EAChB,UAAU,aAAa,SAAS,KAAA,IAAY;CAChD;AACJ"}
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { isPrototypePollutingKey, pathTraversesPrototype } from "@rebasepro/utils";
|
|
2
2
|
//#region src/data_import/utils/transforms.ts
|
|
3
3
|
/**
|
|
4
|
+
* Make `holder[slot]` an array or an object to write into, unless it is one.
|
|
5
|
+
*
|
|
6
|
+
* A file can carry a plain column and nested ones under the same name — `tags`
|
|
7
|
+
* beside `tags[0]`, `address` beside `address.city`. The nested columns win:
|
|
8
|
+
* they say what shape the value has, and writing an index or a key into the
|
|
9
|
+
* text a plain column left there threw (strict mode), which failed the whole
|
|
10
|
+
* file.
|
|
11
|
+
*/
|
|
12
|
+
function containerAt(holder, slot, kind, column) {
|
|
13
|
+
const existing = holder[slot];
|
|
14
|
+
if (existing !== null && typeof existing === "object") return;
|
|
15
|
+
if (existing !== void 0 && existing !== null && existing !== "") console.warn(`Column "${column}" nests under "${String(slot)}", which another column set to a plain value; the nested columns are kept`);
|
|
16
|
+
holder[slot] = kind === "array" ? [] : {};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
4
19
|
* Take an object with keys of type `address.street`, `address.city` and
|
|
5
20
|
* convert it to an object with nested objects like `{ address: { street: ..., city: ... } }`
|
|
6
21
|
*
|
|
@@ -23,18 +38,20 @@ function unflattenObject(flatObj) {
|
|
|
23
38
|
let currentObj = nestedObj;
|
|
24
39
|
const keyParts = key.split(".");
|
|
25
40
|
keyParts.forEach((keyPart, i) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
41
|
+
const indexed = /^(.+)\[(\d+)\]$/.exec(keyPart);
|
|
42
|
+
if (indexed) {
|
|
43
|
+
const mainPropertyName = indexed[1];
|
|
44
|
+
const index = parseInt(indexed[2], 10);
|
|
45
|
+
containerAt(currentObj, mainPropertyName, "array", key);
|
|
30
46
|
if (i !== keyParts.length - 1) {
|
|
31
|
-
currentObj[mainPropertyName]
|
|
47
|
+
containerAt(currentObj[mainPropertyName], index, "object", key);
|
|
32
48
|
currentObj = currentObj[mainPropertyName][index];
|
|
33
49
|
} else currentObj[mainPropertyName][index] = flatObj[key];
|
|
34
50
|
} else if (i !== keyParts.length - 1) {
|
|
35
|
-
currentObj
|
|
51
|
+
containerAt(currentObj, keyPart, "object", key);
|
|
36
52
|
currentObj = currentObj[keyPart];
|
|
37
|
-
} else currentObj[keyPart]
|
|
53
|
+
} else if (currentObj[keyPart] !== null && typeof currentObj[keyPart] === "object") console.warn(`Column "${key}" is a plain value where nested columns already are; the nested columns are kept`);
|
|
54
|
+
else currentObj[keyPart] = flatObj[key];
|
|
38
55
|
});
|
|
39
56
|
return nestedObj;
|
|
40
57
|
}, {});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transforms.js","names":[],"sources":["../../../src/data_import/utils/transforms.ts"],"sourcesContent":["import { isPrototypePollutingKey, pathTraversesPrototype } from \"@rebasepro/utils\";\n\n/**\n * Take an object with keys of type `address.street`, `address.city` and\n * convert it to an object with nested objects like `{ address: { street: ..., city: ... } }`\n *\n * Keys here are the **header row of an uploaded file**, so they are attacker\n * data. `res[\"__proto__\"] = …` is the prototype setter rather than an own\n * property, so a column named `__proto__.polluted` walked out of the accumulator\n * and wrote onto `Object.prototype` for the life of the tab — and\n * `constructor.prototype.x` threw a `TypeError` that surfaced as an unreadable\n * file. Such a column is refused, not sanitised, the way `setIn` settled it\n * (`docs/bug-classes.md` class 22).\n *\n * @param flatObj\n */\nexport function unflattenObject(flatObj: { [key: string]: any }) {\n return Object.keys(flatObj).reduce((nestedObj, key) => {\n // The string form, so `toPath` rewrites `__proto__[0]` to a `__proto__`\n // segment too — the bracket branch below writes through the same setter.\n if (pathTraversesPrototype(key)) {\n console.warn(`Skipping column \"${key}\": a header may not reach the prototype chain`);\n return nestedObj;\n }\n let currentObj = nestedObj;\n const keyParts = key.split(\".\");\n keyParts.forEach((keyPart, i) => {\n\n
|
|
1
|
+
{"version":3,"file":"transforms.js","names":[],"sources":["../../../src/data_import/utils/transforms.ts"],"sourcesContent":["import { isPrototypePollutingKey, pathTraversesPrototype } from \"@rebasepro/utils\";\n\n/**\n * Make `holder[slot]` an array or an object to write into, unless it is one.\n *\n * A file can carry a plain column and nested ones under the same name — `tags`\n * beside `tags[0]`, `address` beside `address.city`. The nested columns win:\n * they say what shape the value has, and writing an index or a key into the\n * text a plain column left there threw (strict mode), which failed the whole\n * file.\n */\nfunction containerAt(holder: { [key: string]: any }, slot: string | number, kind: \"array\" | \"object\", column: string): void {\n const existing = holder[slot];\n if (existing !== null && typeof existing === \"object\") return;\n if (existing !== undefined && existing !== null && existing !== \"\") {\n console.warn(`Column \"${column}\" nests under \"${String(slot)}\", which another column set to a plain value; the nested columns are kept`);\n }\n holder[slot] = kind === \"array\" ? [] : {};\n}\n\n/**\n * Take an object with keys of type `address.street`, `address.city` and\n * convert it to an object with nested objects like `{ address: { street: ..., city: ... } }`\n *\n * Keys here are the **header row of an uploaded file**, so they are attacker\n * data. `res[\"__proto__\"] = …` is the prototype setter rather than an own\n * property, so a column named `__proto__.polluted` walked out of the accumulator\n * and wrote onto `Object.prototype` for the life of the tab — and\n * `constructor.prototype.x` threw a `TypeError` that surfaced as an unreadable\n * file. Such a column is refused, not sanitised, the way `setIn` settled it\n * (`docs/bug-classes.md` class 22).\n *\n * @param flatObj\n */\nexport function unflattenObject(flatObj: { [key: string]: any }) {\n return Object.keys(flatObj).reduce((nestedObj, key) => {\n // The string form, so `toPath` rewrites `__proto__[0]` to a `__proto__`\n // segment too — the bracket branch below writes through the same setter.\n if (pathTraversesPrototype(key)) {\n console.warn(`Skipping column \"${key}\": a header may not reach the prototype chain`);\n return nestedObj;\n }\n let currentObj = nestedObj;\n const keyParts = key.split(\".\");\n keyParts.forEach((keyPart, i) => {\n\n const indexed = /^(.+)\\[(\\d+)\\]$/.exec(keyPart);\n if (indexed) {\n const mainPropertyName = indexed[1];\n const index = parseInt(indexed[2], 10);\n\n containerAt(currentObj, mainPropertyName, \"array\", key);\n\n if (i !== keyParts.length - 1) {\n containerAt(currentObj[mainPropertyName], index, \"object\", key);\n currentObj = currentObj[mainPropertyName][index];\n } else {\n currentObj[mainPropertyName][index] = flatObj[key];\n }\n } else if (i !== keyParts.length - 1) {\n containerAt(currentObj, keyPart, \"object\", key);\n currentObj = currentObj[keyPart];\n } else if (currentObj[keyPart] !== null && typeof currentObj[keyPart] === \"object\") {\n // Nested columns already gave this name its shape; see `containerAt`.\n console.warn(`Column \"${key}\" is a plain value where nested columns already are; the nested columns are kept`);\n } else {\n currentObj[keyPart] = flatObj[key];\n }\n\n });\n return nestedObj;\n }, {} as { [key: string]: any });\n}\n\n/**\n * Read a text cell as the number, boolean or array it spells, when it spells\n * one exactly — so the type inference sees `12`, `true` and the export's\n * `[\"a\",\"b\"]` for what they are.\n *\n * Only a cell that is the canonical JSON of its value is read: `JSON.stringify`\n * of the result must give back the cell's text. That makes the read lossless,\n * and the import coerces each value against the property it lands in only\n * later — a string property turns it back into exactly the text in the file.\n * Anything else stays the text it was: `1.10`, `00123`, `1E3`, a twenty-digit\n * SKU that a double cannot hold, the word `null`, and a cell with quotes in it.\n * So does an object: the import flattens objects into `a.b` columns, which\n * would scatter a string property's JSON text into columns that do not exist.\n */\nfunction parseCanonicalJsonCell(cell: unknown): unknown {\n if (typeof cell !== \"string\") return cell;\n // The canonical JSON of a number, boolean or array starts with `-`, a\n // digit, `t`, `f` or `[` — nothing else can pass the round trip below. Text\n // is most of a file, and `JSON.parse` on text throws: the throw was over\n // half of the main-thread time of reading one.\n if (!couldBeCanonicalJson(cell.charCodeAt(0))) return cell;\n let parsed: unknown;\n try {\n parsed = JSON.parse(cell);\n } catch (e) {\n return cell;\n }\n const readable = typeof parsed === \"number\" || typeof parsed === \"boolean\" || Array.isArray(parsed);\n return readable && JSON.stringify(parsed) === cell ? parsed : cell;\n}\n\nfunction couldBeCanonicalJson(firstChar: number): boolean {\n return firstChar === 45 /* - */\n || (firstChar >= 48 && firstChar <= 57) /* 0-9 */\n || firstChar === 116 /* t */\n || firstChar === 102 /* f */\n || firstChar === 91; /* [ */\n}\n\nexport function mapJsonParse(obj: Record<string, any>) {\n return Object.keys(obj).reduce((acc: Record<string, any>, key) => {\n // Same header row, same setter: `acc[\"__proto__\"] = value` replaces the\n // accumulator's prototype instead of adding a column.\n if (isPrototypePollutingKey(key)) {\n console.warn(`Skipping column \"${key}\": a header may not reach the prototype chain`);\n return acc;\n }\n acc[key] = parseCanonicalJsonCell(obj[key]);\n return acc;\n }, {});\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAS,YAAY,QAAgC,MAAuB,MAA0B,QAAsB;CACxH,MAAM,WAAW,OAAO;CACxB,IAAI,aAAa,QAAQ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AAO;CACtD,IAAI;CACJ,IAAI;EACA,SAAS,KAAK,MAAM,IAAI;CAC5B,SAAS,GAAG;EACR,OAAO;CACX;CAEA,QADiB,OAAO,WAAW,YAAY,OAAO,WAAW,aAAa,MAAM,QAAQ,MAAM,MAC/E,KAAK,UAAU,MAAM,MAAM,OAAO,SAAS;AAClE;AAEA,SAAS,qBAAqB,WAA4B;CACtD,OAAO,cAAc,MACb,aAAa,MAAM,aAAa,MACjC,cAAc,OACd,cAAc,OACd,cAAc;AACzB;AAEA,SAAgB,aAAa,KAA0B;CACnD,OAAO,OAAO,KAAK,GAAG,CAAC,CAAC,QAAQ,KAA0B,QAAQ;EAG9D,IAAI,wBAAwB,GAAG,GAAG;GAC9B,QAAQ,KAAK,oBAAoB,IAAI,8CAA8C;GACnF,OAAO;EACX;EACA,IAAI,OAAO,uBAAuB,IAAI,IAAI;EAC1C,OAAO;CACX,GAAG,CAAC,CAAC;AACT"}
|
|
@@ -9,4 +9,4 @@ import type { EntityFormProps } from "../types/components/EntityFormProps.js";
|
|
|
9
9
|
*
|
|
10
10
|
* @group Components
|
|
11
11
|
*/
|
|
12
|
-
export declare function EntityForm<M extends Record<string, unknown>>({ path, entityId: entityIdProp, collection, onValuesModified, onIdChange, onSaved, entity, initialDirtyValues, onFormContextReady, initialStatus, className, onStatusChange, onEntityChange, openEntityMode, formex: formexProp, disabled: disabledProp, Builder, parentCollectionSlugs, parentEntityIds, EntityFormActionsComponent, showDefaultActions, navigateBack: navigateBackProp, children, onSubmit: onSubmitProp, onValuesChangeDeferred: onValuesChangeDeferredProp, onReset: onResetProp, uniqueFieldValidator: uniqueFieldValidatorProp, lockedPropertyKeys, beforeFields, afterFields, pluginActions: pluginActionsProp, computedInitialValues, hasLocalChanges: hasLocalChangesProp, localChangesData, manualApplyLocalChanges, localChangesCacheKey, onClearLocalChanges }: EntityFormProps<M>): React.JSX.Element;
|
|
12
|
+
export declare function EntityForm<M extends Record<string, unknown>>({ path, entityId: entityIdProp, collection, onValuesModified, onIdChange, onSaved, entity: entityProp, initialDirtyValues, onFormContextReady, initialStatus, className, onStatusChange, onEntityChange, openEntityMode, formex: formexProp, disabled: disabledProp, Builder, parentCollectionSlugs, parentEntityIds, EntityFormActionsComponent, showDefaultActions, navigateBack: navigateBackProp, children, onSubmit: onSubmitProp, onValuesChangeDeferred: onValuesChangeDeferredProp, onReset: onResetProp, uniqueFieldValidator: uniqueFieldValidatorProp, lockedPropertyKeys, beforeFields, afterFields, pluginActions: pluginActionsProp, computedInitialValues, hasLocalChanges: hasLocalChangesProp, localChangesData, manualApplyLocalChanges, localChangesCacheKey, onClearLocalChanges }: EntityFormProps<M>): React.JSX.Element;
|
package/dist/form/EntityForm.js
CHANGED
|
@@ -18,7 +18,7 @@ import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "rea
|
|
|
18
18
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
19
19
|
import { deepEqual } from "fast-equals";
|
|
20
20
|
import { Alert, AlignLeftIcon, Button, CircularProgressCenter, Dialog, DialogActions, DialogContent, DialogTitle, ErrorBoundary, Typography, cls, defaultBorderMixin, isKeyHandled, paperMixin, useDebouncedCallback } from "@rebasepro/ui";
|
|
21
|
-
import { effectiveAccess, getCopyValues, getDefaultValuesFor } from "@rebasepro/common";
|
|
21
|
+
import { effectiveAccess, getCopyValues, getDefaultValuesFor, hasEntityId } from "@rebasepro/common";
|
|
22
22
|
import { flattenKeys, getFormFieldKeys, isDisabled, isHidden, isReadOnly, resolveFormLayout, useAuthController, useCustomizationController, useRebaseContext, useTranslation } from "@rebasepro/app";
|
|
23
23
|
import { Formex, useCreateFormex } from "@rebasepro/forms";
|
|
24
24
|
//#region src/form/EntityForm.tsx
|
|
@@ -31,12 +31,21 @@ import { Formex, useCreateFormex } from "@rebasepro/forms";
|
|
|
31
31
|
*
|
|
32
32
|
* @group Components
|
|
33
33
|
*/
|
|
34
|
-
function EntityForm({ path, entityId: entityIdProp, collection, onValuesModified, onIdChange, onSaved, entity, initialDirtyValues, onFormContextReady, initialStatus, className, onStatusChange, onEntityChange, openEntityMode = "full_screen", formex: formexProp, disabled: disabledProp, Builder, parentCollectionSlugs, parentEntityIds, EntityFormActionsComponent = EntityFormActions, showDefaultActions = true, navigateBack: navigateBackProp, children, onSubmit: onSubmitProp, onValuesChangeDeferred: onValuesChangeDeferredProp, onReset: onResetProp, uniqueFieldValidator: uniqueFieldValidatorProp, lockedPropertyKeys, beforeFields, afterFields, pluginActions: pluginActionsProp, computedInitialValues, hasLocalChanges: hasLocalChangesProp, localChangesData, manualApplyLocalChanges, localChangesCacheKey, onClearLocalChanges }) {
|
|
34
|
+
function EntityForm({ path, entityId: entityIdProp, collection, onValuesModified, onIdChange, onSaved, entity: entityProp, initialDirtyValues, onFormContextReady, initialStatus, className, onStatusChange, onEntityChange, openEntityMode = "full_screen", formex: formexProp, disabled: disabledProp, Builder, parentCollectionSlugs, parentEntityIds, EntityFormActionsComponent = EntityFormActions, showDefaultActions = true, navigateBack: navigateBackProp, children, onSubmit: onSubmitProp, onValuesChangeDeferred: onValuesChangeDeferredProp, onReset: onResetProp, uniqueFieldValidator: uniqueFieldValidatorProp, lockedPropertyKeys, beforeFields, afterFields, pluginActions: pluginActionsProp, computedInitialValues, hasLocalChanges: hasLocalChangesProp, localChangesData, manualApplyLocalChanges, localChangesCacheKey, onClearLocalChanges }) {
|
|
35
35
|
const navigateBack = useCallback(() => {
|
|
36
36
|
if (navigateBackProp) navigateBackProp();
|
|
37
37
|
}, [navigateBackProp]);
|
|
38
38
|
const authController = useAuthController();
|
|
39
39
|
const [status, setStatus] = useState(initialStatus);
|
|
40
|
+
/**
|
|
41
|
+
* The record as this form last saved it. A create turns the form into the
|
|
42
|
+
* editor of the record it made, and the `entity` prop names that record
|
|
43
|
+
* only once the caller has caught up — a caller that never does (the form
|
|
44
|
+
* used on its own) left an "existing" form with no record: it re-seeded
|
|
45
|
+
* itself with the defaults and refused the next Save.
|
|
46
|
+
*/
|
|
47
|
+
const [savedEntity, setSavedEntity] = useState(void 0);
|
|
48
|
+
const entity = entityProp ?? (status === "existing" ? savedEntity : void 0);
|
|
40
49
|
const updateStatus = (status) => {
|
|
41
50
|
setStatus(status);
|
|
42
51
|
onStatusChange?.(status);
|
|
@@ -214,6 +223,7 @@ function EntityForm({ path, entityId: entityIdProp, collection, onValuesModified
|
|
|
214
223
|
return () => window.removeEventListener("keydown", handleKeyDown);
|
|
215
224
|
}, [formex]);
|
|
216
225
|
const afterSave = (updatedEntity) => {
|
|
226
|
+
setSavedEntity(updatedEntity);
|
|
217
227
|
onResetProp?.();
|
|
218
228
|
onValuesModified?.(false, updatedEntity.values);
|
|
219
229
|
onEntityChange?.(updatedEntity);
|
|
@@ -582,7 +592,7 @@ function EntityForm({ path, entityId: entityIdProp, collection, onValuesModified
|
|
|
582
592
|
children: t("entity_does_not_exist")
|
|
583
593
|
}),
|
|
584
594
|
children,
|
|
585
|
-
initialEntityId && !entity && initialStatus !== "new" && /* @__PURE__ */ jsx(Alert, {
|
|
595
|
+
hasEntityId(initialEntityId) && !entity && initialStatus !== "new" && /* @__PURE__ */ jsx(Alert, {
|
|
586
596
|
color: "info",
|
|
587
597
|
size: "small",
|
|
588
598
|
children: t("entity_does_not_exist")
|
|
@@ -638,7 +648,7 @@ function EntityForm({ path, entityId: entityIdProp, collection, onValuesModified
|
|
|
638
648
|
})
|
|
639
649
|
] }) });
|
|
640
650
|
useEffect(() => {
|
|
641
|
-
if (entityId && onIdChange) onIdChange(entityId);
|
|
651
|
+
if (hasEntityId(entityId) && onIdChange) onIdChange(entityId);
|
|
642
652
|
}, [entityId, onIdChange]);
|
|
643
653
|
if (!collection || !path) throw Error("INTERNAL: Collection and path must be defined in form context");
|
|
644
654
|
const dialogActions = /* @__PURE__ */ jsx(EntityFormActionsComponent, {
|