@firecms/data_import_export 3.0.0-alpha.63 → 3.0.0-alpha.64

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/index.umd.js CHANGED
@@ -1,3 +1,3 @@
1
- (function(u,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("react"),require("react/jsx-runtime"),require("@firecms/core"),require("@firecms/schema_inference"),require("xlsx")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime","@firecms/core","@firecms/schema_inference","xlsx"],f):(u=typeof globalThis<"u"?globalThis:u||self,f(u.FireCMS={},u.React,u.jsxRuntime,u.core,u.schema_inference,u.XLSX))})(this,function(u,f,r,a,ce,de){"use strict";function pe(e){const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const s in e)if(s!=="default"){const t=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(n,s,t.get?t:{enumerable:!0,get:()=>e[s]})}}return n.default=e,Object.freeze(n)}const X=pe(de),z=()=>{const[e,n]=f.useState(!1),[s,t]=f.useState(),[l,i]=f.useState([]),[o,c]=f.useState([]),[p,d]=f.useState({}),[h,g]=f.useState({});return{inUse:e,setInUse:n,idColumn:s,setIdColumn:t,entities:o,setEntities:c,importData:l,setImportData:i,headersMapping:p,setHeadersMapping:d,originProperties:h,setOriginProperties:g}};function G(e){return new Promise((n,s)=>{if(e.type==="application/json"){const t=new FileReader;t.onload=function(l){const i=l.target?.result,o=JSON.parse(i);n(o)},t.readAsText(e)}else{const t=new FileReader;t.onload=function(l){const i=new Uint8Array(l.target?.result),o=X.read(i,{type:"array",codepage:65001,cellDates:!0}),c=o.SheetNames[0],p=o.Sheets[c],g=X.utils.sheet_to_json(p).map(fe).map(L);n(g)},t.readAsArrayBuffer(e)}})}function fe(e){return Object.keys(e).reduce((n,s)=>{try{n[s]=JSON.parse(e[s])}catch{n[s]=e[s]}return n},{})}function L(e){return Object.keys(e).reduce((n,s)=>{let t=n;const l=s.split(".");return l.forEach((i,o)=>{if(/^[\w]+\[\d+\]$/.test(i)){const c=i.slice(0,i.indexOf("[")),p=parseInt(i.slice(i.indexOf("[")+1,i.indexOf("]")));t[c]||(t[c]=[]),o!==l.length-1?(t[c][p]=t[c][p]||{},t=t[c][p]):t[c][p]=e[s]}else o!==l.length-1?(t[i]=t[i]||{},t=t[i]):t[i]=e[s]}),n},{})}function Q(e,n,s,t,l,i){const o=H(e);n&&delete o[n];const c=Object.entries(o).map(([d,h])=>{const g=s[d]??d;if(!a.getPropertyInPath(t,g))return{};const y=l[g];let T=h;return y&&(T=k(h,y)),{[g]:T}}).reduce((d,h)=>({...d,...h}),{}),p=L(c);return{id:n?e[n]:void 0,values:p,path:i}}function H(e,n=""){return Object.keys(e).reduce((s,t)=>{const l=n?`${n}.${t}`:t;return typeof e[t]=="object"&&e[t]!==null&&!Array.isArray(e[t])?Object.assign(s,H(e[t],l)):s[l]=e[t],s},{})}function k(e,n){if(n===void 0)return e;const{from:s,to:t}=n;if(s==="array"&&t==="array"&&n.fromSubtype&&n.toSubtype&&Array.isArray(e))return e.map(l=>k(l,{from:n.fromSubtype,to:n.toSubtype}));if(s==="string"&&t==="number"&&typeof e=="string")return Number(e);if(s==="string"&&t==="array"&&n.toSubtype&&typeof e=="string")return e.split(",").map(l=>k(l,{from:"string",to:n.toSubtype}));if(s==="string"&&t==="boolean")return e==="true";if(s==="number"&&t==="boolean")return e===1;if(s==="boolean"&&t==="number")return e?1:0;if(s==="boolean"&&t==="string")return e?"true":"false";if(s==="number"&&t==="string"&&typeof e=="number")return e.toString();if(s==="string"&&t==="array"&&typeof e=="string")return e.split(",").map(l=>l.trim());if(s==="string"&&t==="date"&&typeof e=="string")try{return new Date(e)}catch{return e}else{if(s==="date"&&t==="string")return e instanceof Date&&e.toISOString();if(s==="number"&&t==="date"&&typeof e=="number")try{return new Date(e)}catch{return e}else if(s==="string"&&t==="reference"&&typeof e=="string"){const l=e.split("/").slice(0,-1).join("/"),i=e.split("/").slice(-1)[0];return new a.EntityReference(i,l)}else{if(s===t)return e;if(s==="array"&&t==="string"&&Array.isArray(e))return e.join(",")}}return e}function Y(e){return typeof e=="number"?"number":typeof e=="string"?"string":typeof e=="boolean"?"boolean":e instanceof Date?"date":Array.isArray(e)?"array":"map"}function Z(e,n){function s(t,l){const i={};return Object.keys(t).forEach(o=>{const c=l?`${l}.${o}`:o,p=a.getPropertyInPath(t,o),d=a.getPropertyInPath(e,c);if(p){if(p.dataType==="map"&&p.properties){const h=s(p.properties,c);Object.keys(h).forEach(g=>{i[`${c}.${g}`]=h[g]});return}if(d){const h=d.dataType,g=p.dataType;let b,y;p.dataType==="array"&&p.of&&(y=p.of.dataType),d?.dataType==="array"&&d?.of&&(b=d.of.dataType),(h!==g||b!==y)&&(i[o]={from:h,to:g,fromSubtype:b,toSubtype:y})}}}),i}return s(n)}function K({idColumn:e,headersMapping:n,originProperties:s,destinationProperties:t,onIdPropertyChanged:l,buildPropertyView:i}){return r.jsxs(r.Fragment,{children:[r.jsx(he,{idColumn:e,headersMapping:n,onChange:l}),r.jsxs(a.Table,{style:{tableLayout:"fixed"},children:[r.jsxs(a.TableHeader,{children:[r.jsx(a.TableCell,{header:!0,style:{width:"20%"},children:"Column in file"}),r.jsx(a.TableCell,{header:!0}),r.jsx(a.TableCell,{header:!0,style:{width:"75%"},children:"Property"})]}),r.jsx(a.TableBody,{children:t&&Object.entries(n).map(([o,c])=>{const p=n[o],d=c?a.getPropertyInPath(t,c):null,h=a.getPropertyInPath(s,o),g=h?h.dataType==="array"&&typeof h.of=="object"?`${h.dataType} - ${h.of.dataType}`:h.dataType:void 0;return r.jsxs(a.TableRow,{style:{height:"90px"},children:[r.jsxs(a.TableCell,{style:{width:"20%"},children:[r.jsx(a.Typography,{variant:"body2",children:o}),h&&r.jsx(a.Typography,{variant:"caption",color:"secondary",children:g})]}),r.jsx(a.TableCell,{children:r.jsx(a.ChevronRightIcon,{})}),r.jsx(a.TableCell,{className:o===e?"text-center":void 0,style:{width:"75%"},children:i?.({isIdColumn:o===e,property:d,propertyKey:p,importKey:o})})]},o)})})]})]})}function he({idColumn:e,headersMapping:n,onChange:s}){return r.jsx("div",{children:r.jsxs(a.Select,{size:"small",value:e??"",onChange:t=>{s(t.target.value)},renderValue:t=>r.jsx(a.Typography,{variant:"body2",children:t!==""?t:"Autogenerate ID"}),label:"Column that will be used as ID for each document",children:[r.jsx(a.SelectItem,{value:"",children:"Autogenerate ID"}),Object.entries(n).map(([t,l])=>r.jsx(a.SelectItem,{value:t,children:t},t))]})})}function R({onDataAdded:e}){return r.jsx(a.FileUpload,{accept:{"text/*":[".csv",".xls",".xlsx"],"application/vnd.ms-excel":[".xls",".xlsx"],"application/msexcel":[".xls",".xlsx"],"application/vnd.ms-office":[".xls",".xlsx"],"application/xls":[".xls",".xlsx"],"application/x-xls":[".xls",".xlsx"],"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":[".xls",".xlsx"],"application/json":[".json"]},preventDropOnDocument:!0,size:"small",maxFiles:1,uploadDescription:r.jsxs(r.Fragment,{children:[r.jsx(a.UploadIcon,{}),"Drag and drop a file here or click to upload"]}),onFilesAdded:n=>{n.length>0&&G(n[0]).then(s=>{e(s)})}})}function ge({propertyKey:e,property:n,onEditClick:s,includeName:t=!0,onPropertyNameChanged:l,propertyTypeView:i}){const{propertyConfigs:o}=a.useFireCMSContext(),c=n?a.getFieldConfig(n,o):null;return r.jsx(a.ErrorBoundary,{children:r.jsxs("div",{className:"flex flex-row w-full items-center",children:[r.jsx("div",{className:"mx-4",children:i??r.jsx(a.FieldConfigBadge,{propertyConfig:c??void 0})}),r.jsx("div",{className:"w-full flex flex-col grow",children:r.jsxs("div",{className:"flex flex-row items-center gap-2",children:[t&&r.jsx(a.TextField,{size:"small",className:"text-base grow",value:n?.name??"",onChange:p=>{l&&e&&l(e,p.target.value)}}),r.jsx(a.IconButton,{onClick:s,size:"small",children:r.jsx(a.EditIcon,{size:"small"})})]})})]})})}function ee({importConfig:e,collection:n,onImportSuccess:s}){const t=a.useDataSource(),l=f.useRef(!1),[i,o]=f.useState(0);function c(){l.current||(l.current=!0,te(t,n,e.entities,0,25,o).then(()=>{s(n),l.current=!1}))}return f.useEffect(()=>{c()},[]),r.jsxs(a.CenteredView,{className:"flex flex-col gap-4 items-center",children:[r.jsx(a.CircularProgress,{}),r.jsx(a.Typography,{variant:"h6",children:"Saving data"}),r.jsxs(a.Typography,{variant:"body2",children:[i,"/",e.entities.length," entities saved"]}),r.jsx(a.Typography,{variant:"caption",children:"Do not close this tab or the import will be interrupted."})]})}function te(e,n,s,t=0,l=25,i){console.debug("Saving imported data",t,l);const o=s.slice(t,t+l);return Promise.all(o.map(c=>e.saveEntity({path:n.path,values:c.values,entityId:c.id,collection:n,status:"new"}))).then(()=>t+l<s.length?(i(t+l),te(e,n,s,t+l,l,i)):(i(s.length),Promise.resolve()))}function ue({collection:e,path:n,collectionEntitiesCount:s}){const t=a.useFireCMSContext(),l=a.useSnackbarController(),[i,o]=f.useState(!1),[c,p]=f.useState("initial"),d=z(),h=f.useCallback(()=>{o(!0),p("initial")},[o]),g=f.useCallback(()=>{o(!1)},[o]),b=f.useCallback(()=>{p("preview")},[]),y=f.useCallback(()=>{p("import_data_saving")},[]),T=async m=>{if(d.setImportData(m),m.length>0){const D=await ce.buildEntityPropertiesFromData(m,Y);d.setOriginProperties(D);const A=ae(m);d.setHeadersMapping(A);const v=Object.keys(A)?.[0];if(v?.includes("id")||v?.includes("key")){const I=v;d.setIdColumn(I)}}setTimeout(()=>{p("mapping")},100)},P=a.resolveCollection({collection:e,path:n,fields:t.propertyConfigs}),x=a.getPropertiesWithPropertiesOrder(P.properties,P.propertiesOrder),_=Object.entries(x).flatMap(([m,D])=>re(m,D,0)),E=P.propertiesOrder??Object.keys(P.properties);return e.collectionGroup?null:r.jsxs(r.Fragment,{children:[r.jsx(a.Tooltip,{title:"Import",children:r.jsx(a.IconButton,{color:"primary",onClick:h,children:r.jsx(a.FileUploadIcon,{})})}),r.jsxs(a.Dialog,{open:i,fullWidth:c==="preview",fullHeight:c==="preview",maxWidth:c==="initial"?"lg":"7xl",children:[r.jsxs(a.DialogContent,{className:"flex flex-col gap-4 my-4",fullHeight:c==="preview",children:[c==="initial"&&r.jsxs(r.Fragment,{children:[r.jsx(a.Typography,{variant:"h6",children:"Import data"}),r.jsx(a.Typography,{variant:"body2",children:"Upload a CSV, Excel or JSON file and map it to your existing schema"}),r.jsx(R,{onDataAdded:T})]}),c==="mapping"&&r.jsxs(r.Fragment,{children:[r.jsx(a.Typography,{variant:"h6",children:"Map fields"}),r.jsx(K,{headersMapping:d.headersMapping,idColumn:d.idColumn,originProperties:d.originProperties,destinationProperties:x,onIdPropertyChanged:m=>d.setIdColumn(m),buildPropertyView:({isIdColumn:m,property:D,propertyKey:A,importKey:v})=>r.jsx(ye,{selectedPropertyKey:A??"",properties:x,propertiesAndLevel:_,isIdColumn:m,onIdSelected:()=>{d.setIdColumn(v)},onPropertySelected:I=>{const O=Object.entries(d.headersMapping).map(([w,S])=>S===I?{[w]:null}:w===v?{[w]:I}:{[w]:S}).reduce((w,S)=>({...w,...S}),{});d.setHeadersMapping(O),I===d.idColumn&&d.setIdColumn(void 0)}})})]}),c==="preview"&&r.jsx(me,{importConfig:d,properties:x,propertiesOrder:E}),c==="import_data_saving"&&d&&r.jsx(ee,{importConfig:d,collection:e,onImportSuccess:m=>{g(),l.open({type:"info",message:"Data imported successfully"})}})]}),r.jsxs(a.DialogActions,{children:[c==="mapping"&&r.jsx(a.Button,{onClick:()=>p("initial"),variant:"text",children:"Back"}),c==="preview"&&r.jsx(a.Button,{onClick:()=>p("mapping"),variant:"text",children:"Back"}),r.jsx(a.Button,{onClick:g,variant:"text",children:"Cancel"}),c==="mapping"&&r.jsx(a.Button,{variant:"filled",onClick:b,children:"Next"}),c==="preview"&&r.jsx(a.Button,{variant:"filled",onClick:y,children:"Save data"})]})]})]})}const M="__internal_id__";function ye({selectedPropertyKey:e,properties:n,onPropertySelected:s,onIdSelected:t,propertiesAndLevel:l,isIdColumn:i}){const o=e?a.getPropertyInPath(n,e):null,c=f.useCallback(d=>d===M?r.jsx(a.Typography,{variant:"body2",className:"p-4",children:"Use this column as ID"}):!d||!o?r.jsx(a.Typography,{variant:"body2",className:"p-4",children:"Do not import this property"}):r.jsx(ne,{propertyKey:d,property:o}),[o]),p=f.useCallback(d=>{d===M?(t(),s(null)):s(d===""?null:d)},[]);return r.jsxs(a.Select,{value:i?M:e??void 0,onValueChange:p,renderValue:c,children:[r.jsx(a.SelectItem,{value:"",children:r.jsx(a.Typography,{variant:"body2",className:"p-4",children:"Do not import this property"})}),r.jsx(a.SelectItem,{value:M,children:r.jsx(a.Typography,{variant:"body2",className:"p-4",children:"Use this column as ID"})}),l.map(({property:d,level:h,propertyKey:g})=>r.jsx(a.SelectItem,{value:g,disabled:d.dataType==="map",children:r.jsx(ne,{propertyKey:g,property:d,level:h})},g))]})}function re(e,n,s){const t=[];return t.push({property:n,level:s,propertyKey:e}),n.dataType==="map"&&n.properties&&Object.entries(n.properties).forEach(([l,i])=>{t.push(...re(`${e}.${l}`,i,s+1))}),t}function ne({propertyKey:e,property:n,level:s=0}){const{propertyConfigs:t}=a.useFireCMSContext(),l=a.getFieldConfig(n,t);return r.jsxs("div",{className:"flex flex-row w-full text-start items-center h-full",children:[new Array(s).fill(0).map((i,o)=>r.jsx("div",{className:a.cn(a.defaultBorderMixin,"ml-8 border-l h-12")},o)),r.jsx("div",{className:"m-4",children:r.jsx(a.Tooltip,{title:l?.name,children:r.jsx(a.FieldConfigBadge,{propertyConfig:l})})}),r.jsxs("div",{className:"flex flex-col flex-grow p-2 pl-2",children:[r.jsx(a.Typography,{variant:"body1",component:"span",className:"flex-grow pr-2",children:n.name?n.name:" "}),r.jsx(a.Typography,{className:" pr-2",variant:"body2",component:"span",color:"secondary",children:e})]})]})}function me({importConfig:e,properties:n,propertiesOrder:s}){f.useEffect(()=>{const l=Z(e.originProperties,n),i=e.importData.map(o=>Q(o,e.idColumn,e.headersMapping,n,l,"TEMP_PATH"));e.setEntities(i)},[]);const t=a.useSelectionController();return r.jsx(a.EntityCollectionTable,{title:r.jsxs("div",{children:[r.jsx(a.Typography,{variant:"subtitle2",children:"Imported data preview"}),r.jsx(a.Typography,{variant:"caption",children:"Entities with the same id will be overwritten"})]}),tableController:{data:e.entities,dataLoading:!1,noMoreToLoad:!1},endAdornment:r.jsx("div",{className:"h-12"}),filterable:!1,sortable:!1,selectionController:t,displayedColumnIds:s.map(l=>({key:l,disabled:!1})),properties:n})}function ae(e){const n={};return e.forEach(s=>{Object.keys(s).forEach(t=>{const l=s[t];typeof l=="object"&&!Array.isArray(l)&&Object.entries(ae([l])).forEach(([i,o])=>{n[`${t}.${i}`]=`${t}.${o}`}),n[t]=t})}),n}function be(e,n,s,t,l,i,o){const c=s.properties;if(i==="csv"){const p=t?a.getArrayValuesCount(e.map(y=>y.values)):{},d=ve(c,l,p),h=xe(e,n,c,d,o),g=se(d.map(y=>y.label)),b=h.map(y=>se(y));le([g,...b],`${s.name}.csv`,"text/csv")}else{const p=Ce(e,n,c,o),d=JSON.stringify(p,null,2);le([d],`${s.name}.json`,"application/json")}}function xe(e,n,s,t,l){const i=e.map(o=>({id:o.id,...U(o.values,s,"csv",l)}));return n&&n.forEach((o,c)=>{i[c]={...i[c],...o}}),i&&i.map(o=>t.map(c=>a.getValueInPath(o,c.key)))}function Ce(e,n,s,t){const l=e.map(i=>({id:i.id,...U(i.values,s,"json",t)}));return n&&n.forEach((i,o)=>{l[o]={...l[o],...i}}),l}function ve(e,n,s){const t=[{label:"id",key:"id"},...Object.entries(e).flatMap(([l,i])=>s&&s[l]>1?Array.from({length:s[l]},(o,c)=>J(i,`${l}[${c}]`,"")).flat():J(i,l,""))];return n&&t.push(...n.map(l=>({label:l,key:l}))),t}function J(e,n,s=""){const t=s?`${s}.${n}`:n;return e.dataType==="map"&&e.properties?Object.entries(e.properties).map(([l,i])=>J(i,l,t)).flat():[{label:t,key:t}]}function B(e,n,s,t){let l;if(n.dataType==="map"&&n.properties)l=U(e,n.properties,s,t);else if(n.dataType==="array")n.of&&Array.isArray(e)?Array.isArray(n.of)?l=n.of.map((i,o)=>B(e[o],i,s,t)):n.of.dataType==="map"?l=s==="csv"?e.map(i=>JSON.stringify(i)):e.map(i=>B(i,n.of,s,t)):l=e.map(i=>B(i,n.of,s,t)):l=e;else if(n.dataType==="reference"&&e instanceof a.EntityReference){const i=e||void 0;l=i?i.pathWithId:null}else n.dataType==="date"&&e instanceof Date?l=e?t==="timestamp"?e.getTime():e.toISOString():null:l=e;return l}function U(e,n,s,t){const l=Object.entries(n).map(([i,o])=>{const c=e&&e[i],p=B(c,o,s,t);return p===void 0?{}:{[i]:p}}).reduce((i,o)=>({...i,...o}),{});return{...e,...l}}function se(e){return e.map(n=>n==null?"":Array.isArray(n)?'"'+JSON.stringify(n).replaceAll('"','\\"')+'"':'"'+String(n).replaceAll('"','""')+'"').join(",")+`\r
2
- `}function le(e,n,s){const t=new Blob(e,{type:s}),l=URL.createObjectURL(t),i=document.createElement("a");i.href=l,i.setAttribute("download",n),i.click()}const we=500;function Se({collection:e,path:n,collectionEntitiesCount:s,exportAllowed:t,notAllowedView:l}){const i=typeof e.exportable=="object"?e.exportable:void 0,o=f.useRef(new Date),[c,p]=f.useState(!0),[d,h]=f.useState("csv"),[g,b]=f.useState("string"),y=a.useFireCMSContext(),T=a.useDataSource(),x=a.useNavigationController().resolveAliasesFrom(n),_=!t||t({collectionEntitiesCount:s,path:x,collection:e}),E=f.useMemo(()=>a.resolveCollection({collection:e,path:x,fields:y.propertyConfigs}),[e,x]),[m,D]=f.useState(!1),[A,v]=f.useState(),[I,O]=f.useState(!1),w=f.useCallback(()=>{O(!0)},[O]),S=f.useCallback(()=>{O(!1)},[O]),ie=f.useCallback(async F=>{const $=i?.additionalFields,j=E.additionalFields,V=$?await Promise.all(F.map(async N=>(await Promise.all($.map(async C=>({[C.key]:await C.builder({entity:N,context:y})})))).reduce((C,q)=>({...C,...q}),{}))):[],W=j?await Promise.all(F.map(async N=>(await Promise.all(j.map(async C=>C.value?{[C.key]:await C.value({entity:N,context:y})}:{}))).reduce((C,q)=>({...C,...q}),{}))):[];return[...V,...W]},[i?.additionalFields]),oe=f.useCallback(async(F,$)=>{D(!0),T.fetchCollection({path:x,collection:F}).then(async j=>{v(void 0);const V=await ie(j),W=[...$?.additionalFields?.map(N=>N.key)??[],...F.additionalFields?.map(N=>N.key)??[]];be(j,V,F,c,W,d,g)}).catch(v).finally(()=>D(!1))},[T,x,ie,c,d,g]),De=f.useCallback(()=>{oe(E,i),S()},[oe,E,i,S]);return r.jsxs(r.Fragment,{children:[r.jsx(a.Tooltip,{title:"Export",children:r.jsx(a.IconButton,{color:"primary",onClick:w,children:r.jsx(a.GetAppIcon,{})})}),r.jsxs(a.Dialog,{open:I,onOpenChange:O,maxWidth:"xl",children:[r.jsxs(a.DialogContent,{className:"flex flex-col gap-4 my-4",children:[r.jsx(a.Typography,{variant:"h6",children:"Export data"}),r.jsx("div",{children:"Download the the content of this table as a CSV"}),s>we&&r.jsx(a.Alert,{color:"warning",children:r.jsxs("div",{children:["This collections has a large number of documents (",s,")."]})}),r.jsxs("div",{className:"flex flex-row gap-4",children:[r.jsxs("div",{className:"p-4 flex flex-col",children:[r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-csv",type:"radio",value:"csv",name:"exportType",checked:d==="csv",onChange:()=>h("csv"),className:a.cn(a.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsx("label",{htmlFor:"radio-csv",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:"CSV"})]}),r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-json",type:"radio",value:"json",name:"exportType",checked:d==="json",onChange:()=>h("json"),className:a.cn(a.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsx("label",{htmlFor:"radio-json",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:"JSON"})]})]}),r.jsxs("div",{className:"p-4 flex flex-col",children:[r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-timestamp",type:"radio",value:"timestamp",name:"dateExportType",checked:g==="timestamp",onChange:()=>b("timestamp"),className:a.cn(a.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsxs("label",{htmlFor:"radio-timestamp",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:["Dates as timestamps (",o.current.getTime(),")"]})]}),r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-string",type:"radio",value:"string",name:"dateExportType",checked:g==="string",onChange:()=>b("string"),className:a.cn(a.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsxs("label",{htmlFor:"radio-string",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:["Dates as strings (",o.current.toISOString(),")"]})]})]})]}),r.jsx(a.BooleanSwitchWithLabel,{size:"small",disabled:d!=="csv",value:c,onValueChange:p,label:"Flatten arrays"}),!_&&l]}),r.jsxs(a.DialogActions,{children:[m&&r.jsx(a.CircularProgress,{size:"small"}),r.jsx(a.Button,{onClick:S,variant:"text",children:"Cancel"}),r.jsx(a.Button,{variant:"filled",onClick:De,disabled:m||!_,children:"Download"})]})]})]})}function Te(e){return f.useMemo(()=>({name:"Import/Export",collections:{CollectionActions:[ue,Se],collectionActionsProps:e}}),[e])}u.DataNewPropertiesMapping=K,u.ImportFileUpload=R,u.ImportNewPropertyFieldPreview=ge,u.ImportSaveInProgress=ee,u.convertDataToEntity=Q,u.convertFileToJson=G,u.flattenEntry=H,u.getInferenceType=Y,u.getPropertiesMapping=Z,u.processValueMapping=k,u.unflattenObject=L,u.useImportConfig=z,u.useImportExportPlugin=Te,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
1
+ (function(m,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("react"),require("react/jsx-runtime"),require("@firecms/core"),require("@firecms/ui"),require("@firecms/schema_inference"),require("xlsx")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime","@firecms/core","@firecms/ui","@firecms/schema_inference","xlsx"],f):(m=typeof globalThis<"u"?globalThis:m||self,f(m.FireCMS={},m.React,m.jsxRuntime,m.core,m.ui,m.schema_inference,m.XLSX))})(this,function(m,f,r,y,l,de,pe){"use strict";function fe(e){const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const a in e)if(a!=="default"){const t=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(n,a,t.get?t:{enumerable:!0,get:()=>e[a]})}}return n.default=e,Object.freeze(n)}const z=fe(pe),G=()=>{const[e,n]=f.useState(!1),[a,t]=f.useState(),[s,o]=f.useState([]),[i,c]=f.useState([]),[p,d]=f.useState({}),[h,g]=f.useState({});return{inUse:e,setInUse:n,idColumn:a,setIdColumn:t,entities:i,setEntities:c,importData:s,setImportData:o,headersMapping:p,setHeadersMapping:d,originProperties:h,setOriginProperties:g}};function Q(e){return new Promise((n,a)=>{if(e.type==="application/json"){const t=new FileReader;t.onload=function(s){const o=s.target?.result,i=JSON.parse(o);n(i)},t.readAsText(e)}else{const t=new FileReader;t.onload=function(s){const o=new Uint8Array(s.target?.result),i=z.read(o,{type:"array",codepage:65001,cellDates:!0}),c=i.SheetNames[0],p=i.Sheets[c],g=z.utils.sheet_to_json(p).map(he).map(H);n(g)},t.readAsArrayBuffer(e)}})}function he(e){return Object.keys(e).reduce((n,a)=>{try{n[a]=JSON.parse(e[a])}catch{n[a]=e[a]}return n},{})}function H(e){return Object.keys(e).reduce((n,a)=>{let t=n;const s=a.split(".");return s.forEach((o,i)=>{if(/^[\w]+\[\d+\]$/.test(o)){const c=o.slice(0,o.indexOf("[")),p=parseInt(o.slice(o.indexOf("[")+1,o.indexOf("]")));t[c]||(t[c]=[]),i!==s.length-1?(t[c][p]=t[c][p]||{},t=t[c][p]):t[c][p]=e[a]}else i!==s.length-1?(t[o]=t[o]||{},t=t[o]):t[o]=e[a]}),n},{})}function Y(e,n,a,t,s,o){const i=J(e);n&&delete i[n];const c=Object.entries(i).map(([d,h])=>{const g=a[d]??d;if(!y.getPropertyInPath(t,g))return{};const b=s[g];let D=h;return b&&(D=M(h,b)),{[g]:D}}).reduce((d,h)=>({...d,...h}),{}),p=H(c);return{id:n?e[n]:void 0,values:p,path:o}}function J(e,n=""){return Object.keys(e).reduce((a,t)=>{const s=n?`${n}.${t}`:t;return typeof e[t]=="object"&&e[t]!==null&&!Array.isArray(e[t])?Object.assign(a,J(e[t],s)):a[s]=e[t],a},{})}function M(e,n){if(n===void 0)return e;const{from:a,to:t}=n;if(a==="array"&&t==="array"&&n.fromSubtype&&n.toSubtype&&Array.isArray(e))return e.map(s=>M(s,{from:n.fromSubtype,to:n.toSubtype}));if(a==="string"&&t==="number"&&typeof e=="string")return Number(e);if(a==="string"&&t==="array"&&n.toSubtype&&typeof e=="string")return e.split(",").map(s=>M(s,{from:"string",to:n.toSubtype}));if(a==="string"&&t==="boolean")return e==="true";if(a==="number"&&t==="boolean")return e===1;if(a==="boolean"&&t==="number")return e?1:0;if(a==="boolean"&&t==="string")return e?"true":"false";if(a==="number"&&t==="string"&&typeof e=="number")return e.toString();if(a==="string"&&t==="array"&&typeof e=="string")return e.split(",").map(s=>s.trim());if(a==="string"&&t==="date"&&typeof e=="string")try{return new Date(e)}catch{return e}else{if(a==="date"&&t==="string")return e instanceof Date&&e.toISOString();if(a==="number"&&t==="date"&&typeof e=="number")try{return new Date(e)}catch{return e}else if(a==="string"&&t==="reference"&&typeof e=="string"){const s=e.split("/").slice(0,-1).join("/"),o=e.split("/").slice(-1)[0];return new y.EntityReference(o,s)}else{if(a===t)return e;if(a==="array"&&t==="string"&&Array.isArray(e))return e.join(",")}}return e}function Z(e){return typeof e=="number"?"number":typeof e=="string"?"string":typeof e=="boolean"?"boolean":e instanceof Date?"date":Array.isArray(e)?"array":"map"}function K(e,n){function a(t,s){const o={};return Object.keys(t).forEach(i=>{const c=s?`${s}.${i}`:i,p=y.getPropertyInPath(t,i),d=y.getPropertyInPath(e,c);if(p){if(p.dataType==="map"&&p.properties){const h=a(p.properties,c);Object.keys(h).forEach(g=>{o[`${c}.${g}`]=h[g]});return}if(d){const h=d.dataType,g=p.dataType;let x,b;p.dataType==="array"&&p.of&&(b=p.of.dataType),d?.dataType==="array"&&d?.of&&(x=d.of.dataType),(h!==g||x!==b)&&(o[i]={from:h,to:g,fromSubtype:x,toSubtype:b})}}}),o}return a(n)}function R({idColumn:e,headersMapping:n,originProperties:a,destinationProperties:t,onIdPropertyChanged:s,buildPropertyView:o}){return r.jsxs(r.Fragment,{children:[r.jsx(ge,{idColumn:e,headersMapping:n,onChange:s}),r.jsxs(l.Table,{style:{tableLayout:"fixed"},children:[r.jsxs(l.TableHeader,{children:[r.jsx(l.TableCell,{header:!0,style:{width:"20%"},children:"Column in file"}),r.jsx(l.TableCell,{header:!0}),r.jsx(l.TableCell,{header:!0,style:{width:"75%"},children:"Property"})]}),r.jsx(l.TableBody,{children:t&&Object.entries(n).map(([i,c])=>{const p=n[i],d=c?y.getPropertyInPath(t,c):null,h=y.getPropertyInPath(a,i),g=h?h.dataType==="array"&&typeof h.of=="object"?`${h.dataType} - ${h.of.dataType}`:h.dataType:void 0;return r.jsxs(l.TableRow,{style:{height:"90px"},children:[r.jsxs(l.TableCell,{style:{width:"20%"},children:[r.jsx(l.Typography,{variant:"body2",children:i}),h&&r.jsx(l.Typography,{variant:"caption",color:"secondary",children:g})]}),r.jsx(l.TableCell,{children:r.jsx(l.ChevronRightIcon,{})}),r.jsx(l.TableCell,{className:i===e?"text-center":void 0,style:{width:"75%"},children:o?.({isIdColumn:i===e,property:d,propertyKey:p,importKey:i})})]},i)})})]})]})}function ge({idColumn:e,headersMapping:n,onChange:a}){return r.jsx("div",{children:r.jsxs(l.Select,{size:"small",value:e??"",onChange:t=>{a(t.target.value)},renderValue:t=>r.jsx(l.Typography,{variant:"body2",children:t!==""?t:"Autogenerate ID"}),label:"Column that will be used as ID for each document",children:[r.jsx(l.SelectItem,{value:"",children:"Autogenerate ID"}),Object.entries(n).map(([t,s])=>r.jsx(l.SelectItem,{value:t,children:t},t))]})})}function ee({onDataAdded:e}){return r.jsx(l.FileUpload,{accept:{"text/*":[".csv",".xls",".xlsx"],"application/vnd.ms-excel":[".xls",".xlsx"],"application/msexcel":[".xls",".xlsx"],"application/vnd.ms-office":[".xls",".xlsx"],"application/xls":[".xls",".xlsx"],"application/x-xls":[".xls",".xlsx"],"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":[".xls",".xlsx"],"application/json":[".json"]},preventDropOnDocument:!0,size:"small",maxFiles:1,uploadDescription:r.jsxs(r.Fragment,{children:[r.jsx(l.UploadIcon,{}),"Drag and drop a file here or click to upload"]}),onFilesAdded:n=>{n.length>0&&Q(n[0]).then(a=>{e(a)})}})}function ye({propertyKey:e,property:n,onEditClick:a,includeName:t=!0,onPropertyNameChanged:s,propertyTypeView:o}){const{propertyConfigs:i}=y.useFireCMSContext(),c=n?y.getFieldConfig(n,i):null;return r.jsx(y.ErrorBoundary,{children:r.jsxs("div",{className:"flex flex-row w-full items-center",children:[r.jsx("div",{className:"mx-4",children:o??r.jsx(y.FieldConfigBadge,{propertyConfig:c??void 0})}),r.jsx("div",{className:"w-full flex flex-col grow",children:r.jsxs("div",{className:"flex flex-row items-center gap-2",children:[t&&r.jsx(l.TextField,{size:"small",className:"text-base grow",value:n?.name??"",onChange:p=>{s&&e&&s(e,p.target.value)}}),r.jsx(l.IconButton,{onClick:a,size:"small",children:r.jsx(l.EditIcon,{size:"small"})})]})})]})})}function te({importConfig:e,collection:n,onImportSuccess:a}){const t=y.useDataSource(),s=f.useRef(!1),[o,i]=f.useState(0);function c(){s.current||(s.current=!0,re(t,n,e.entities,0,25,i).then(()=>{a(n),s.current=!1}))}return f.useEffect(()=>{c()},[]),r.jsxs(l.CenteredView,{className:"flex flex-col gap-4 items-center",children:[r.jsx(l.CircularProgress,{}),r.jsx(l.Typography,{variant:"h6",children:"Saving data"}),r.jsxs(l.Typography,{variant:"body2",children:[o,"/",e.entities.length," entities saved"]}),r.jsx(l.Typography,{variant:"caption",children:"Do not close this tab or the import will be interrupted."})]})}function re(e,n,a,t=0,s=25,o){console.debug("Saving imported data",t,s);const i=a.slice(t,t+s);return Promise.all(i.map(c=>e.saveEntity({path:n.path,values:c.values,entityId:c.id,collection:n,status:"new"}))).then(()=>t+s<a.length?(o(t+s),re(e,n,a,t+s,s,o)):(o(a.length),Promise.resolve()))}function me({collection:e,path:n,collectionEntitiesCount:a}){const t=y.useFireCMSContext(),s=y.useSnackbarController(),[o,i]=f.useState(!1),[c,p]=f.useState("initial"),d=G(),h=f.useCallback(()=>{i(!0),p("initial")},[i]),g=f.useCallback(()=>{i(!1)},[i]),x=f.useCallback(()=>{p("preview")},[]),b=f.useCallback(()=>{p("import_data_saving")},[]),D=async u=>{if(d.setImportData(u),u.length>0){const I=await de.buildEntityPropertiesFromData(u,Z);d.setOriginProperties(I);const j=se(u);d.setHeadersMapping(j);const w=Object.keys(j)?.[0];if(w?.includes("id")||w?.includes("key")){const O=w;d.setIdColumn(O)}}setTimeout(()=>{p("mapping")},100)},E=y.resolveCollection({collection:e,path:n,fields:t.propertyConfigs}),C=y.getPropertiesWithPropertiesOrder(E.properties,E.propertiesOrder),$=Object.entries(C).flatMap(([u,I])=>ne(u,I,0)),A=E.propertiesOrder??Object.keys(E.properties);return e.collectionGroup?null:r.jsxs(r.Fragment,{children:[r.jsx(l.Tooltip,{title:"Import",children:r.jsx(l.IconButton,{color:"primary",onClick:h,children:r.jsx(l.FileUploadIcon,{})})}),r.jsxs(l.Dialog,{open:o,fullWidth:c==="preview",fullHeight:c==="preview",maxWidth:c==="initial"?"lg":"7xl",children:[r.jsxs(l.DialogContent,{className:"flex flex-col gap-4 my-4",fullHeight:c==="preview",children:[c==="initial"&&r.jsxs(r.Fragment,{children:[r.jsx(l.Typography,{variant:"h6",children:"Import data"}),r.jsx(l.Typography,{variant:"body2",children:"Upload a CSV, Excel or JSON file and map it to your existing schema"}),r.jsx(ee,{onDataAdded:D})]}),c==="mapping"&&r.jsxs(r.Fragment,{children:[r.jsx(l.Typography,{variant:"h6",children:"Map fields"}),r.jsx(R,{headersMapping:d.headersMapping,idColumn:d.idColumn,originProperties:d.originProperties,destinationProperties:C,onIdPropertyChanged:u=>d.setIdColumn(u),buildPropertyView:({isIdColumn:u,property:I,propertyKey:j,importKey:w})=>r.jsx(be,{selectedPropertyKey:j??"",properties:C,propertiesAndLevel:$,isIdColumn:u,onIdSelected:()=>{d.setIdColumn(w)},onPropertySelected:O=>{const N=Object.entries(d.headersMapping).map(([S,T])=>T===O?{[S]:null}:S===w?{[S]:O}:{[S]:T}).reduce((S,T)=>({...S,...T}),{});d.setHeadersMapping(N),O===d.idColumn&&d.setIdColumn(void 0)}})})]}),c==="preview"&&r.jsx(ue,{importConfig:d,properties:C,propertiesOrder:A}),c==="import_data_saving"&&d&&r.jsx(te,{importConfig:d,collection:e,onImportSuccess:u=>{g(),s.open({type:"info",message:"Data imported successfully"})}})]}),r.jsxs(l.DialogActions,{children:[c==="mapping"&&r.jsx(l.Button,{onClick:()=>p("initial"),variant:"text",children:"Back"}),c==="preview"&&r.jsx(l.Button,{onClick:()=>p("mapping"),variant:"text",children:"Back"}),r.jsx(l.Button,{onClick:g,variant:"text",children:"Cancel"}),c==="mapping"&&r.jsx(l.Button,{variant:"filled",onClick:x,children:"Next"}),c==="preview"&&r.jsx(l.Button,{variant:"filled",onClick:b,children:"Save data"})]})]})]})}const B="__internal_id__";function be({selectedPropertyKey:e,properties:n,onPropertySelected:a,onIdSelected:t,propertiesAndLevel:s,isIdColumn:o}){const i=e?y.getPropertyInPath(n,e):null,c=f.useCallback(d=>d===B?r.jsx(l.Typography,{variant:"body2",className:"p-4",children:"Use this column as ID"}):!d||!i?r.jsx(l.Typography,{variant:"body2",className:"p-4",children:"Do not import this property"}):r.jsx(ae,{propertyKey:d,property:i}),[i]),p=f.useCallback(d=>{d===B?(t(),a(null)):a(d===""?null:d)},[]);return r.jsxs(l.Select,{value:o?B:e??void 0,onValueChange:p,renderValue:c,children:[r.jsx(l.SelectItem,{value:"",children:r.jsx(l.Typography,{variant:"body2",className:"p-4",children:"Do not import this property"})}),r.jsx(l.SelectItem,{value:B,children:r.jsx(l.Typography,{variant:"body2",className:"p-4",children:"Use this column as ID"})}),s.map(({property:d,level:h,propertyKey:g})=>r.jsx(l.SelectItem,{value:g,disabled:d.dataType==="map",children:r.jsx(ae,{propertyKey:g,property:d,level:h})},g))]})}function ne(e,n,a){const t=[];return t.push({property:n,level:a,propertyKey:e}),n.dataType==="map"&&n.properties&&Object.entries(n.properties).forEach(([s,o])=>{t.push(...ne(`${e}.${s}`,o,a+1))}),t}function ae({propertyKey:e,property:n,level:a=0}){const{propertyConfigs:t}=y.useFireCMSContext(),s=y.getFieldConfig(n,t);return r.jsxs("div",{className:"flex flex-row w-full text-start items-center h-full",children:[new Array(a).fill(0).map((o,i)=>r.jsx("div",{className:l.cn(l.defaultBorderMixin,"ml-8 border-l h-12")},i)),r.jsx("div",{className:"m-4",children:r.jsx(l.Tooltip,{title:s?.name,children:r.jsx(y.FieldConfigBadge,{propertyConfig:s})})}),r.jsxs("div",{className:"flex flex-col flex-grow p-2 pl-2",children:[r.jsx(l.Typography,{variant:"body1",component:"span",className:"flex-grow pr-2",children:n.name?n.name:" "}),r.jsx(l.Typography,{className:" pr-2",variant:"body2",component:"span",color:"secondary",children:e})]})]})}function ue({importConfig:e,properties:n,propertiesOrder:a}){f.useEffect(()=>{const s=K(e.originProperties,n),o=e.importData.map(i=>Y(i,e.idColumn,e.headersMapping,n,s,"TEMP_PATH"));e.setEntities(o)},[]);const t=y.useSelectionController();return r.jsx(y.EntityCollectionTable,{title:r.jsxs("div",{children:[r.jsx(l.Typography,{variant:"subtitle2",children:"Imported data preview"}),r.jsx(l.Typography,{variant:"caption",children:"Entities with the same id will be overwritten"})]}),tableController:{data:e.entities,dataLoading:!1,noMoreToLoad:!1},endAdornment:r.jsx("div",{className:"h-12"}),filterable:!1,sortable:!1,selectionController:t,displayedColumnIds:a.map(s=>({key:s,disabled:!1})),properties:n})}function se(e){const n={};return e.filter(Boolean).forEach(a=>{Object.keys(a).forEach(t=>{const s=a[t];typeof s=="object"&&!Array.isArray(s)&&Object.entries(se([s])).forEach(([o,i])=>{n[`${t}.${o}`]=`${t}.${i}`}),n[t]=t})}),n}function xe(e,n,a,t,s,o,i){const c=a.properties;if(o==="csv"){const p=t?y.getArrayValuesCount(e.map(b=>b.values)):{},d=we(c,s,p),h=Ce(e,n,c,d,i),g=oe(d.map(b=>b.label)),x=h.map(b=>oe(b));le([g,...x],`${a.name}.csv`,"text/csv")}else{const p=ve(e,n,c,i),d=JSON.stringify(p,null,2);le([d],`${a.name}.json`,"application/json")}}function Ce(e,n,a,t,s){const o=e.map(i=>({id:i.id,...V(i.values,a,"csv",s)}));return n&&n.forEach((i,c)=>{o[c]={...o[c],...i}}),o&&o.map(i=>t.map(c=>y.getValueInPath(i,c.key)))}function ve(e,n,a,t){const s=e.map(o=>({id:o.id,...V(o.values,a,"json",t)}));return n&&n.forEach((o,i)=>{s[i]={...s[i],...o}}),s}function we(e,n,a){const t=[{label:"id",key:"id"},...Object.entries(e).flatMap(([s,o])=>a&&a[s]>1?Array.from({length:a[s]},(i,c)=>U(o,`${s}[${c}]`,"")).flat():U(o,s,""))];return n&&t.push(...n.map(s=>({label:s,key:s}))),t}function U(e,n,a=""){const t=a?`${a}.${n}`:n;return e.dataType==="map"&&e.properties?Object.entries(e.properties).map(([s,o])=>U(o,s,t)).flat():[{label:t,key:t}]}function _(e,n,a,t){let s;if(n.dataType==="map"&&n.properties)s=V(e,n.properties,a,t);else if(n.dataType==="array")n.of&&Array.isArray(e)?Array.isArray(n.of)?s=n.of.map((o,i)=>_(e[i],o,a,t)):n.of.dataType==="map"?s=a==="csv"?e.map(o=>JSON.stringify(o)):e.map(o=>_(o,n.of,a,t)):s=e.map(o=>_(o,n.of,a,t)):s=e;else if(n.dataType==="reference"&&e instanceof y.EntityReference){const o=e||void 0;s=o?o.pathWithId:null}else n.dataType==="date"&&e instanceof Date?s=e?t==="timestamp"?e.getTime():e.toISOString():null:s=e;return s}function V(e,n,a,t){const s=Object.entries(n).map(([o,i])=>{const c=e&&e[o],p=_(c,i,a,t);return p===void 0?{}:{[o]:p}}).reduce((o,i)=>({...o,...i}),{});return{...e,...s}}function oe(e){return e.map(n=>n==null?"":Array.isArray(n)?'"'+JSON.stringify(n).replaceAll('"','\\"')+'"':'"'+String(n).replaceAll('"','""')+'"').join(",")+`\r
2
+ `}function le(e,n,a){const t=new Blob(e,{type:a}),s=URL.createObjectURL(t),o=document.createElement("a");o.href=s,o.setAttribute("download",n),o.click()}const Se=500;function Te({collection:e,path:n,collectionEntitiesCount:a,exportAllowed:t,notAllowedView:s}){const o=typeof e.exportable=="object"?e.exportable:void 0,i=f.useRef(new Date),[c,p]=f.useState(!0),[d,h]=f.useState("csv"),[g,x]=f.useState("string"),b=y.useFireCMSContext(),D=y.useDataSource(),C=y.useNavigationController().resolveAliasesFrom(n),$=!t||t({collectionEntitiesCount:a,path:C,collection:e}),A=f.useMemo(()=>y.resolveCollection({collection:e,path:C,fields:b.propertyConfigs}),[e,C]),[u,I]=f.useState(!1),[j,w]=f.useState(),[O,N]=f.useState(!1),S=f.useCallback(()=>{N(!0)},[N]),T=f.useCallback(()=>{N(!1)},[N]),ie=f.useCallback(async P=>{const L=o?.additionalFields,k=A.additionalFields,q=L?await Promise.all(P.map(async F=>(await Promise.all(L.map(async v=>({[v.key]:await v.builder({entity:F,context:b})})))).reduce((v,X)=>({...v,...X}),{}))):[],W=k?await Promise.all(P.map(async F=>(await Promise.all(k.map(async v=>v.value?{[v.key]:await v.value({entity:F,context:b})}:{}))).reduce((v,X)=>({...v,...X}),{}))):[];return[...q,...W]},[o?.additionalFields]),ce=f.useCallback(async(P,L)=>{I(!0),D.fetchCollection({path:C,collection:P}).then(async k=>{w(void 0);const q=await ie(k),W=[...L?.additionalFields?.map(F=>F.key)??[],...P.additionalFields?.map(F=>F.key)??[]];xe(k,q,P,c,W,d,g)}).catch(w).finally(()=>I(!1))},[D,C,ie,c,d,g]),Ie=f.useCallback(()=>{ce(A,o),T()},[ce,A,o,T]);return r.jsxs(r.Fragment,{children:[r.jsx(l.Tooltip,{title:"Export",children:r.jsx(l.IconButton,{color:"primary",onClick:S,children:r.jsx(l.GetAppIcon,{})})}),r.jsxs(l.Dialog,{open:O,onOpenChange:N,maxWidth:"xl",children:[r.jsxs(l.DialogContent,{className:"flex flex-col gap-4 my-4",children:[r.jsx(l.Typography,{variant:"h6",children:"Export data"}),r.jsx("div",{children:"Download the the content of this table as a CSV"}),a>Se&&r.jsx(l.Alert,{color:"warning",children:r.jsxs("div",{children:["This collections has a large number of documents (",a,")."]})}),r.jsxs("div",{className:"flex flex-row gap-4",children:[r.jsxs("div",{className:"p-4 flex flex-col",children:[r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-csv",type:"radio",value:"csv",name:"exportType",checked:d==="csv",onChange:()=>h("csv"),className:l.cn(l.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsx("label",{htmlFor:"radio-csv",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:"CSV"})]}),r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-json",type:"radio",value:"json",name:"exportType",checked:d==="json",onChange:()=>h("json"),className:l.cn(l.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsx("label",{htmlFor:"radio-json",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:"JSON"})]})]}),r.jsxs("div",{className:"p-4 flex flex-col",children:[r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-timestamp",type:"radio",value:"timestamp",name:"dateExportType",checked:g==="timestamp",onChange:()=>x("timestamp"),className:l.cn(l.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsxs("label",{htmlFor:"radio-timestamp",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:["Dates as timestamps (",i.current.getTime(),")"]})]}),r.jsxs("div",{className:"flex items-center",children:[r.jsx("input",{id:"radio-string",type:"radio",value:"string",name:"dateExportType",checked:g==="string",onChange:()=>x("string"),className:l.cn(l.focusedMixin,"w-4 text-blue-600 bg-gray-100 border-gray-300 dark:bg-gray-700 dark:border-gray-600")}),r.jsxs("label",{htmlFor:"radio-string",className:"p-2 text-sm font-medium text-gray-900 dark:text-gray-300",children:["Dates as strings (",i.current.toISOString(),")"]})]})]})]}),r.jsx(l.BooleanSwitchWithLabel,{size:"small",disabled:d!=="csv",value:c,onValueChange:p,label:"Flatten arrays"}),!$&&s]}),r.jsxs(l.DialogActions,{children:[u&&r.jsx(l.CircularProgress,{size:"small"}),r.jsx(l.Button,{onClick:T,variant:"text",children:"Cancel"}),r.jsx(l.Button,{variant:"filled",onClick:Ie,disabled:u||!$,children:"Download"})]})]})]})}function De(e){return f.useMemo(()=>({name:"Import/Export",collections:{CollectionActions:[me,Te],collectionActionsProps:e}}),[e])}m.DataNewPropertiesMapping=R,m.ImportFileUpload=ee,m.ImportNewPropertyFieldPreview=ye,m.ImportSaveInProgress=te,m.convertDataToEntity=Y,m.convertFileToJson=Q,m.flattenEntry=J,m.getInferenceType=Z,m.getPropertiesMapping=K,m.processValueMapping=M,m.unflattenObject=H,m.useImportConfig=G,m.useImportExportPlugin=De,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});
3
3
  //# sourceMappingURL=index.umd.js.map