@firecms/data_import_export 3.0.0-canary.2 → 3.0.0-canary.200

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 (45) hide show
  1. package/LICENSE +114 -21
  2. package/README.md +1 -4
  3. package/dist/index.d.ts +2 -4
  4. package/dist/index.es.js +15 -971
  5. package/dist/index.es.js.map +1 -1
  6. package/dist/index.umd.js +29 -2
  7. package/dist/index.umd.js.map +1 -1
  8. package/dist/useImportExportPlugin.d.ts +7 -4
  9. package/package.json +18 -35
  10. package/src/index.ts +2 -4
  11. package/src/useImportExportPlugin.tsx +13 -8
  12. package/dist/components/DataNewPropertiesMapping.d.ts +0 -15
  13. package/dist/components/ImportFileUpload.d.ts +0 -3
  14. package/dist/components/ImportNewPropertyFieldPreview.d.ts +0 -10
  15. package/dist/components/ImportSaveInProgress.d.ts +0 -7
  16. package/dist/components/index.d.ts +0 -4
  17. package/dist/export_import/ExportCollectionAction.d.ts +0 -10
  18. package/dist/export_import/ImportCollectionAction.d.ts +0 -13
  19. package/dist/export_import/export.d.ts +0 -10
  20. package/dist/hooks/index.d.ts +0 -1
  21. package/dist/hooks/useImportConfig.d.ts +0 -2
  22. package/dist/types/column_mapping.d.ts +0 -22
  23. package/dist/types/index.d.ts +0 -1
  24. package/dist/utils/data.d.ts +0 -11
  25. package/dist/utils/file_to_json.d.ts +0 -11
  26. package/dist/utils/get_import_inference_type.d.ts +0 -2
  27. package/dist/utils/get_properties_mapping.d.ts +0 -3
  28. package/dist/utils/index.d.ts +0 -4
  29. package/src/components/DataNewPropertiesMapping.tsx +0 -127
  30. package/src/components/ImportFileUpload.tsx +0 -34
  31. package/src/components/ImportNewPropertyFieldPreview.tsx +0 -55
  32. package/src/components/ImportSaveInProgress.tsx +0 -95
  33. package/src/components/index.ts +0 -4
  34. package/src/export_import/ExportCollectionAction.tsx +0 -251
  35. package/src/export_import/ImportCollectionAction.tsx +0 -420
  36. package/src/export_import/export.ts +0 -200
  37. package/src/hooks/index.ts +0 -1
  38. package/src/hooks/useImportConfig.tsx +0 -28
  39. package/src/types/column_mapping.ts +0 -32
  40. package/src/types/index.ts +0 -1
  41. package/src/utils/data.ts +0 -210
  42. package/src/utils/file_to_json.ts +0 -88
  43. package/src/utils/get_import_inference_type.ts +0 -27
  44. package/src/utils/get_properties_mapping.ts +0 -59
  45. package/src/utils/index.ts +0 -4
package/dist/index.es.js CHANGED
@@ -1,975 +1,19 @@
1
- import C, { useState as j, useRef as Ie, useEffect as ce, useCallback as x, useMemo as Ae } from "react";
2
- import { jsxs as p, Fragment as $, jsx as s } from "react/jsx-runtime";
3
- import { getPropertyInPath as L, EntityReference as de, useSnackbarController as pe, useCustomizationController as z, getFieldConfig as fe, ErrorBoundary as Ee, PropertyConfigBadge as me, useDataSource as ue, resolveCollection as he, getPropertiesWithPropertiesOrder as Te, useSelectionController as je, EntityCollectionTable as ke, getArrayValuesCount as Fe, getValueInPath as Pe, useFireCMSContext as Me, useNavigationController as $e } from "@firecms/core";
4
- import { Table as Le, TableHeader as _e, TableCell as M, TableBody as Be, TableRow as Je, Typography as u, ChevronRightIcon as Re, Select as ge, SelectItem as J, FileUpload as He, UploadIcon as Ue, TextField as Ve, IconButton as ee, EditIcon as We, CenteredView as ze, CircularProgress as ye, Tooltip as te, FileUploadIcon as Ge, Dialog as be, DialogContent as xe, DialogActions as ve, Button as k, cn as B, defaultBorderMixin as Xe, GetAppIcon as qe, Alert as Qe, focusedMixin as U, BooleanSwitchWithLabel as Ye } from "@firecms/ui";
5
- import { buildEntityPropertiesFromData as Ze } from "@firecms/schema_inference";
6
- import * as ie from "xlsx";
7
- const Ke = () => {
8
- const [e, r] = j(!1), [n, t] = j(), [a, i] = j([]), [o, l] = j([]), [d, c] = j({}), [f, m] = j({});
9
- return {
10
- inUse: e,
11
- setInUse: r,
12
- idColumn: n,
13
- setIdColumn: t,
14
- entities: o,
15
- setEntities: l,
16
- importData: a,
17
- setImportData: i,
18
- headersMapping: d,
19
- setHeadersMapping: c,
20
- originProperties: f,
21
- setOriginProperties: m
22
- };
23
- };
24
- function et(e) {
25
- return new Promise((r, n) => {
26
- if (e.type === "application/json") {
27
- console.debug("Converting JSON file to JSON", e.name);
28
- const t = new FileReader();
29
- t.onload = function(a) {
30
- const i = a.target?.result, o = JSON.parse(i);
31
- Array.isArray(o) || n(new Error("JSON file should contain an array of objects")), r(o);
32
- }, t.readAsText(e);
33
- } else {
34
- console.debug("Converting Excel file to JSON", e.name);
35
- const t = new FileReader();
36
- t.onload = function(a) {
37
- const i = new Uint8Array(a.target?.result), o = ie.read(
38
- i,
39
- {
40
- type: "array",
41
- codepage: 65001,
42
- cellDates: !0
43
- }
44
- ), l = o.SheetNames[0], d = o.Sheets[l], m = ie.utils.sheet_to_json(d).map(tt).map(Ce);
45
- r(m);
46
- }, t.readAsArrayBuffer(e);
47
- }
48
- });
49
- }
50
- function tt(e) {
51
- return Object.keys(e).reduce((r, n) => {
52
- try {
53
- r[n] = JSON.parse(e[n]);
54
- } catch {
55
- r[n] = e[n];
56
- }
57
- return r;
58
- }, {});
59
- }
60
- function Ce(e) {
61
- return Object.keys(e).reduce((r, n) => {
62
- let t = r;
63
- const a = n.split(".");
64
- return a.forEach((i, o) => {
65
- if (/^[\w]+\[\d+\]$/.test(i)) {
66
- const l = i.slice(0, i.indexOf("[")), d = parseInt(i.slice(i.indexOf("[") + 1, i.indexOf("]")));
67
- t[l] || (t[l] = []), o !== a.length - 1 ? (t[l][d] = t[l][d] || {}, t = t[l][d]) : t[l][d] = e[n];
68
- } else
69
- o !== a.length - 1 ? (t[i] = t[i] || {}, t = t[i]) : t[i] = e[n];
70
- }), r;
71
- }, {});
72
- }
73
- function rt(e, r, n, t, a, i) {
74
- const o = we(e);
75
- r && delete o[r];
76
- const l = Object.entries(o).map(([c, f]) => {
77
- const m = n[c] ?? c;
78
- if (!L(t, m))
79
- return {};
80
- const h = a[m];
81
- let S = f;
82
- return h && (S = Z(f, h)), {
83
- [m]: S
84
- };
85
- }).reduce((c, f) => ({ ...c, ...f }), {}), d = Ce(l);
86
- return {
87
- id: r ? e[r] : void 0,
88
- values: d,
89
- path: i
90
- };
91
- }
92
- function we(e, r = "") {
93
- return Object.keys(e).reduce((n, t) => {
94
- const a = r ? `${r}.${t}` : t;
95
- return typeof e[t] == "object" && e[t] !== null && !Array.isArray(e[t]) ? Object.assign(n, we(e[t], a)) : n[a] = e[t], n;
96
- }, {});
97
- }
98
- function Z(e, r) {
99
- if (r === void 0)
100
- return e;
101
- const {
102
- from: n,
103
- to: t
104
- } = r;
105
- if (n === "array" && t === "array" && r.fromSubtype && r.toSubtype && Array.isArray(e))
106
- return e.map((a) => Z(a, {
107
- from: r.fromSubtype,
108
- to: r.toSubtype
109
- }));
110
- if (n === "string" && t === "number" && typeof e == "string")
111
- return Number(e);
112
- if (n === "string" && t === "array" && r.toSubtype && typeof e == "string")
113
- return e.split(",").map((a) => Z(a, {
114
- from: "string",
115
- to: r.toSubtype
116
- }));
117
- if (n === "string" && t === "boolean")
118
- return e === "true";
119
- if (n === "number" && t === "boolean")
120
- return e === 1;
121
- if (n === "boolean" && t === "number")
122
- return e ? 1 : 0;
123
- if (n === "boolean" && t === "string")
124
- return e ? "true" : "false";
125
- if (n === "number" && t === "string" && typeof e == "number")
126
- return e.toString();
127
- if (n === "string" && t === "array" && typeof e == "string")
128
- return e.split(",").map((a) => a.trim());
129
- if (n === "string" && t === "date" && typeof e == "string")
130
- try {
131
- return new Date(e);
132
- } catch {
133
- return e;
134
- }
135
- else {
136
- if (n === "date" && t === "string")
137
- return e instanceof Date && e.toISOString();
138
- if (n === "number" && t === "date" && typeof e == "number")
139
- try {
140
- return new Date(e);
141
- } catch {
142
- return e;
143
- }
144
- else if (n === "string" && t === "reference" && typeof e == "string") {
145
- const a = e.split("/").slice(0, -1).join("/"), i = e.split("/").slice(-1)[0];
146
- return new de(i, a);
147
- } else {
148
- if (n === t)
149
- return e;
150
- if (n === "array" && t === "string" && Array.isArray(e))
151
- return e.join(",");
152
- }
153
- }
154
- return e;
155
- }
156
- function nt(e) {
157
- return typeof e == "number" ? "number" : typeof e == "string" ? "string" : typeof e == "boolean" ? "boolean" : e instanceof Date ? "date" : Array.isArray(e) ? "array" : "map";
158
- }
159
- function at(e, r) {
160
- function n(t, a) {
161
- const i = {};
162
- return Object.keys(t).forEach((o) => {
163
- const l = a ? `${a}.${o}` : o, d = L(t, o), c = L(e, l);
164
- if (d) {
165
- if (d.dataType === "map" && d.properties) {
166
- const f = n(d.properties, l);
167
- Object.keys(f).forEach((m) => {
168
- i[`${l}.${m}`] = f[m];
169
- });
170
- return;
171
- }
172
- if (c) {
173
- const f = c.dataType, m = d.dataType;
174
- let y, h;
175
- d.dataType === "array" && d.of && (h = d.of.dataType), c?.dataType === "array" && c?.of && (y = c.of.dataType), (f !== m || y !== h) && (i[o] = {
176
- from: f,
177
- to: m,
178
- fromSubtype: y,
179
- toSubtype: h
180
- });
181
- }
182
- }
183
- }), i;
184
- }
185
- return n(r);
186
- }
187
- function it({
188
- idColumn: e,
189
- headersMapping: r,
190
- originProperties: n,
191
- destinationProperties: t,
192
- onIdPropertyChanged: a,
193
- buildPropertyView: i
194
- }) {
195
- return /* @__PURE__ */ p($, { children: [
196
- /* @__PURE__ */ s(
197
- st,
198
- {
199
- idColumn: e,
200
- headersMapping: r,
201
- onChange: a
202
- }
203
- ),
204
- /* @__PURE__ */ p(Le, { style: {
205
- tableLayout: "fixed"
206
- }, children: [
207
- /* @__PURE__ */ p(_e, { children: [
208
- /* @__PURE__ */ s(M, { header: !0, style: { width: "20%" }, children: "Column in file" }),
209
- /* @__PURE__ */ s(M, { header: !0 }),
210
- /* @__PURE__ */ s(M, { header: !0, style: { width: "75%" }, children: "Property" })
211
- ] }),
212
- /* @__PURE__ */ s(Be, { children: t && Object.entries(r).map(
213
- ([o, l]) => {
214
- const d = r[o], c = l ? L(t, l) : null, f = L(n, o), m = f ? f.dataType === "array" && typeof f.of == "object" ? `${f.dataType} - ${f.of.dataType}` : f.dataType : void 0;
215
- return /* @__PURE__ */ p(Je, { style: { height: "90px" }, children: [
216
- /* @__PURE__ */ p(M, { style: { width: "20%" }, children: [
217
- /* @__PURE__ */ s(u, { variant: "body2", children: o }),
218
- f && /* @__PURE__ */ s(
219
- u,
220
- {
221
- variant: "caption",
222
- color: "secondary",
223
- children: m
224
- }
225
- )
226
- ] }),
227
- /* @__PURE__ */ s(M, { children: /* @__PURE__ */ s(Re, {}) }),
228
- /* @__PURE__ */ s(
229
- M,
230
- {
231
- className: o === e ? "text-center" : void 0,
232
- style: { width: "75%" },
233
- children: i?.({
234
- isIdColumn: o === e,
235
- property: c,
236
- propertyKey: d,
237
- importKey: o
238
- })
239
- }
240
- )
241
- ] }, o);
242
- }
243
- ) })
244
- ] })
245
- ] });
246
- }
247
- function st({
248
- idColumn: e,
249
- headersMapping: r,
250
- onChange: n
251
- }) {
252
- return /* @__PURE__ */ s("div", { children: /* @__PURE__ */ p(
253
- ge,
254
- {
255
- size: "small",
256
- value: e ?? "",
257
- onChange: (t) => {
258
- n(t.target.value);
259
- },
260
- renderValue: (t) => /* @__PURE__ */ s(u, { variant: "body2", children: t !== "" ? t : "Autogenerate ID" }),
261
- label: "Column that will be used as ID for each document",
262
- children: [
263
- /* @__PURE__ */ s(J, { value: "", children: "Autogenerate ID" }),
264
- Object.entries(r).map(([t, a]) => /* @__PURE__ */ s(J, { value: t, children: t }, t))
265
- ]
266
- }
267
- ) });
268
- }
269
- function ot({ onDataAdded: e }) {
270
- const r = pe();
271
- return /* @__PURE__ */ s(
272
- He,
273
- {
274
- accept: {
275
- "text/*": [".csv", ".xls", ".xlsx"],
276
- "application/vnd.ms-excel": [".xls", ".xlsx"],
277
- "application/msexcel": [".xls", ".xlsx"],
278
- "application/vnd.ms-office": [".xls", ".xlsx"],
279
- "application/xls": [".xls", ".xlsx"],
280
- "application/x-xls": [".xls", ".xlsx"],
281
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": [".xls", ".xlsx"],
282
- "application/json": [".json"]
283
- },
284
- preventDropOnDocument: !0,
285
- size: "small",
286
- maxFiles: 1,
287
- uploadDescription: /* @__PURE__ */ p($, { children: [
288
- /* @__PURE__ */ s(Ue, {}),
289
- "Drag and drop a file here or click to upload"
290
- ] }),
291
- onFilesAdded: (n) => {
292
- n.length > 0 && et(n[0]).then((t) => {
293
- e(t);
294
- }).catch((t) => {
295
- console.error("Error parsing file", t), r.open({ type: "error", message: t.message });
296
- });
297
- }
298
- }
299
- );
300
- }
301
- function Dt({
302
- propertyKey: e,
303
- property: r,
304
- onEditClick: n,
305
- includeName: t = !0,
306
- onPropertyNameChanged: a,
307
- propertyTypeView: i
308
- }) {
309
- const { propertyConfigs: o } = z(), l = r ? fe(r, o) : null;
310
- return /* @__PURE__ */ s(Ee, { children: /* @__PURE__ */ p(
311
- "div",
312
- {
313
- className: "flex flex-row w-full items-center",
314
- children: [
315
- /* @__PURE__ */ s("div", { className: "mx-4", children: i ?? /* @__PURE__ */ s(me, { propertyConfig: l ?? void 0 }) }),
316
- /* @__PURE__ */ s("div", { className: "w-full flex flex-col grow", children: /* @__PURE__ */ p("div", { className: "flex flex-row items-center gap-2", children: [
317
- t && /* @__PURE__ */ s(
318
- Ve,
319
- {
320
- size: "small",
321
- className: "text-base grow",
322
- value: r?.name ?? "",
323
- onChange: (d) => {
324
- a && e && a(e, d.target.value);
325
- }
326
- }
327
- ),
328
- /* @__PURE__ */ s(ee, { onClick: n, size: "small", children: /* @__PURE__ */ s(We, { size: "small" }) })
329
- ] }) })
330
- ]
331
- }
332
- ) });
333
- }
334
- function lt({
335
- importConfig: e,
336
- collection: r,
337
- onImportSuccess: n
338
- }) {
339
- const t = ue(), a = Ie(!1), [i, o] = j(0);
340
- function l() {
341
- a.current || (a.current = !0, De(
342
- t,
343
- r,
344
- e.entities,
345
- 0,
346
- 25,
347
- o
348
- ).then(() => {
349
- n(r), a.current = !1;
350
- }));
351
- }
352
- return ce(() => {
353
- l();
354
- }, []), /* @__PURE__ */ p(ze, { className: "flex flex-col gap-4 items-center", children: [
355
- /* @__PURE__ */ s(ye, {}),
356
- /* @__PURE__ */ s(u, { variant: "h6", children: "Saving data" }),
357
- /* @__PURE__ */ p(u, { variant: "body2", children: [
358
- i,
359
- "/",
360
- e.entities.length,
361
- " entities saved"
362
- ] }),
363
- /* @__PURE__ */ s(u, { variant: "caption", children: "Do not close this tab or the import will be interrupted." })
364
- ] });
365
- }
366
- function De(e, r, n, t = 0, a = 25, i) {
367
- console.debug("Saving imported data", t, a);
368
- const o = n.slice(t, t + a);
369
- return Promise.all(o.map((l) => e.saveEntity({
370
- path: r.path,
371
- // TODO: should check if this is correct, specially for subcollections
372
- values: l.values,
373
- entityId: l.id,
374
- collection: r,
375
- status: "new"
376
- }))).then(() => t + a < n.length ? (i(t + a), De(e, r, n, t + a, a, i)) : (i(n.length), Promise.resolve()));
377
- }
378
- function ct({
379
- collection: e,
380
- path: r,
381
- collectionEntitiesCount: n
382
- }) {
383
- const t = z(), a = pe(), [i, o] = C.useState(!1), [l, d] = C.useState("initial"), c = Ke(), f = x(() => {
384
- o(!0), d("initial");
385
- }, [o]), m = x(() => {
386
- o(!1);
387
- }, [o]), y = x(() => {
388
- d("preview");
389
- }, []), h = x(() => {
390
- d("import_data_saving");
391
- }, []), S = async (g) => {
392
- if (c.setImportData(g), g.length > 0) {
393
- const I = await Ze(g, nt);
394
- c.setOriginProperties(I);
395
- const A = Ne(g);
396
- c.setHeadersMapping(A);
397
- const E = Object.keys(A)?.[0];
398
- if (E?.includes("id") || E?.includes("key")) {
399
- const w = E;
400
- c.setIdColumn(w);
401
- }
402
- }
403
- setTimeout(() => {
404
- d("mapping");
405
- }, 100);
406
- }, N = he({
407
- collection: e,
408
- path: r,
409
- fields: t.propertyConfigs
410
- }), _ = Te(N.properties, N.propertiesOrder), O = Object.entries(_).flatMap(([g, I]) => Se(g, I, 0)), R = N.propertiesOrder ?? Object.keys(N.properties);
411
- return e.collectionGroup ? null : /* @__PURE__ */ p($, { children: [
412
- /* @__PURE__ */ s(te, { title: "Import", children: /* @__PURE__ */ s(ee, { color: "primary", onClick: f, children: /* @__PURE__ */ s(Ge, {}) }) }),
413
- /* @__PURE__ */ p(
414
- be,
415
- {
416
- open: i,
417
- fullWidth: l === "preview",
418
- fullHeight: l === "preview",
419
- maxWidth: l === "initial" ? "lg" : "7xl",
420
- children: [
421
- /* @__PURE__ */ p(xe, { className: "flex flex-col gap-4 my-4", fullHeight: l === "preview", children: [
422
- l === "initial" && /* @__PURE__ */ p($, { children: [
423
- /* @__PURE__ */ s(u, { variant: "h6", children: "Import data" }),
424
- /* @__PURE__ */ s(u, { variant: "body2", children: "Upload a CSV, Excel or JSON file and map it to your existing schema" }),
425
- /* @__PURE__ */ s(ot, { onDataAdded: S })
426
- ] }),
427
- l === "mapping" && /* @__PURE__ */ p($, { children: [
428
- /* @__PURE__ */ s(u, { variant: "h6", children: "Map fields" }),
429
- /* @__PURE__ */ s(
430
- it,
431
- {
432
- headersMapping: c.headersMapping,
433
- idColumn: c.idColumn,
434
- originProperties: c.originProperties,
435
- destinationProperties: _,
436
- onIdPropertyChanged: (g) => c.setIdColumn(g),
437
- buildPropertyView: ({
438
- isIdColumn: g,
439
- property: I,
440
- propertyKey: A,
441
- importKey: E
442
- }) => /* @__PURE__ */ s(
443
- dt,
444
- {
445
- selectedPropertyKey: A ?? "",
446
- properties: _,
447
- propertiesAndLevel: O,
448
- isIdColumn: g,
449
- onIdSelected: () => {
450
- c.setIdColumn(E);
451
- },
452
- onPropertySelected: (w) => {
453
- const G = Object.entries(c.headersMapping).map(([b, F]) => F === w ? { [b]: null } : b === E ? { [b]: w } : { [b]: F }).reduce((b, F) => ({ ...b, ...F }), {});
454
- c.setHeadersMapping(G), w === c.idColumn && c.setIdColumn(void 0);
455
- }
456
- }
457
- )
458
- }
459
- )
460
- ] }),
461
- l === "preview" && /* @__PURE__ */ s(
462
- pt,
463
- {
464
- importConfig: c,
465
- properties: _,
466
- propertiesOrder: R
467
- }
468
- ),
469
- l === "import_data_saving" && c && /* @__PURE__ */ s(
470
- lt,
471
- {
472
- importConfig: c,
473
- collection: e,
474
- onImportSuccess: (g) => {
475
- m(), a.open({
476
- type: "info",
477
- message: "Data imported successfully"
478
- });
479
- }
480
- }
481
- )
482
- ] }),
483
- /* @__PURE__ */ p(ve, { children: [
484
- l === "mapping" && /* @__PURE__ */ s(
485
- k,
486
- {
487
- onClick: () => d("initial"),
488
- variant: "text",
489
- children: "Back"
490
- }
491
- ),
492
- l === "preview" && /* @__PURE__ */ s(
493
- k,
494
- {
495
- onClick: () => d("mapping"),
496
- variant: "text",
497
- children: "Back"
498
- }
499
- ),
500
- /* @__PURE__ */ s(
501
- k,
502
- {
503
- onClick: m,
504
- variant: "text",
505
- children: "Cancel"
506
- }
507
- ),
508
- l === "mapping" && /* @__PURE__ */ s(
509
- k,
510
- {
511
- variant: "filled",
512
- onClick: y,
513
- children: "Next"
514
- }
515
- ),
516
- l === "preview" && /* @__PURE__ */ s(
517
- k,
518
- {
519
- variant: "filled",
520
- onClick: h,
521
- children: "Save data"
522
- }
523
- )
524
- ] })
525
- ]
526
- }
527
- )
528
- ] });
529
- }
530
- const V = "__internal_id__";
531
- function dt({
532
- selectedPropertyKey: e,
533
- properties: r,
534
- onPropertySelected: n,
535
- onIdSelected: t,
536
- propertiesAndLevel: a,
537
- isIdColumn: i
538
- }) {
539
- const o = e ? L(r, e) : null, l = x((c) => c === V ? /* @__PURE__ */ s(u, { variant: "body2", className: "p-4", children: "Use this column as ID" }) : !c || !o ? /* @__PURE__ */ s(u, { variant: "body2", className: "p-4", children: "Do not import this property" }) : /* @__PURE__ */ s(
540
- se,
541
- {
542
- propertyKey: c,
543
- property: o
544
- }
545
- ), [o]), d = x((c) => {
546
- c === V ? (t(), n(null)) : n(c === "" ? null : c);
547
- }, []);
548
- return /* @__PURE__ */ p(
549
- ge,
550
- {
551
- value: i ? V : e ?? void 0,
552
- onValueChange: d,
553
- renderValue: l,
554
- children: [
555
- /* @__PURE__ */ s(J, { value: "", children: /* @__PURE__ */ s(u, { variant: "body2", className: "p-4", children: "Do not import this property" }) }),
556
- /* @__PURE__ */ s(J, { value: V, children: /* @__PURE__ */ s(u, { variant: "body2", className: "p-4", children: "Use this column as ID" }) }),
557
- a.map(({
558
- property: c,
559
- level: f,
560
- propertyKey: m
561
- }) => /* @__PURE__ */ s(
562
- J,
563
- {
564
- value: m,
565
- disabled: c.dataType === "map",
566
- children: /* @__PURE__ */ s(
567
- se,
568
- {
569
- propertyKey: m,
570
- property: c,
571
- level: f
572
- }
573
- )
574
- },
575
- m
576
- ))
577
- ]
578
- }
579
- );
580
- }
581
- function Se(e, r, n) {
582
- const t = [];
583
- return t.push({
584
- property: r,
585
- level: n,
586
- propertyKey: e
587
- }), r.dataType === "map" && r.properties && Object.entries(r.properties).forEach(([a, i]) => {
588
- t.push(...Se(`${e}.${a}`, i, n + 1));
589
- }), t;
590
- }
591
- function se({
592
- propertyKey: e,
593
- property: r,
594
- level: n = 0
595
- }) {
596
- const { propertyConfigs: t } = z(), a = fe(r, t);
597
- return /* @__PURE__ */ p(
598
- "div",
599
- {
600
- className: "flex flex-row w-full text-start items-center h-full",
601
- children: [
602
- new Array(n).fill(0).map((i, o) => /* @__PURE__ */ s("div", { className: B(Xe, "ml-8 border-l h-12") }, o)),
603
- /* @__PURE__ */ s("div", { className: "m-4", children: /* @__PURE__ */ s(te, { title: a?.name, children: /* @__PURE__ */ s(me, { propertyConfig: a }) }) }),
604
- /* @__PURE__ */ p("div", { className: "flex flex-col flex-grow p-2 pl-2", children: [
605
- /* @__PURE__ */ s(
606
- u,
607
- {
608
- variant: "body1",
609
- component: "span",
610
- className: "flex-grow pr-2",
611
- children: r.name ? r.name : " "
612
- }
613
- ),
614
- /* @__PURE__ */ s(
615
- u,
616
- {
617
- className: " pr-2",
618
- variant: "body2",
619
- component: "span",
620
- color: "secondary",
621
- children: e
622
- }
623
- )
624
- ] })
625
- ]
626
- }
627
- );
628
- }
629
- function pt({
630
- importConfig: e,
631
- properties: r,
632
- propertiesOrder: n
633
- }) {
634
- ce(() => {
635
- const a = at(e.originProperties, r), i = e.importData.map((o) => rt(o, e.idColumn, e.headersMapping, r, a, "TEMP_PATH"));
636
- e.setEntities(i);
637
- }, []);
638
- const t = je();
639
- return /* @__PURE__ */ s(
640
- ke,
641
- {
642
- title: /* @__PURE__ */ p("div", { children: [
643
- /* @__PURE__ */ s(u, { variant: "subtitle2", children: "Imported data preview" }),
644
- /* @__PURE__ */ s(u, { variant: "caption", children: "Entities with the same id will be overwritten" })
645
- ] }),
646
- tableController: {
647
- data: e.entities,
648
- dataLoading: !1,
649
- noMoreToLoad: !1
650
- },
651
- endAdornment: /* @__PURE__ */ s("div", { className: "h-12" }),
652
- filterable: !1,
653
- sortable: !1,
654
- selectionController: t,
655
- displayedColumnIds: n.map((a) => ({
656
- key: a,
657
- disabled: !1
658
- })),
659
- properties: r
660
- }
661
- );
662
- }
663
- function Ne(e) {
664
- const r = {};
665
- return e.filter(Boolean).forEach((n) => {
666
- Object.keys(n).forEach((t) => {
667
- const a = n[t];
668
- typeof a == "object" && !Array.isArray(a) && Object.entries(Ne([a])).forEach(([i, o]) => {
669
- r[`${t}.${i}`] = `${t}.${o}`;
670
- }), r[t] = t;
671
- });
672
- }), r;
673
- }
674
- function ft(e, r, n, t, a, i, o) {
675
- console.debug("Downloading export", { dataLength: e.length, collection: n, exportType: i, dateExportType: o });
676
- const l = n.properties;
677
- if (i === "csv") {
678
- const d = t ? Fe(e.map((h) => h.values)) : {}, c = ht(l, a, d), f = mt(e, r, l, c, o), m = oe(c.map((h) => h.label)), y = f.map((h) => oe(h));
679
- le([m, ...y], `${n.name}.csv`, "text/csv");
680
- } else {
681
- const d = ut(e, r, l, o), c = JSON.stringify(d, null, 2);
682
- le([c], `${n.name}.json`, "application/json");
683
- }
684
- }
685
- function mt(e, r, n, t, a) {
686
- const i = e.map((o) => ({
687
- id: o.id,
688
- ...re(o.values, n, "csv", a)
689
- }));
690
- return r && r.forEach((o, l) => {
691
- i[l] = { ...i[l], ...o };
692
- }), i && i.map((o) => t.map((l) => Pe(o, l.key)));
693
- }
694
- function ut(e, r, n, t) {
695
- const a = e.map((i) => ({
696
- id: i.id,
697
- ...re(i.values, n, "json", t)
698
- }));
699
- return r && r.forEach((i, o) => {
700
- a[o] = { ...a[o], ...i };
701
- }), a;
702
- }
703
- function ht(e, r, n) {
704
- const t = [
705
- { label: "id", key: "id" },
706
- ...Object.entries(e).flatMap(([a, i]) => n && n[a] > 1 ? Array.from(
707
- { length: n[a] },
708
- (o, l) => K(i, `${a}[${l}]`, "")
709
- ).flat() : K(i, a, ""))
710
- ];
711
- return r && t.push(...r.map((a) => ({ label: a, key: a }))), t;
712
- }
713
- function K(e, r, n = "") {
714
- const t = n ? `${n}.${r}` : r;
715
- return e.dataType === "map" && e.properties ? Object.entries(e.properties).map(([a, i]) => K(i, a, t)).flat() : [{ label: t, key: t }];
716
- }
717
- function W(e, r, n, t) {
718
- let a;
719
- if (r.dataType === "map" && r.properties)
720
- a = re(e, r.properties, n, t);
721
- else if (r.dataType === "array")
722
- r.of && Array.isArray(e) ? Array.isArray(r.of) ? a = r.of.map((i, o) => W(e[o], i, n, t)) : r.of.dataType === "map" ? a = n === "csv" ? e.map((i) => JSON.stringify(i)) : e.map((i) => W(i, r.of, n, t)) : a = e.map((i) => W(i, r.of, n, t)) : a = e;
723
- else if (r.dataType === "reference" && e instanceof de) {
724
- const i = e || void 0;
725
- a = i ? i.pathWithId : null;
726
- } else
727
- r.dataType === "date" && e instanceof Date ? a = e ? t === "timestamp" ? e.getTime() : e.toISOString() : null : a = e;
728
- return a;
729
- }
730
- function re(e, r, n, t) {
731
- const a = Object.entries(r).map(([i, o]) => {
732
- const l = e && e[i], d = W(l, o, n, t);
733
- return d === void 0 ? {} : { [i]: d };
734
- }).reduce((i, o) => ({ ...i, ...o }), {});
735
- return { ...e, ...a };
736
- }
737
- function oe(e) {
738
- return e.map((r) => r == null ? "" : Array.isArray(r) ? '"' + JSON.stringify(r).replaceAll('"', '\\"') + '"' : '"' + String(r).replaceAll('"', '""') + '"').join(",") + `\r
739
- `;
740
- }
741
- function le(e, r, n) {
742
- const t = new Blob(e, { type: n }), a = URL.createObjectURL(t), i = document.createElement("a");
743
- i.href = a, i.setAttribute("download", r), i.click();
744
- }
745
- const gt = 500;
746
- function yt({
747
- collection: e,
748
- path: r,
749
- collectionEntitiesCount: n,
750
- exportAllowed: t,
751
- notAllowedView: a
752
- }) {
753
- const i = z(), o = typeof e.exportable == "object" ? e.exportable : void 0, l = C.useRef(/* @__PURE__ */ new Date()), [d, c] = C.useState(!0), [f, m] = C.useState("csv"), [y, h] = C.useState("string"), S = Me(), N = ue(), O = $e().resolveAliasesFrom(r), R = !t || t({
754
- collectionEntitiesCount: n,
755
- path: O,
756
- collection: e
757
- }), g = C.useMemo(() => he({
758
- collection: e,
759
- path: O,
760
- fields: i.propertyConfigs
761
- }), [e, O]), [I, A] = C.useState(!1), [E, w] = C.useState(), [G, b] = C.useState(!1), F = x(() => {
762
- b(!0);
763
- }, [b]), X = x(() => {
764
- b(!1);
765
- }, [b]), ne = x(async (P) => {
766
- const H = o?.additionalFields, D = g.additionalFields, q = H ? await Promise.all(P.map(async (T) => (await Promise.all(H.map(async (v) => ({
767
- [v.key]: await v.builder({
768
- entity: T,
769
- context: S
770
- })
771
- })))).reduce((v, Y) => ({ ...v, ...Y }), {}))) : [], Q = D ? await Promise.all(P.map(async (T) => (await Promise.all(D.map(async (v) => v.value ? {
772
- [v.key]: await v.value({
773
- entity: T,
774
- context: S
775
- })
776
- } : {}))).reduce((v, Y) => ({ ...v, ...Y }), {}))) : [];
777
- return [...q, ...Q];
778
- }, [o?.additionalFields]), ae = x(async (P, H) => {
779
- A(!0), N.fetchCollection({
780
- path: O,
781
- collection: P
782
- }).then(async (D) => {
783
- w(void 0);
784
- const q = await ne(D), Q = [
785
- ...H?.additionalFields?.map((T) => T.key) ?? [],
786
- ...P.additionalFields?.map((T) => T.key) ?? []
787
- ];
788
- ft(D, q, P, d, Q, f, y);
789
- }).catch((D) => {
790
- console.error("Error loading export data", D), w(D);
791
- }).finally(() => A(!1));
792
- }, [N, O, ne, d, f, y]), Oe = x(() => {
793
- ae(g, o), X();
794
- }, [ae, g, o, X]);
795
- return /* @__PURE__ */ p($, { children: [
796
- /* @__PURE__ */ s(te, { title: "Export", children: /* @__PURE__ */ s(ee, { color: "primary", onClick: F, children: /* @__PURE__ */ s(qe, {}) }) }),
797
- /* @__PURE__ */ p(
798
- be,
799
- {
800
- open: G,
801
- onOpenChange: b,
802
- maxWidth: "xl",
803
- children: [
804
- /* @__PURE__ */ p(xe, { className: "flex flex-col gap-4 my-4", children: [
805
- /* @__PURE__ */ s(u, { variant: "h6", children: "Export data" }),
806
- /* @__PURE__ */ s("div", { children: "Download the the content of this table as a CSV" }),
807
- n > gt && /* @__PURE__ */ s(Qe, { color: "warning", children: /* @__PURE__ */ p("div", { children: [
808
- "This collections has a large number of documents (",
809
- n,
810
- ")."
811
- ] }) }),
812
- /* @__PURE__ */ p("div", { className: "flex flex-row gap-4", children: [
813
- /* @__PURE__ */ p("div", { className: "p-4 flex flex-col", children: [
814
- /* @__PURE__ */ p("div", { className: "flex items-center", children: [
815
- /* @__PURE__ */ s(
816
- "input",
817
- {
818
- id: "radio-csv",
819
- type: "radio",
820
- value: "csv",
821
- name: "exportType",
822
- checked: f === "csv",
823
- onChange: () => m("csv"),
824
- className: B(U, "w-4 text-primary-dark bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")
825
- }
826
- ),
827
- /* @__PURE__ */ s(
828
- "label",
829
- {
830
- htmlFor: "radio-csv",
831
- className: "p-2 text-sm font-medium text-gray-900 dark:text-gray-300",
832
- children: "CSV"
833
- }
834
- )
835
- ] }),
836
- /* @__PURE__ */ p("div", { className: "flex items-center", children: [
837
- /* @__PURE__ */ s(
838
- "input",
839
- {
840
- id: "radio-json",
841
- type: "radio",
842
- value: "json",
843
- name: "exportType",
844
- checked: f === "json",
845
- onChange: () => m("json"),
846
- className: B(U, "w-4 text-primary-dark bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")
847
- }
848
- ),
849
- /* @__PURE__ */ s(
850
- "label",
851
- {
852
- htmlFor: "radio-json",
853
- className: "p-2 text-sm font-medium text-gray-900 dark:text-gray-300",
854
- children: "JSON"
855
- }
856
- )
857
- ] })
858
- ] }),
859
- /* @__PURE__ */ p("div", { className: "p-4 flex flex-col", children: [
860
- /* @__PURE__ */ p("div", { className: "flex items-center", children: [
861
- /* @__PURE__ */ s(
862
- "input",
863
- {
864
- id: "radio-timestamp",
865
- type: "radio",
866
- value: "timestamp",
867
- name: "dateExportType",
868
- checked: y === "timestamp",
869
- onChange: () => h("timestamp"),
870
- className: B(U, "w-4 text-primary-dark bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")
871
- }
872
- ),
873
- /* @__PURE__ */ p(
874
- "label",
875
- {
876
- htmlFor: "radio-timestamp",
877
- className: "p-2 text-sm font-medium text-gray-900 dark:text-gray-300",
878
- children: [
879
- "Dates as timestamps (",
880
- l.current.getTime(),
881
- ")"
882
- ]
883
- }
884
- )
885
- ] }),
886
- /* @__PURE__ */ p("div", { className: "flex items-center", children: [
887
- /* @__PURE__ */ s(
888
- "input",
889
- {
890
- id: "radio-string",
891
- type: "radio",
892
- value: "string",
893
- name: "dateExportType",
894
- checked: y === "string",
895
- onChange: () => h("string"),
896
- className: B(U, "w-4 text-primary-dark bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")
897
- }
898
- ),
899
- /* @__PURE__ */ p(
900
- "label",
901
- {
902
- htmlFor: "radio-string",
903
- className: "p-2 text-sm font-medium text-gray-900 dark:text-gray-300",
904
- children: [
905
- "Dates as strings (",
906
- l.current.toISOString(),
907
- ")"
908
- ]
909
- }
910
- )
911
- ] })
912
- ] })
913
- ] }),
914
- /* @__PURE__ */ s(
915
- Ye,
916
- {
917
- size: "small",
918
- disabled: f !== "csv",
919
- value: d,
920
- onValueChange: c,
921
- label: "Flatten arrays"
922
- }
923
- ),
924
- !R && a
925
- ] }),
926
- /* @__PURE__ */ p(ve, { children: [
927
- I && /* @__PURE__ */ s(ye, { size: "small" }),
928
- /* @__PURE__ */ s(
929
- k,
930
- {
931
- onClick: X,
932
- variant: "text",
933
- children: "Cancel"
934
- }
935
- ),
936
- /* @__PURE__ */ s(
937
- k,
938
- {
939
- variant: "filled",
940
- onClick: Oe,
941
- disabled: I || !R,
942
- children: "Download"
943
- }
944
- )
945
- ] })
946
- ]
947
- }
948
- )
949
- ] });
950
- }
951
- function St(e) {
952
- return Ae(() => ({
953
- name: "Import/Export",
954
- collections: {
955
- CollectionActions: [ct, yt],
956
- collectionActionsProps: e
957
- }
958
- }), [e]);
1
+ import { useMemo } from "react";
2
+ import { ImportCollectionAction } from "@firecms/data_import";
3
+ export * from "@firecms/data_import";
4
+ import { ExportCollectionAction } from "@firecms/data_export";
5
+ export * from "@firecms/data_export";
6
+ function useImportExportPlugin(props) {
7
+ console.warn("useImportExportPlugin is deprecated. Please use useImportPlugin and useExportPlugin separately");
8
+ return useMemo(() => ({
9
+ key: "import_export",
10
+ collectionView: {
11
+ CollectionActions: [ImportCollectionAction, ExportCollectionAction],
12
+ collectionActionsProps: props
13
+ }
14
+ }), [props]);
959
15
  }
960
16
  export {
961
- it as DataNewPropertiesMapping,
962
- ot as ImportFileUpload,
963
- Dt as ImportNewPropertyFieldPreview,
964
- lt as ImportSaveInProgress,
965
- rt as convertDataToEntity,
966
- et as convertFileToJson,
967
- we as flattenEntry,
968
- nt as getInferenceType,
969
- at as getPropertiesMapping,
970
- Z as processValueMapping,
971
- Ce as unflattenObject,
972
- Ke as useImportConfig,
973
- St as useImportExportPlugin
17
+ useImportExportPlugin
974
18
  };
975
19
  //# sourceMappingURL=index.es.js.map