@wix/bex-core 2.400.0 → 2.401.0

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.
Files changed (51) hide show
  1. package/dist/cjs/hooks/useImport.js +2 -0
  2. package/dist/cjs/hooks/useImport.js.map +1 -1
  3. package/dist/cjs/state/ImportState/ImportState.js +67 -17
  4. package/dist/cjs/state/ImportState/ImportState.js.map +1 -1
  5. package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js +248 -0
  6. package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
  7. package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js +30 -4
  8. package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
  9. package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js +91 -0
  10. package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
  11. package/dist/cjs/state/ImportState/detectFieldType.js +3 -3
  12. package/dist/cjs/state/ImportState/detectFieldType.js.map +1 -1
  13. package/dist/cjs/state/ImportState/importFieldCompatibility.js +92 -0
  14. package/dist/cjs/state/ImportState/importFieldCompatibility.js.map +1 -0
  15. package/dist/cjs/state/ImportState/importSampling.js +6 -0
  16. package/dist/cjs/state/ImportState/importSampling.js.map +1 -0
  17. package/dist/cjs/state/ImportState/index.js.map +1 -1
  18. package/dist/cjs/types/Field.js.map +1 -1
  19. package/dist/esm/hooks/useImport.js +2 -1
  20. package/dist/esm/hooks/useImport.js.map +1 -1
  21. package/dist/esm/state/ImportState/ImportState.js +68 -17
  22. package/dist/esm/state/ImportState/ImportState.js.map +1 -1
  23. package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js +187 -2
  24. package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
  25. package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js +32 -4
  26. package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
  27. package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js +99 -0
  28. package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
  29. package/dist/esm/state/ImportState/detectFieldType.js +3 -3
  30. package/dist/esm/state/ImportState/detectFieldType.js.map +1 -1
  31. package/dist/esm/state/ImportState/importFieldCompatibility.js +102 -0
  32. package/dist/esm/state/ImportState/importFieldCompatibility.js.map +1 -0
  33. package/dist/esm/state/ImportState/importSampling.js +2 -0
  34. package/dist/esm/state/ImportState/importSampling.js.map +1 -0
  35. package/dist/esm/state/ImportState/index.js.map +1 -1
  36. package/dist/types/hooks/useImport.d.ts +2 -1
  37. package/dist/types/hooks/useImport.d.ts.map +1 -1
  38. package/dist/types/state/ImportState/ImportState.d.ts +10 -1
  39. package/dist/types/state/ImportState/ImportState.d.ts.map +1 -1
  40. package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts +2 -0
  41. package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts.map +1 -0
  42. package/dist/types/state/ImportState/detectFieldType.d.ts.map +1 -1
  43. package/dist/types/state/ImportState/importFieldCompatibility.d.ts +4 -0
  44. package/dist/types/state/ImportState/importFieldCompatibility.d.ts.map +1 -0
  45. package/dist/types/state/ImportState/importSampling.d.ts +2 -0
  46. package/dist/types/state/ImportState/importSampling.d.ts.map +1 -0
  47. package/dist/types/state/ImportState/index.d.ts +1 -0
  48. package/dist/types/state/ImportState/index.d.ts.map +1 -1
  49. package/dist/types/types/Field.d.ts +4 -0
  50. package/dist/types/types/Field.d.ts.map +1 -1
  51. package/package.json +4 -4
@@ -11,6 +11,7 @@ function useCreateImport(params) {
11
11
  importInputs,
12
12
  fieldsSource,
13
13
  detectColumnType,
14
+ isValueCompatible,
14
15
  importModalState,
15
16
  reportBi
16
17
  } = params;
@@ -30,6 +31,7 @@ function useCreateImport(params) {
30
31
  fieldsSource,
31
32
  httpClient,
32
33
  detectColumnType,
34
+ isValueCompatible,
33
35
  collection,
34
36
  importModalState,
35
37
  showToast,
@@ -1 +1 @@
1
- {"version":3,"names":["_react","require","_providers","_state","useCreateImport","params","config","importInputs","fieldsSource","detectColumnType","importModalState","reportBi","container","useWixPatternsContainer","showToast","translate","errorHandler","window","httpClient","collection","useCollectionContext","useCallback","ImportState","downloadCsv","content","fileName","download","encodeURIComponent"],"sources":["../../../src/hooks/useImport.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { useWixPatternsContainer, useCollectionContext } from '../providers';\nimport {\n ModalState,\n ImportState,\n ResolvedImportConfig,\n ImportConfig,\n DetectColumnType,\n} from '../state';\nimport type { FieldsSource } from '../types/FieldsSource';\nimport { FiltersMap } from '../model';\nimport type { ReportBI } from '../services';\n\nexport interface UseImportParams {\n /** Consumer inputs (destination, extra fields, backup, …). The collection,\n * fields, and create-new-field are read from `fieldsSource`. */\n importInputs?: ImportConfig;\n /** The table's live fields source; `ImportState` builds the config from it. */\n fieldsSource?: FieldsSource;\n /** A fully pre-built config, used instead of building from the inputs/source. */\n config?: ResolvedImportConfig;\n detectColumnType?: DetectColumnType;\n importModalState: ModalState;\n /** BI reporter for the import funnel; defaults to the container's. */\n reportBi?: ReportBI;\n}\n\nexport function useCreateImport<T = unknown, F extends FiltersMap = FiltersMap>(\n params: UseImportParams,\n) {\n const {\n config,\n importInputs,\n fieldsSource,\n detectColumnType,\n importModalState,\n reportBi,\n } = params;\n const container = useWixPatternsContainer();\n const { showToast, translate, errorHandler, window, httpClient } = container;\n const collection = useCollectionContext<T, F>();\n\n return useCallback(() => {\n return new ImportState({\n config,\n importInputs,\n fieldsSource,\n httpClient,\n detectColumnType,\n collection,\n importModalState,\n showToast,\n translate,\n errorHandler,\n reportBi: reportBi ?? container.reportBi,\n downloadCsv: (content, fileName) => {\n window.download(\n `data:text/csv;charset=utf-8,${encodeURIComponent(content)}`,\n fileName,\n );\n },\n });\n }, []);\n}\n"],"mappings":";;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,UAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AAyBO,SAASG,eAAeA,CAC7BC,MAAuB,EACvB;EACA,MAAM;IACJC,MAAM;IACNC,YAAY;IACZC,YAAY;IACZC,gBAAgB;IAChBC,gBAAgB;IAChBC;EACF,CAAC,GAAGN,MAAM;EACV,MAAMO,SAAS,GAAG,IAAAC,kCAAuB,EAAC,CAAC;EAC3C,MAAM;IAAEC,SAAS;IAAEC,SAAS;IAAEC,YAAY;IAAEC,MAAM;IAAEC;EAAW,CAAC,GAAGN,SAAS;EAC5E,MAAMO,UAAU,GAAG,IAAAC,+BAAoB,EAAO,CAAC;EAE/C,OAAO,IAAAC,kBAAW,EAAC,MAAM;IACvB,OAAO,IAAIC,kBAAW,CAAC;MACrBhB,MAAM;MACNC,YAAY;MACZC,YAAY;MACZU,UAAU;MACVT,gBAAgB;MAChBU,UAAU;MACVT,gBAAgB;MAChBI,SAAS;MACTC,SAAS;MACTC,YAAY;MACZL,QAAQ,EAAEA,QAAQ,IAAIC,SAAS,CAACD,QAAQ;MACxCY,WAAW,EAAEA,CAACC,OAAO,EAAEC,QAAQ,KAAK;QAClCR,MAAM,CAACS,QAAQ,CACb,+BAA+BC,kBAAkB,CAACH,OAAO,CAAC,EAAE,EAC5DC,QACF,CAAC;MACH;IACF,CAAC,CAAC;EACJ,CAAC,EAAE,EAAE,CAAC;AACR","ignoreList":[]}
1
+ {"version":3,"names":["_react","require","_providers","_state","useCreateImport","params","config","importInputs","fieldsSource","detectColumnType","isValueCompatible","importModalState","reportBi","container","useWixPatternsContainer","showToast","translate","errorHandler","window","httpClient","collection","useCollectionContext","useCallback","ImportState","downloadCsv","content","fileName","download","encodeURIComponent"],"sources":["../../../src/hooks/useImport.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { useWixPatternsContainer, useCollectionContext } from '../providers';\nimport {\n ModalState,\n ImportState,\n ResolvedImportConfig,\n ImportConfig,\n DetectColumnType,\n IsImportValueCompatible,\n} from '../state';\nimport type { FieldsSource } from '../types/FieldsSource';\nimport { FiltersMap } from '../model';\nimport type { ReportBI } from '../services';\n\nexport interface UseImportParams {\n /** Consumer inputs (destination, extra fields, backup, …). The collection,\n * fields, and create-new-field are read from `fieldsSource`. */\n importInputs?: ImportConfig;\n /** The table's live fields source; `ImportState` builds the config from it. */\n fieldsSource?: FieldsSource;\n /** A fully pre-built config, used instead of building from the inputs/source. */\n config?: ResolvedImportConfig;\n detectColumnType?: DetectColumnType;\n isValueCompatible?: IsImportValueCompatible;\n importModalState: ModalState;\n /** BI reporter for the import funnel; defaults to the container's. */\n reportBi?: ReportBI;\n}\n\nexport function useCreateImport<T = unknown, F extends FiltersMap = FiltersMap>(\n params: UseImportParams,\n) {\n const {\n config,\n importInputs,\n fieldsSource,\n detectColumnType,\n isValueCompatible,\n importModalState,\n reportBi,\n } = params;\n const container = useWixPatternsContainer();\n const { showToast, translate, errorHandler, window, httpClient } = container;\n const collection = useCollectionContext<T, F>();\n\n return useCallback(() => {\n return new ImportState({\n config,\n importInputs,\n fieldsSource,\n httpClient,\n detectColumnType,\n isValueCompatible,\n collection,\n importModalState,\n showToast,\n translate,\n errorHandler,\n reportBi: reportBi ?? container.reportBi,\n downloadCsv: (content, fileName) => {\n window.download(\n `data:text/csv;charset=utf-8,${encodeURIComponent(content)}`,\n fileName,\n );\n },\n });\n }, []);\n}\n"],"mappings":";;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,UAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AA2BO,SAASG,eAAeA,CAC7BC,MAAuB,EACvB;EACA,MAAM;IACJC,MAAM;IACNC,YAAY;IACZC,YAAY;IACZC,gBAAgB;IAChBC,iBAAiB;IACjBC,gBAAgB;IAChBC;EACF,CAAC,GAAGP,MAAM;EACV,MAAMQ,SAAS,GAAG,IAAAC,kCAAuB,EAAC,CAAC;EAC3C,MAAM;IAAEC,SAAS;IAAEC,SAAS;IAAEC,YAAY;IAAEC,MAAM;IAAEC;EAAW,CAAC,GAAGN,SAAS;EAC5E,MAAMO,UAAU,GAAG,IAAAC,+BAAoB,EAAO,CAAC;EAE/C,OAAO,IAAAC,kBAAW,EAAC,MAAM;IACvB,OAAO,IAAIC,kBAAW,CAAC;MACrBjB,MAAM;MACNC,YAAY;MACZC,YAAY;MACZW,UAAU;MACVV,gBAAgB;MAChBC,iBAAiB;MACjBU,UAAU;MACVT,gBAAgB;MAChBI,SAAS;MACTC,SAAS;MACTC,YAAY;MACZL,QAAQ,EAAEA,QAAQ,IAAIC,SAAS,CAACD,QAAQ;MACxCY,WAAW,EAAEA,CAACC,OAAO,EAAEC,QAAQ,KAAK;QAClCR,MAAM,CAACS,QAAQ,CACb,+BAA+BC,kBAAkB,CAACH,OAAO,CAAC,EAAE,EAC5DC,QACF,CAAC;MACH;IACF,CAAC,CAAC;EACJ,CAAC,EAAE,EAAE,CAAC;AACR","ignoreList":[]}
@@ -14,6 +14,7 @@ var _rewriteCsv = require("./rewriteCsv");
14
14
  var _hubJobClient = require("./hubJobClient");
15
15
  var _importJobPoller = require("./importJobPoller");
16
16
  var _detectFieldType = require("./detectFieldType");
17
+ var _importFieldCompatibility = require("./importFieldCompatibility");
17
18
  var _fieldEdit = require("./fieldEdit");
18
19
  var _newFieldDraft = require("./newFieldDraft");
19
20
  var _ImportStateBIReporter = require("./ImportStateBIReporter");
@@ -50,7 +51,8 @@ var _ImportStateBIReporter = require("./ImportStateBIReporter");
50
51
  // `id`/`header`): a field the destination mandates. Consumers set it — the
51
52
  // schema-backed config forwards `validation.required`, manual/platformized
52
53
  // configs set it per field — and the mapping step blocks import while any
53
- // required field has no column mapped to it (CAIRO-4452).
54
+ // required field has no column mapped to it (CAIRO-4452). `type` hides the field
55
+ // from the mapping dropdown of columns whose data doesn't fit it (CAIRO-4392).
54
56
 
55
57
  // `NewFieldType`, `detectColumnType`, etc. are re-exported from the package via
56
58
  // the `ImportState` barrel (`./index`), which also exports `./detectFieldType`.
@@ -211,7 +213,11 @@ const buildImportConfig = (fieldsSource, collection, inputs = {}) => {
211
213
  return {
212
214
  id: f.id,
213
215
  header: f.displayName,
214
- required: (_f$validation = f.validation) == null ? void 0 : _f$validation.required
216
+ required: (_f$validation = f.validation) == null ? void 0 : _f$validation.required,
217
+ type: f.type,
218
+ ...(f.arrayMetadata && {
219
+ arrayMetadata: f.arrayMetadata
220
+ })
215
221
  };
216
222
  });
217
223
  return [...fields, ...live];
@@ -302,6 +308,21 @@ function uniqueFieldNameAndKey(header, usedNames, usedKeys, requestedKey) {
302
308
  displayName = `${header}-${suffix}`;
303
309
  }
304
310
  }
311
+ function uniqueColumnHeaders(headers) {
312
+ const reserved = new Set(headers);
313
+ const used = new Set();
314
+ return headers.map(header => {
315
+ let unique = header;
316
+ let suffix = 2;
317
+ while (used.has(unique)) {
318
+ do {
319
+ unique = `${header} (${suffix++})`;
320
+ } while (reserved.has(unique));
321
+ }
322
+ used.add(unique);
323
+ return unique;
324
+ });
325
+ }
305
326
  class ImportState {
306
327
  constructor(props) {
307
328
  // The import inputs, held by reference so a live schema-backed config's getters
@@ -317,6 +338,7 @@ class ImportState {
317
338
  (0, _defineProperty2.default)(this, "bi", void 0);
318
339
  (0, _defineProperty2.default)(this, "hubJobClient", void 0);
319
340
  (0, _defineProperty2.default)(this, "_detectColumnType", void 0);
341
+ (0, _defineProperty2.default)(this, "_isValueCompatible", void 0);
320
342
  (0, _defineProperty2.default)(this, "discardChangesModal", new _ConditionalModalState.ConditionalModalState());
321
343
  (0, _defineProperty2.default)(this, "currentStep", 'upload');
322
344
  (0, _defineProperty2.default)(this, "importStatus", {
@@ -382,6 +404,7 @@ class ImportState {
382
404
  this.reportBi = props.reportBi ?? NOOP_REPORT_BI;
383
405
  this.bi = new _ImportStateBIReporter.ImportStateBIReporter(this);
384
406
  this._detectColumnType = props.detectColumnType ?? _detectFieldType.detectColumnType;
407
+ this._isValueCompatible = props.isValueCompatible;
385
408
  this.hubJobClient = (0, _hubJobClient.createHubJobClient)({
386
409
  httpClient: props.httpClient,
387
410
  errorHandler: props.errorHandler
@@ -392,6 +415,13 @@ class ImportState {
392
415
  fileName: _mobx.observable.ref,
393
416
  csvHeaders: _mobx.observable.ref,
394
417
  csvRows: _mobx.observable.ref,
418
+ // Auto-mapping reads these before the configuration step observes them.
419
+ _columnSamples: (0, _mobx.computed)({
420
+ keepAlive: true
421
+ }),
422
+ _compatibleFieldsByHeader: (0, _mobx.computed)({
423
+ keepAlive: true
424
+ }),
395
425
  orderedHeaders: _mobx.observable.ref,
396
426
  mappingByHeader: _mobx.observable.ref,
397
427
  writePolicy: _mobx.observable.ref,
@@ -535,6 +565,15 @@ class ImportState {
535
565
  get selectedFieldIds() {
536
566
  return new Set(Object.values(this.mappingByHeader).filter(m => (m == null ? void 0 : m.kind) === 'existing').map(m => m.fieldId));
537
567
  }
568
+ get _compatibleFieldsByHeader() {
569
+ const fields = this.fields;
570
+ return new Map(this.csvHeaders.map((header, index) => [header, new Set(fields.filter(field => !field.type || (0, _importFieldCompatibility.isImportColumnCompatible)(field.type, this._columnValues(index), field.arrayMetadata, this._isValueCompatible)).map(field => field.id))]));
571
+ }
572
+ fieldsForHeader(header) {
573
+ const compatibleIds = this._compatibleFieldsByHeader.get(header);
574
+ const mapping = this.mappingByHeader[header];
575
+ return this.fields.filter(field => field.type !== 'UNKNOWN' && ((compatibleIds == null ? void 0 : compatibleIds.has(field.id)) || (mapping == null ? void 0 : mapping.kind) === 'existing' && mapping.fieldId === field.id));
576
+ }
538
577
 
539
578
  /**
540
579
  * Required destination fields that no CSV column is mapped to (CAIRO-4452).
@@ -748,20 +787,19 @@ class ImportState {
748
787
  this.currentStep = 'configuration';
749
788
  }
750
789
  }
751
- getDefaultMapping(headers) {
790
+ getDefaultMapping(headers, originalHeaders) {
752
791
  const mapping = {};
753
- const fields = this.fields.map(field => ({
754
- id: field.id ?? '',
755
- header: field.header ?? ''
756
- }));
792
+ const matchedIds = new Set();
793
+ const fields = this.fields.filter(field => field.type !== 'UNKNOWN');
757
794
  headers.forEach((header, index) => {
758
- const normalizedHeader = normalize(header);
759
- const matched = fields.find(field => normalize(field.id) === normalizedHeader || normalize(field.header) === normalizedHeader);
795
+ const normalizedHeader = normalize(originalHeaders[index]);
796
+ const matched = fields.find(field => field.id != null && !matchedIds.has(field.id) && (normalize(field.id) === normalizedHeader || normalize(field.header ?? '') === normalizedHeader));
760
797
  if (matched != null && matched.id) {
761
798
  mapping[header] = {
762
799
  kind: 'existing',
763
800
  fieldId: matched.id
764
801
  };
802
+ matchedIds.add(matched.id);
765
803
  } else {
766
804
  // Unmatched columns default to creating a new field (type inferred from
767
805
  // the column's data) when the consumer supplied `updateSchema`;
@@ -774,12 +812,23 @@ class ImportState {
774
812
  });
775
813
  return mapping;
776
814
  }
777
-
778
- // The column's values, capped at the detection sample size — detection only
779
- // looks at the first `TYPE_SAMPLE_SIZE` values, so avoid copying the whole
780
- // column (a wide CSV would otherwise allocate rows × columns strings).
815
+ get _columnSamples() {
816
+ return this.csvHeaders.map((_, columnIndex) => {
817
+ const values = [];
818
+ for (const row of this.csvRows) {
819
+ const value = row[columnIndex] ?? '';
820
+ if (value.trim() !== '') {
821
+ values.push(value);
822
+ if (values.length === _detectFieldType.TYPE_SAMPLE_SIZE) {
823
+ break;
824
+ }
825
+ }
826
+ }
827
+ return values;
828
+ });
829
+ }
781
830
  _columnValues(columnIndex) {
782
- return this.csvRows.slice(0, _detectFieldType.TYPE_SAMPLE_SIZE).map(row => row[columnIndex] ?? '');
831
+ return this._columnSamples[columnIndex] ?? [];
783
832
  }
784
833
 
785
834
  // Puts the `_id`-mapped column first (CAIRO-4418), preserving CSV order for the
@@ -792,13 +841,14 @@ class ImportState {
792
841
  this._pendingFieldsMap = undefined;
793
842
  this._createdFields.clear();
794
843
  this.fileName = fileName;
795
- this.csvHeaders = parsedCsv.headers;
844
+ // Mapping, compatibility and upload rewriting all key columns by this name.
845
+ this.csvHeaders = uniqueColumnHeaders(parsedCsv.headers);
796
846
  this.csvRows = parsedCsv.rows;
797
- this.mappingByHeader = this.getDefaultMapping(parsedCsv.headers);
847
+ this.mappingByHeader = this.getDefaultMapping(this.csvHeaders, parsedCsv.headers);
798
848
  // Snapshot the row order for this file with the auto-mapped `_id` column
799
849
  // first. Reads `idMappedHeader`, so it must run after `mappingByHeader` is
800
850
  // set. Kept stable until the next file so mapping edits don't reorder rows.
801
- this.orderedHeaders = this._orderHeadersWithIdFirst(parsedCsv.headers);
851
+ this.orderedHeaders = this._orderHeadersWithIdFirst(this.csvHeaders);
802
852
  // A new file is a fresh import: start from the default policy so a prior
803
853
  // file's Skip choice doesn't silently carry over (only the new-file path
804
854
  // resets — remapping `_id` within the same file keeps the user's choice).