@wix/bex-core 2.399.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 (131) hide show
  1. package/dist/cjs/assets/locale/messages_ar.json +20 -20
  2. package/dist/cjs/assets/locale/messages_bg.json +20 -20
  3. package/dist/cjs/assets/locale/messages_ca.json +20 -20
  4. package/dist/cjs/assets/locale/messages_cs.json +20 -20
  5. package/dist/cjs/assets/locale/messages_da.json +20 -20
  6. package/dist/cjs/assets/locale/messages_de.json +20 -20
  7. package/dist/cjs/assets/locale/messages_el.json +20 -20
  8. package/dist/cjs/assets/locale/messages_en.json +20 -20
  9. package/dist/cjs/assets/locale/messages_es.json +20 -20
  10. package/dist/cjs/assets/locale/messages_fi.json +20 -20
  11. package/dist/cjs/assets/locale/messages_fr.json +20 -20
  12. package/dist/cjs/assets/locale/messages_he.json +20 -20
  13. package/dist/cjs/assets/locale/messages_hi.json +20 -20
  14. package/dist/cjs/assets/locale/messages_hu.json +20 -20
  15. package/dist/cjs/assets/locale/messages_id.json +20 -20
  16. package/dist/cjs/assets/locale/messages_it.json +20 -20
  17. package/dist/cjs/assets/locale/messages_ja.json +20 -20
  18. package/dist/cjs/assets/locale/messages_ko.json +20 -20
  19. package/dist/cjs/assets/locale/messages_lt.json +20 -20
  20. package/dist/cjs/assets/locale/messages_ms.json +20 -20
  21. package/dist/cjs/assets/locale/messages_nl.json +20 -20
  22. package/dist/cjs/assets/locale/messages_no.json +20 -20
  23. package/dist/cjs/assets/locale/messages_pl.json +20 -20
  24. package/dist/cjs/assets/locale/messages_pt.json +20 -20
  25. package/dist/cjs/assets/locale/messages_ro.json +20 -20
  26. package/dist/cjs/assets/locale/messages_ru.json +20 -20
  27. package/dist/cjs/assets/locale/messages_sk.json +20 -20
  28. package/dist/cjs/assets/locale/messages_sl.json +20 -20
  29. package/dist/cjs/assets/locale/messages_sv.json +20 -20
  30. package/dist/cjs/assets/locale/messages_th.json +20 -20
  31. package/dist/cjs/assets/locale/messages_tl.json +20 -20
  32. package/dist/cjs/assets/locale/messages_tr.json +20 -20
  33. package/dist/cjs/assets/locale/messages_uk.json +20 -20
  34. package/dist/cjs/assets/locale/messages_vi.json +20 -20
  35. package/dist/cjs/assets/locale/messages_zh.json +20 -20
  36. package/dist/cjs/assets/locale/version_file +1 -1
  37. package/dist/cjs/assets/visual-e2e-locale/messages_visuale2e.json +19 -19
  38. package/dist/cjs/hooks/useImport.js +7 -2
  39. package/dist/cjs/hooks/useImport.js.map +1 -1
  40. package/dist/cjs/state/ImportState/ImportState.js +187 -34
  41. package/dist/cjs/state/ImportState/ImportState.js.map +1 -1
  42. package/dist/cjs/state/ImportState/ImportStateBIReporter.js +279 -0
  43. package/dist/cjs/state/ImportState/ImportStateBIReporter.js.map +1 -0
  44. package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js +518 -14
  45. package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
  46. package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js +30 -4
  47. package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
  48. package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js +91 -0
  49. package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
  50. package/dist/cjs/state/ImportState/detectFieldType.js +3 -3
  51. package/dist/cjs/state/ImportState/detectFieldType.js.map +1 -1
  52. package/dist/cjs/state/ImportState/importFieldCompatibility.js +92 -0
  53. package/dist/cjs/state/ImportState/importFieldCompatibility.js.map +1 -0
  54. package/dist/cjs/state/ImportState/importSampling.js +6 -0
  55. package/dist/cjs/state/ImportState/importSampling.js.map +1 -0
  56. package/dist/cjs/state/ImportState/index.js.map +1 -1
  57. package/dist/cjs/types/Field.js.map +1 -1
  58. package/dist/esm/assets/locale/messages_ar.json +20 -20
  59. package/dist/esm/assets/locale/messages_bg.json +20 -20
  60. package/dist/esm/assets/locale/messages_ca.json +20 -20
  61. package/dist/esm/assets/locale/messages_cs.json +20 -20
  62. package/dist/esm/assets/locale/messages_da.json +20 -20
  63. package/dist/esm/assets/locale/messages_de.json +20 -20
  64. package/dist/esm/assets/locale/messages_el.json +20 -20
  65. package/dist/esm/assets/locale/messages_en.json +20 -20
  66. package/dist/esm/assets/locale/messages_es.json +20 -20
  67. package/dist/esm/assets/locale/messages_fi.json +20 -20
  68. package/dist/esm/assets/locale/messages_fr.json +20 -20
  69. package/dist/esm/assets/locale/messages_he.json +20 -20
  70. package/dist/esm/assets/locale/messages_hi.json +20 -20
  71. package/dist/esm/assets/locale/messages_hu.json +20 -20
  72. package/dist/esm/assets/locale/messages_id.json +20 -20
  73. package/dist/esm/assets/locale/messages_it.json +20 -20
  74. package/dist/esm/assets/locale/messages_ja.json +20 -20
  75. package/dist/esm/assets/locale/messages_ko.json +20 -20
  76. package/dist/esm/assets/locale/messages_lt.json +20 -20
  77. package/dist/esm/assets/locale/messages_ms.json +20 -20
  78. package/dist/esm/assets/locale/messages_nl.json +20 -20
  79. package/dist/esm/assets/locale/messages_no.json +20 -20
  80. package/dist/esm/assets/locale/messages_pl.json +20 -20
  81. package/dist/esm/assets/locale/messages_pt.json +20 -20
  82. package/dist/esm/assets/locale/messages_ro.json +20 -20
  83. package/dist/esm/assets/locale/messages_ru.json +20 -20
  84. package/dist/esm/assets/locale/messages_sk.json +20 -20
  85. package/dist/esm/assets/locale/messages_sl.json +20 -20
  86. package/dist/esm/assets/locale/messages_sv.json +20 -20
  87. package/dist/esm/assets/locale/messages_th.json +20 -20
  88. package/dist/esm/assets/locale/messages_tl.json +20 -20
  89. package/dist/esm/assets/locale/messages_tr.json +20 -20
  90. package/dist/esm/assets/locale/messages_uk.json +20 -20
  91. package/dist/esm/assets/locale/messages_vi.json +20 -20
  92. package/dist/esm/assets/locale/messages_zh.json +20 -20
  93. package/dist/esm/assets/locale/version_file +1 -1
  94. package/dist/esm/assets/visual-e2e-locale/messages_visuale2e.json +19 -19
  95. package/dist/esm/hooks/useImport.js +5 -2
  96. package/dist/esm/hooks/useImport.js.map +1 -1
  97. package/dist/esm/state/ImportState/ImportState.js +181 -36
  98. package/dist/esm/state/ImportState/ImportState.js.map +1 -1
  99. package/dist/esm/state/ImportState/ImportStateBIReporter.js +232 -0
  100. package/dist/esm/state/ImportState/ImportStateBIReporter.js.map +1 -0
  101. package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js +385 -9
  102. package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
  103. package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js +32 -4
  104. package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
  105. package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js +99 -0
  106. package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
  107. package/dist/esm/state/ImportState/detectFieldType.js +3 -3
  108. package/dist/esm/state/ImportState/detectFieldType.js.map +1 -1
  109. package/dist/esm/state/ImportState/importFieldCompatibility.js +102 -0
  110. package/dist/esm/state/ImportState/importFieldCompatibility.js.map +1 -0
  111. package/dist/esm/state/ImportState/importSampling.js +2 -0
  112. package/dist/esm/state/ImportState/importSampling.js.map +1 -0
  113. package/dist/esm/state/ImportState/index.js.map +1 -1
  114. package/dist/types/hooks/useImport.d.ts +5 -1
  115. package/dist/types/hooks/useImport.d.ts.map +1 -1
  116. package/dist/types/state/ImportState/ImportState.d.ts +23 -4
  117. package/dist/types/state/ImportState/ImportState.d.ts.map +1 -1
  118. package/dist/types/state/ImportState/ImportStateBIReporter.d.ts +66 -0
  119. package/dist/types/state/ImportState/ImportStateBIReporter.d.ts.map +1 -0
  120. package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts +2 -0
  121. package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts.map +1 -0
  122. package/dist/types/state/ImportState/detectFieldType.d.ts.map +1 -1
  123. package/dist/types/state/ImportState/importFieldCompatibility.d.ts +4 -0
  124. package/dist/types/state/ImportState/importFieldCompatibility.d.ts.map +1 -0
  125. package/dist/types/state/ImportState/importSampling.d.ts +2 -0
  126. package/dist/types/state/ImportState/importSampling.d.ts.map +1 -0
  127. package/dist/types/state/ImportState/index.d.ts +1 -0
  128. package/dist/types/state/ImportState/index.d.ts.map +1 -1
  129. package/dist/types/types/Field.d.ts +4 -0
  130. package/dist/types/types/Field.d.ts.map +1 -1
  131. package/package.json +8 -5
@@ -14,8 +14,10 @@ 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");
20
+ var _ImportStateBIReporter = require("./ImportStateBIReporter");
19
21
  /** Outcome of a server-side import job, derived from the job's progress. */
20
22
 
21
23
  /** A rejected row: its index into `csvRows` and the server's failure reason. */
@@ -49,7 +51,8 @@ var _newFieldDraft = require("./newFieldDraft");
49
51
  // `id`/`header`): a field the destination mandates. Consumers set it — the
50
52
  // schema-backed config forwards `validation.required`, manual/platformized
51
53
  // configs set it per field — and the mapping step blocks import while any
52
- // 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).
53
56
 
54
57
  // `NewFieldType`, `detectColumnType`, etc. are re-exported from the package via
55
58
  // the `ImportState` barrel (`./index`), which also exports `./detectFieldType`.
@@ -210,7 +213,11 @@ const buildImportConfig = (fieldsSource, collection, inputs = {}) => {
210
213
  return {
211
214
  id: f.id,
212
215
  header: f.displayName,
213
- 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
+ })
214
221
  };
215
222
  });
216
223
  return [...fields, ...live];
@@ -257,6 +264,11 @@ exports.buildImportConfig = buildImportConfig;
257
264
  // so the bar sits at 0 for the whole import and would otherwise flip straight to
258
265
  // the summary without the user ever seeing it reach completion.
259
266
  const COMPLETION_LINGER_MS = 600;
267
+ const FILE_ERROR_MESSAGE_KEY = {
268
+ encoding_failed: 'cairo.import.encodingFailed',
269
+ process_failed: 'cairo.import.processFailed'
270
+ };
271
+ const NOOP_REPORT_BI = () => {};
260
272
 
261
273
  // The Wix Data collection's id field key. A CSV column mapped to this field lets
262
274
  // the user choose how rows that collide on `_id` are written (writePolicy).
@@ -296,6 +308,21 @@ function uniqueFieldNameAndKey(header, usedNames, usedKeys, requestedKey) {
296
308
  displayName = `${header}-${suffix}`;
297
309
  }
298
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
+ }
299
326
  class ImportState {
300
327
  constructor(props) {
301
328
  // The import inputs, held by reference so a live schema-backed config's getters
@@ -307,8 +334,11 @@ class ImportState {
307
334
  (0, _defineProperty2.default)(this, "errorHandler", void 0);
308
335
  (0, _defineProperty2.default)(this, "showToast", void 0);
309
336
  (0, _defineProperty2.default)(this, "downloadCsv", void 0);
337
+ (0, _defineProperty2.default)(this, "reportBi", void 0);
338
+ (0, _defineProperty2.default)(this, "bi", void 0);
310
339
  (0, _defineProperty2.default)(this, "hubJobClient", void 0);
311
340
  (0, _defineProperty2.default)(this, "_detectColumnType", void 0);
341
+ (0, _defineProperty2.default)(this, "_isValueCompatible", void 0);
312
342
  (0, _defineProperty2.default)(this, "discardChangesModal", new _ConditionalModalState.ConditionalModalState());
313
343
  (0, _defineProperty2.default)(this, "currentStep", 'upload');
314
344
  (0, _defineProperty2.default)(this, "importStatus", {
@@ -344,6 +374,8 @@ class ImportState {
344
374
  });
345
375
  (0, _defineProperty2.default)(this, "isBackingUp", false);
346
376
  (0, _defineProperty2.default)(this, "isRestoring", false);
377
+ (0, _defineProperty2.default)(this, "attempt", 0);
378
+ (0, _defineProperty2.default)(this, "importStartTime", 0);
347
379
  (0, _defineProperty2.default)(this, "restoreFn", null);
348
380
  (0, _defineProperty2.default)(this, "_cancelled", false);
349
381
  (0, _defineProperty2.default)(this, "_abortController", null);
@@ -369,7 +401,10 @@ class ImportState {
369
401
  this.errorHandler = props.errorHandler;
370
402
  this.showToast = props.showToast;
371
403
  this.downloadCsv = props.downloadCsv;
404
+ this.reportBi = props.reportBi ?? NOOP_REPORT_BI;
405
+ this.bi = new _ImportStateBIReporter.ImportStateBIReporter(this);
372
406
  this._detectColumnType = props.detectColumnType ?? _detectFieldType.detectColumnType;
407
+ this._isValueCompatible = props.isValueCompatible;
373
408
  this.hubJobClient = (0, _hubJobClient.createHubJobClient)({
374
409
  httpClient: props.httpClient,
375
410
  errorHandler: props.errorHandler
@@ -380,6 +415,13 @@ class ImportState {
380
415
  fileName: _mobx.observable.ref,
381
416
  csvHeaders: _mobx.observable.ref,
382
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
+ }),
383
425
  orderedHeaders: _mobx.observable.ref,
384
426
  mappingByHeader: _mobx.observable.ref,
385
427
  writePolicy: _mobx.observable.ref,
@@ -395,6 +437,7 @@ class ImportState {
395
437
  missingRequiredFields: _mobx.computed,
396
438
  hasMissingRequiredFields: _mobx.computed,
397
439
  idMappedHeader: _mobx.computed,
440
+ hasDuplicateIdsInFile: _mobx.computed,
398
441
  editingHeader: _mobx.observable.ref,
399
442
  editingMapping: _mobx.computed,
400
443
  endFieldEdit: _mobx.action.bound,
@@ -413,6 +456,7 @@ class ImportState {
413
456
  downloadFailedRows: _mobx.action.bound,
414
457
  onFileSelected: _mobx.action.bound,
415
458
  startImport: _mobx.action.bound,
459
+ retryImport: _mobx.action.bound,
416
460
  reset: _mobx.action.bound
417
461
  });
418
462
  }
@@ -473,7 +517,16 @@ class ImportState {
473
517
  return this.config.idFieldKey ?? ID_FIELD_KEY;
474
518
  }
475
519
  init() {
520
+ // This state mounts after `open` fired and unmounts on close, so each mount
521
+ // is one BI flow.
522
+ if (this.importModalState.isOpen) {
523
+ this.bi.onModalOpened();
524
+ }
525
+ const disposers = [(0, _mobx.reaction)(() => this.currentStep === 'configuration' && this.hasNoMappedData, shown => shown && this.bi.onMappingWarningShown('no_rows')), (0, _mobx.reaction)(() => this.currentStep === 'configuration' && this.hasMissingRequiredFields, shown => shown && this.bi.onMappingWarningShown('required_field_unmapped')), (0, _mobx.reaction)(() => this.currentStep === 'configuration' && this.canConfigureWritePolicy && this.idMappedHeader != null, shown => shown && this.bi.onDuplicateIdPolicyShown(), {
526
+ fireImmediately: true
527
+ })];
476
528
  return () => {
529
+ disposers.forEach(dispose => dispose());
477
530
  // Unmounting mid-import stops the poll loop (it checks `_cancelled`) so it
478
531
  // doesn't run to terminal holding references. We deliberately do NOT abort
479
532
  // the AbortController: during a cancel-triggered restore it holds the
@@ -512,6 +565,15 @@ class ImportState {
512
565
  get selectedFieldIds() {
513
566
  return new Set(Object.values(this.mappingByHeader).filter(m => (m == null ? void 0 : m.kind) === 'existing').map(m => m.fieldId));
514
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
+ }
515
577
 
516
578
  /**
517
579
  * Required destination fields that no CSV column is mapped to (CAIRO-4452).
@@ -545,8 +607,26 @@ class ImportState {
545
607
  const entry = Object.entries(this.mappingByHeader).find(([, mapping]) => (mapping == null ? void 0 : mapping.kind) === 'existing' && mapping.fieldId === idFieldKey);
546
608
  return entry ? entry[0] : null;
547
609
  }
610
+ get hasDuplicateIdsInFile() {
611
+ const header = this.idMappedHeader;
612
+ const index = header == null ? -1 : this.csvHeaders.indexOf(header);
613
+ if (index < 0) {
614
+ return false;
615
+ }
616
+ const seen = new Set();
617
+ return this.csvRows.some(row => {
618
+ const value = (row[index] ?? '').trim();
619
+ if (value === '' || !seen.has(value)) {
620
+ seen.add(value);
621
+ return false;
622
+ }
623
+ return true;
624
+ });
625
+ }
548
626
  setWritePolicy(policy) {
627
+ const prev = this.writePolicy;
549
628
  this.writePolicy = policy;
629
+ this.bi.onDuplicateIdPolicyChanged(prev, policy);
550
630
  }
551
631
 
552
632
  /**
@@ -601,14 +681,34 @@ class ImportState {
601
681
  this.restoreFn = null;
602
682
  this._cancelled = false;
603
683
  this._jobId = null;
684
+ this.attempt = 0;
604
685
  }
605
- async close() {
686
+
687
+ // The configuration step closes through `discardAndClose`, which reports it.
688
+ async close(origin = 'X Button') {
689
+ const {
690
+ bi,
691
+ currentStep,
692
+ importStatus
693
+ } = this;
694
+ const isCancellingImport = currentStep === 'progress' && !importStatus.isSuccess;
695
+ if (currentStep === 'upload') {
696
+ bi.onModalDismissed(origin, 'upload');
697
+ } else if (currentStep === 'summary') {
698
+ bi.onSummaryCtaClicked(origin === 'Cancel Button' ? 'Close' : 'X Button');
699
+ } else if (isCancellingImport && importStatus.isError) {
700
+ bi.onFailedModalCtaClicked(origin === 'Cancel Button' ? 'Cancel' : 'X Button');
701
+ } else if (isCancellingImport) {
702
+ bi.onCancelAttempt(origin);
703
+ bi.onProcessEnd('stopped by user');
704
+ }
705
+
606
706
  // While on the progress step we treat close as a cancel — terminate the job
607
707
  // and restore the backup — UNLESS the import already succeeded. On success
608
708
  // `startImport` moves straight to the 'summary' step, so a close from there
609
709
  // just closes the modal (the summary's own `done` is the normal exit); this
610
710
  // guard covers only closing mid-import.
611
- if (this.currentStep === 'progress' && !this.importStatus.isSuccess) {
711
+ if (isCancellingImport) {
612
712
  var _this$_abortControlle2;
613
713
  this._cancelled = true;
614
714
  (_this$_abortControlle2 = this._abortController) == null || _this$_abortControlle2.abort();
@@ -636,7 +736,8 @@ class ImportState {
636
736
  * Closes the summary step at the end of a completed import. The collection was
637
737
  * already refreshed when the summary opened, so this only dismisses the modal.
638
738
  */
639
- done() {
739
+ done(origin = 'Close') {
740
+ this.bi.onSummaryCtaClicked(origin);
640
741
  this.importModalState.close();
641
742
  }
642
743
  async _restoreBackup() {
@@ -665,17 +766,20 @@ class ImportState {
665
766
  const {
666
767
  result
667
768
  } = await this.discardChangesModal.modal.promise();
769
+ this.bi.onDiscardModalClosed(result);
668
770
  if (result === 'confirmed') {
669
771
  this.goToUpload();
670
772
  }
671
773
  }
672
- async discardAndClose() {
774
+ async discardAndClose(origin = 'X Button') {
775
+ this.bi.onModalDismissed(origin, 'configuration');
673
776
  this.discardChangesModal.open('close');
674
777
  const {
675
778
  result
676
779
  } = await this.discardChangesModal.modal.promise();
780
+ this.bi.onDiscardModalClosed(result);
677
781
  if (result === 'confirmed') {
678
- this.close();
782
+ this.close(origin);
679
783
  }
680
784
  }
681
785
  onUploadContinue() {
@@ -683,20 +787,19 @@ class ImportState {
683
787
  this.currentStep = 'configuration';
684
788
  }
685
789
  }
686
- getDefaultMapping(headers) {
790
+ getDefaultMapping(headers, originalHeaders) {
687
791
  const mapping = {};
688
- const fields = this.fields.map(field => ({
689
- id: field.id ?? '',
690
- header: field.header ?? ''
691
- }));
792
+ const matchedIds = new Set();
793
+ const fields = this.fields.filter(field => field.type !== 'UNKNOWN');
692
794
  headers.forEach((header, index) => {
693
- const normalizedHeader = normalize(header);
694
- 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));
695
797
  if (matched != null && matched.id) {
696
798
  mapping[header] = {
697
799
  kind: 'existing',
698
800
  fieldId: matched.id
699
801
  };
802
+ matchedIds.add(matched.id);
700
803
  } else {
701
804
  // Unmatched columns default to creating a new field (type inferred from
702
805
  // the column's data) when the consumer supplied `updateSchema`;
@@ -709,12 +812,23 @@ class ImportState {
709
812
  });
710
813
  return mapping;
711
814
  }
712
-
713
- // The column's values, capped at the detection sample size — detection only
714
- // looks at the first `TYPE_SAMPLE_SIZE` values, so avoid copying the whole
715
- // 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
+ }
716
830
  _columnValues(columnIndex) {
717
- return this.csvRows.slice(0, _detectFieldType.TYPE_SAMPLE_SIZE).map(row => row[columnIndex] ?? '');
831
+ return this._columnSamples[columnIndex] ?? [];
718
832
  }
719
833
 
720
834
  // Puts the `_id`-mapped column first (CAIRO-4418), preserving CSV order for the
@@ -727,13 +841,14 @@ class ImportState {
727
841
  this._pendingFieldsMap = undefined;
728
842
  this._createdFields.clear();
729
843
  this.fileName = fileName;
730
- this.csvHeaders = parsedCsv.headers;
844
+ // Mapping, compatibility and upload rewriting all key columns by this name.
845
+ this.csvHeaders = uniqueColumnHeaders(parsedCsv.headers);
731
846
  this.csvRows = parsedCsv.rows;
732
- this.mappingByHeader = this.getDefaultMapping(parsedCsv.headers);
847
+ this.mappingByHeader = this.getDefaultMapping(this.csvHeaders, parsedCsv.headers);
733
848
  // Snapshot the row order for this file with the auto-mapped `_id` column
734
849
  // first. Reads `idMappedHeader`, so it must run after `mappingByHeader` is
735
850
  // set. Kept stable until the next file so mapping edits don't reorder rows.
736
- this.orderedHeaders = this._orderHeadersWithIdFirst(parsedCsv.headers);
851
+ this.orderedHeaders = this._orderHeadersWithIdFirst(this.csvHeaders);
737
852
  // A new file is a fresh import: start from the default policy so a prior
738
853
  // file's Skip choice doesn't silently carry over (only the new-file path
739
854
  // resets — remapping `_id` within the same file keeps the user's choice).
@@ -751,16 +866,18 @@ class ImportState {
751
866
  }
752
867
  const content = this._decodeUtf8(bytes);
753
868
  if (content == null) {
754
- this._rejectFile(file, 'cairo.import.encodingFailed');
869
+ this._rejectFile(file, 'encoding_failed');
755
870
  return;
756
871
  }
757
872
  const parsedCsv = (0, _parseCsv.parseCsv)(content);
758
873
  if ('code' in parsedCsv) {
759
- this._rejectFile(file, 'cairo.import.processFailed');
874
+ this._rejectFile(file, 'process_failed');
760
875
  return;
761
876
  }
762
877
  (0, _mobx.runInAction)(() => {
763
878
  this.applyParsedCsv(file.name, parsedCsv);
879
+ // Inside the action, so 144:28 goes out before the reactions it triggers.
880
+ this.bi.onFileSelected();
764
881
  });
765
882
  }
766
883
  async _readFileBytes(file) {
@@ -784,7 +901,7 @@ class ImportState {
784
901
  return null;
785
902
  }
786
903
  }
787
- _rejectFile(file, messageKey) {
904
+ _rejectFile(file, errorType) {
788
905
  var _this$showToast;
789
906
  (0, _mobx.runInAction)(() => {
790
907
  this.fileName = file.name;
@@ -796,11 +913,13 @@ class ImportState {
796
913
  });
797
914
  (_this$showToast = this.showToast) == null || _this$showToast.call(this, {
798
915
  type: 'ERROR',
799
- message: this.translate(messageKey)
916
+ message: this.translate(FILE_ERROR_MESSAGE_KEY[errorType])
800
917
  });
918
+ this.bi.onFileErrorDisplayed(errorType, file.name);
801
919
  }
802
920
  _showFileReadErrorToast(file) {
803
921
  var _this$showToast2;
922
+ this.bi.onFileErrorDisplayed('file_read_failed', file.name);
804
923
  (_this$showToast2 = this.showToast) == null || _this$showToast2.call(this, {
805
924
  type: 'ERROR',
806
925
  message: this.translate('cairo.import.fileReadFailed'),
@@ -821,6 +940,7 @@ class ImportState {
821
940
  */
822
941
  async downloadFailedRows() {
823
942
  var _this$downloadCsv;
943
+ this.bi.onSummaryCtaClicked('Download failed items CSV');
824
944
  const jobId = this._jobId;
825
945
  if (!jobId) {
826
946
  return;
@@ -887,24 +1007,30 @@ class ImportState {
887
1007
  return failures;
888
1008
  }
889
1009
  setMapping(header, fieldId) {
1010
+ const prev = this.mappingByHeader[header] ?? null;
1011
+ const next = fieldId == null ? null : {
1012
+ kind: 'existing',
1013
+ fieldId
1014
+ };
890
1015
  this.mappingByHeader = {
891
1016
  ...this.mappingByHeader,
892
- [header]: fieldId == null ? null : {
893
- kind: 'existing',
894
- fieldId
895
- }
1017
+ [header]: next
896
1018
  };
1019
+ this.bi.onMappingChanged(header, prev, next);
897
1020
  }
898
1021
  setNewFieldMapping(header) {
899
1022
  const index = this.csvHeaders.indexOf(header);
900
1023
  const fieldType = index >= 0 ? this._detectColumnType(this._columnValues(index)) : 'text';
1024
+ const prev = this.mappingByHeader[header] ?? null;
1025
+ const next = {
1026
+ kind: 'new',
1027
+ fieldType
1028
+ };
901
1029
  this.mappingByHeader = {
902
1030
  ...this.mappingByHeader,
903
- [header]: {
904
- kind: 'new',
905
- fieldType
906
- }
1031
+ [header]: next
907
1032
  };
1033
+ this.bi.onMappingChanged(header, prev, next);
908
1034
  }
909
1035
 
910
1036
  /**
@@ -1154,6 +1280,7 @@ class ImportState {
1154
1280
  error
1155
1281
  };
1156
1282
  });
1283
+ this.bi.onProcessEnd('fail');
1157
1284
  return true;
1158
1285
  }
1159
1286
  (0, _mobx.runInAction)(() => {
@@ -1312,6 +1439,13 @@ class ImportState {
1312
1439
  (_this$config$onItemsL = (_this$config2 = this.config).onItemsLimitExceeded) == null || _this$config$onItemsL.call(_this$config2);
1313
1440
  return;
1314
1441
  }
1442
+
1443
+ // Retries come through `retryImport`, which reports its own CTA.
1444
+ this.attempt += 1;
1445
+ this.importStartTime = performance.now();
1446
+ if (this.attempt === 1) {
1447
+ this.bi.onImportConfirmed();
1448
+ }
1315
1449
  this._cancelled = false;
1316
1450
  this._abortController = new AbortController();
1317
1451
 
@@ -1336,6 +1470,9 @@ class ImportState {
1336
1470
  };
1337
1471
  this._jobId = null;
1338
1472
  });
1473
+ if (this.backup) {
1474
+ this.bi.onProcessStart('backup');
1475
+ }
1339
1476
  const backupFailed = await this._runBackup();
1340
1477
  if (backupFailed) {
1341
1478
  return;
@@ -1344,6 +1481,9 @@ class ImportState {
1344
1481
  return;
1345
1482
  }
1346
1483
 
1484
+ // Before field creation, so a creation failure's process end has a start.
1485
+ this.bi.onProcessStart('import');
1486
+
1347
1487
  // Create any new fields and resolve every column to its destination key
1348
1488
  // before uploading. A creation failure surfaces as the error state with a
1349
1489
  // "Try again" CTA, like an upload/job failure.
@@ -1357,6 +1497,10 @@ class ImportState {
1357
1497
  error
1358
1498
  };
1359
1499
  });
1500
+ // A cancelled import already reported its end as "stopped by user".
1501
+ if (!this._cancelled) {
1502
+ this.bi.onProcessEnd('fail');
1503
+ }
1360
1504
  return;
1361
1505
  }
1362
1506
  if (this._cancelled) {
@@ -1390,6 +1534,7 @@ class ImportState {
1390
1534
  this.processedCount = this.totalCount;
1391
1535
  });
1392
1536
  this._notifyImportCompleted();
1537
+ this.bi.onProcessEnd(this.result.failed > 0 ? 'partial' : 'success');
1393
1538
 
1394
1539
  // Keep the completed bar on screen briefly before moving on. `importStatus`
1395
1540
  // is already success, so a close during this window just dismisses the
@@ -1412,6 +1557,7 @@ class ImportState {
1412
1557
  force: true
1413
1558
  });
1414
1559
  });
1560
+ this.bi.onSummaryDisplayed();
1415
1561
  } catch (error) {
1416
1562
  (0, _mobx.runInAction)(() => {
1417
1563
  this.importStatus = {
@@ -1419,6 +1565,9 @@ class ImportState {
1419
1565
  error
1420
1566
  };
1421
1567
  });
1568
+ if (!this._cancelled) {
1569
+ this.bi.onProcessEnd('fail');
1570
+ }
1422
1571
  }
1423
1572
 
1424
1573
  // Merge the created fields into the table so the new columns appear with no
@@ -1436,6 +1585,10 @@ class ImportState {
1436
1585
  }
1437
1586
  }
1438
1587
  }
1588
+ retryImport() {
1589
+ this.bi.onFailedModalCtaClicked('Try Again');
1590
+ return this.startImport();
1591
+ }
1439
1592
  }
1440
1593
  exports.ImportState = ImportState;
1441
1594
  //# sourceMappingURL=ImportState.js.map