@donotdev/crud 0.0.15 → 0.0.17
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/CrudService.d.ts.map +1 -1
- package/dist/CrudService.js +2 -2
- package/dist/CrudStore.d.ts.map +1 -1
- package/dist/FieldRegistry.d.ts +3 -30
- package/dist/FieldRegistry.d.ts.map +1 -1
- package/dist/adapters/FunctionsAdapter.d.ts.map +1 -1
- package/dist/adapters/FunctionsAdapter.js +1 -1
- package/dist/components/CrudCard.d.ts.map +1 -1
- package/dist/components/EntityFilters.d.ts +1 -34
- package/dist/components/EntityFilters.d.ts.map +1 -1
- package/dist/components/EntityFilters.js +1 -1
- package/dist/components/__tests__/EntityFilters.test.js +1 -1
- package/dist/components/__tests__/FormFieldRenderer.test.js +1 -1
- package/dist/components/controlled/complex/ControlledFieldArrayField.d.ts +18 -0
- package/dist/components/controlled/complex/ControlledFieldArrayField.d.ts.map +1 -0
- package/dist/components/controlled/complex/ControlledFieldArrayField.js +1 -0
- package/dist/components/controlled/complex/index.d.ts +1 -0
- package/dist/components/controlled/complex/index.d.ts.map +1 -1
- package/dist/components/controlled/complex/index.js +1 -1
- package/dist/components/controlled/types.d.ts +19 -5
- package/dist/components/controlled/types.d.ts.map +1 -1
- package/dist/components/form/fields/DocumentFieldComponent.d.ts.map +1 -1
- package/dist/components/form/fields/DocumentFieldComponent.js +1 -1
- package/dist/components/form/fields/FileFieldComponent.d.ts.map +1 -1
- package/dist/components/form/fields/FileFieldComponent.js +1 -1
- package/dist/components/form/fields/internal/TiptapEditor.d.ts.map +1 -1
- package/dist/components/index.d.ts +2 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +1 -1
- package/dist/forms/hooks/useEntityField.d.ts +15 -24
- package/dist/forms/hooks/useEntityField.d.ts.map +1 -1
- package/dist/forms/hooks/useEntityField.js +1 -1
- package/dist/forms/hooks/useEntityForm.d.ts +3 -2
- package/dist/forms/hooks/useEntityForm.d.ts.map +1 -1
- package/dist/forms/hooks/useEntityForm.js +1 -1
- package/dist/forms/types.d.ts +13 -8
- package/dist/forms/types.d.ts.map +1 -1
- package/dist/forms/utils/buildInitialValues.d.ts.map +1 -1
- package/dist/forms/utils/buildInitialValues.js +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -1
- package/dist/hooks/useFileUpload.d.ts.map +1 -1
- package/dist/hooks/useFileUpload.js +1 -1
- package/dist/hooks/useUnsavedChangesWarning.d.ts +3 -24
- package/dist/hooks/useUnsavedChangesWarning.d.ts.map +1 -1
- package/dist/hooks/useUnsavedChangesWarning.js +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/registerBuiltinFieldTypes.d.ts.map +1 -1
- package/dist/registerBuiltinFieldTypes.js +1 -1
- package/dist/stores/UploadStore.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/useBaseCrudList.d.ts.map +1 -1
- package/dist/useCrud.js +1 -1
- package/dist/useCrudCardList.d.ts +14 -19
- package/dist/useCrudCardList.d.ts.map +1 -1
- package/dist/useCrudCardList.js +1 -1
- package/dist/useCrudList.d.ts +19 -18
- package/dist/useCrudList.d.ts.map +1 -1
- package/dist/useCrudList.js +1 -1
- package/dist/utils/clientListProcessing.d.ts +82 -0
- package/dist/utils/clientListProcessing.d.ts.map +1 -0
- package/dist/utils/clientListProcessing.js +1 -0
- package/dist/utils/fileStorage.d.ts.map +1 -1
- package/dist/utils/imageStorage.d.ts.map +1 -1
- package/dist/utils/imageStorage.js +1 -1
- package/dist/utils/matchesFilter.d.ts +45 -0
- package/dist/utils/matchesFilter.d.ts.map +1 -0
- package/dist/utils/matchesFilter.js +1 -0
- package/dist/utils/mergeWithOptimistic.d.ts.map +1 -1
- package/dist/utils/uploadValidation.d.ts.map +1 -1
- package/dist/utils/uploadValidation.js +1 -1
- package/package.json +5 -8
- package/dist/hooks/useFormNavigationGuard.d.ts +0 -34
- package/dist/hooks/useFormNavigationGuard.d.ts.map +0 -1
- package/dist/hooks/useFormNavigationGuard.js +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CrudService.d.ts","sourceRoot":"","sources":["../src/CrudService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"CrudService.d.ts","sourceRoot":"","sources":["../src/CrudService.ts"],"names":[],"mappings":"AAwDA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EAEX,YAAY,EACZ,oBAAoB,EACpB,cAAc,EACd,eAAe,EAChB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,KAAK,EAGV,YAAY,EACZ,oBAAoB,EACpB,YAAY,EACZ,eAAe,EAEhB,MAAM,SAAS,CAAC;AAUjB,YAAY,EAAE,YAAY,EAAE,CAAC;AAQ7B;;;;;;GAMG;AACH,cAAM,WAAY,YAAW,oBAAoB;IAC/C,OAAO,CAAC,OAAO,CAA6B;IAC5C,OAAO,CAAC,KAAK,CAA6B;IAC1C,oFAAoF;IACpF,OAAO,CAAC,QAAQ,CAAgC;IAChD;;;;OAIG;IACH,OAAO,CAAC,aAAa,CAAuB;IAM5C;;;;;OAKG;IACH,OAAO,CAAC,uBAAuB;IAW/B,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAInC;;;;;;;;;OASG;IACH,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI;IAI5C;;;;;;;;;;OAUG;IACH,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAI3C;;;OAGG;IACH,OAAO,CAAC,aAAa;IAMrB;;;OAGG;IACH,OAAO,CAAC,UAAU;IAelB;;;;;;;;;OASG;IACH,OAAO,CAAC,aAAa;IAMrB,4DAA4D;YAC9C,cAAc;IAK5B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAsBxB;;;;;OAKG;IACG,UAAU,IA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CAAC,CAAC,GACrB,MAAM,IAAI;IAkCb,uFAAuF;IACvF,OAAO,CAAC,0BAA0B;IA2C5B,aAAa,CAAC,CAAC,SAAS;QAAE,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,EAC3C,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,CAAC,EACP,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EACtB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,CAAC,GAAG;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IAsFxB,gBAAgB,CAAC,CAAC,EACtB,UAAU,EAAE,MAAM,EAClB,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAChB,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EACtB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,CAAC,CAAC;IA4FP,gBAAgB,CACpB,UAAU,EAAE,MAAM,EAClB,EAAE,EAAE,MAAM,EACV,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,IAAI,CAAC;CA6EjB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,mBAE1B,CAAC"}
|
package/dist/CrudService.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{toast as m}from"@donotdev/components";import{createSingleton as A,handleError as f,getQueryClient as L,getI18nInstance as c,hasProvider as D,getProvider as T,hasRestrictedVisibility as q
|
|
1
|
+
import{toast as m}from"@donotdev/components";import{createSingleton as A,handleError as f,getQueryClient as L,getI18nInstance as c,hasProvider as D,getProvider as T,hasRestrictedVisibility as q}from"@donotdev/core";import{LIST_SCHEMA_TYPE as _}from"@donotdev/core";import{CRUD_OPERATION as y}from"./types";const S=1/0,Q=1e3*60*30;class F{adapter=null;store=null;security=null;currentUserId=null;_shouldShowSuccessToast(t){return t?.showSuccessToast===!0?!0:t?.showSuccessToast===!1?!1:!this.store?.getState().hideSuccessToasts}setStore(t){this.store=t}setSecurity(t){this.security=t}setCurrentUser(t){this.currentUserId=t}_getPiiFields(t){return t?.security?.piiFields??[]}_auditCrud(t,e,s,i){this.security&&this.security.audit({type:t,collection:e,docId:s,userId:i??this.currentUserId??void 0})}_rateLimitKey(t){return this.currentUserId?`${this.currentUserId}:${t}`:t}async _ensureAdapter(){this.adapter||await this.initialize()}_assertCanAccess(t,e){if(this.adapter&&e&&q(e)&&!this.adapter.serverSideOnly&&!this.adapter.dbLevelSecurity)throw new Error(`[dndev] Direct adapter cannot access "${t}": entity has restricted field visibility. Use a server-side adapter (FunctionsAdapter for Firebase, or SupabaseCrudAdapter with RLS enabled) to enforce field-level security (serverSideOnly = true | dbLevelSecurity = true).`)}async initialize(){if(!this.adapter){if(!D("crud"))throw new Error(`[dndev] No CRUD adapter configured. Call configureProviders({ crud: yourAdapter }) at app startup.
|
|
2
2
|
Examples:
|
|
3
3
|
import { FirestoreAdapter } from "@donotdev/firebase";
|
|
4
4
|
import { FunctionsAdapter } from "@donotdev/crud";
|
|
@@ -6,4 +6,4 @@ Examples:
|
|
|
6
6
|
|
|
7
7
|
configureProviders({ crud: new FirestoreAdapter() }) // Firebase direct
|
|
8
8
|
configureProviders({ crud: new FunctionsAdapter() }) // via callable functions
|
|
9
|
-
configureProviders({ crud: new SupabaseCrudAdapter(sb) }) // Supabase direct (RLS)`);this.adapter=T("crud"),this.store&&this.store.getState().setCrudService(this)}}getQueryClient(){return L()}getListQueryOptions(t,e,s,i,a=_.LIST){const r=this.adapter,u=()=>this._ensureAdapter();return{queryKey:["crud",t,"query",JSON.stringify(e)],queryFn:async()=>{if(r||await u(),!this.adapter)throw new Error("Adapter not initialized");return this.adapter.query(t,e,s,a)},staleTime:i?.staleTime??S}}getDocQueryOptions(t,e,s,i){const a=this.adapter,r=()=>this._ensureAdapter();return{queryKey:["crud",t,"get",e],queryFn:async()=>{if(a||await r(),!this.adapter)throw new Error("Adapter not initialized");return this.adapter.get(t,e,s)},staleTime:i?.staleTime??S}}_updateGetCache(t,e,s,i){const a=this.getQueryClient(),r=["crud",t,"get",e];i===y.DELETE?a.removeQueries({queryKey:r}):i===y.SET&&s?a.setQueryData(r,s):(i===y.UPDATE||i===y.ADD)&&s&&a.setQueryData(r,u=>u?{...u,...s}:s)}_updateListCaches(t,e,s,i){const a=this.getQueryClient();this._updateGetCache(t,e,s,i),a.setQueriesData({queryKey:["crud",t]},r=>{if(r&&typeof r=="object"&&"items"in r){const u=r;let h=u.items;return i===y.DELETE?(h=u.items.filter(d=>d!=null&&typeof d=="object"&&d.id!==e),{...u,items:h,total:Math.max(0,(u.total??h.length)-1)}):i===y.ADD&&s?(h=[...u.items,{...s,id:e}],{...u,items:h,total:(u.total??u.items.length)+1}):(i===y.UPDATE||i===y.SET)&&s&&u.items.some(n=>n!=null&&typeof n=="object"&&n.id===e)?(h=u.items.map(n=>n==null||typeof n!="object"||n.id!==e?n:i===y.SET?{...s,id:e}:{...n,...s}),{...u,items:h}):r}if(Array.isArray(r)){if(i===y.DELETE)return r.filter(u=>u.id!==e);if(i===y.ADD&&s)return[...r,{...s,id:e}];if((i===y.UPDATE||i===y.SET)&&s)return r.some(h=>h!=null&&typeof h=="object"&&h.id===e)?r.map(h=>h==null||typeof h!="object"||h.id!==e?h:i===y.SET?{...s,id:e}:{...h,...s}):r}return r})}_checkUniquenessFromCache(t,e,s){const a=s.metadata?.uniqueKeys;if(!a||a.length===0)return null;const u=this.getQueryClient().getQueriesData({queryKey:["crud",t]}),h=[];for(const[,n]of u)n&&typeof n=="object"&&"items"in n?h.push(...n.items):Array.isArray(n)&&h.push(...n);if(h.length===0)return null;const d=e;for(const n of a){if(!n.fields.every(p=>d[p]!=null&&d[p]!==""))continue;const g=h.find(p=>n.fields.every(w=>{const C=typeof d[w]=="string"?d[w].toLowerCase():d[w],E=typeof p[w]=="string"?p[w].toLowerCase():p[w];return C===E}));if(g){if(n.findOrCreate)return g.id;const p=n.fields.join(" + ");throw f(new Error(n.errorMessage||`Duplicate ${p}`),{userMessage:n.errorMessage||`A record with this ${p} already exists`,showNotification:!0})}}return null}_getItemFromListCache(t,e){const i=this.getQueryClient().getQueriesData({queryKey:["crud",t]});for(const[,a]of i)if(a?.items){const r=a.items.find(u=>u.id===e);if(r)return r}return null}async invalidateCollection(t){await this.getQueryClient().invalidateQueries({queryKey:["crud",t]})}async get(t,e,s,i){if(await this._ensureAdapter(),i?.noCache)return this._getFromAdapter(t,e,s);this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{return await this.getQueryClient().fetchQuery({queryKey:["crud",t,"get",e],queryFn:()=>this._getFromAdapter(t,e,s,!1),staleTime:i?.staleTime??S})}catch(a){const r=f(a,{userMessage:`Failed to fetch ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,r),r}finally{this.store&&this.store.getState().setLoading(t,!1)}}async _getFromAdapter(t,e,s,i=!0){this.store&&i&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{if(!this.adapter)throw new Error("Adapter not initialized");return this._assertCanAccess(t,s),await this.adapter.get(t,e,s)}catch(a){const r=f(a,{userMessage:`Failed to fetch ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,r),r}finally{this.store&&i&&this.store.getState().setLoading(t,!1)}}async query(t,e,s,i,a=_.LIST){if(await this._ensureAdapter(),i?.noCache)return this._queryFromAdapter(t,e,s,!0,a);this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{return await this.getQueryClient().fetchQuery({queryKey:["crud",t,"query",JSON.stringify(e)],queryFn:()=>this._queryFromAdapter(t,e,s,!1,a),staleTime:i?.staleTime??S})}catch(r){const u=f(r,{userMessage:`Failed to query ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,u),u}finally{this.store&&this.store.getState().setLoading(t,!1)}}async _queryFromAdapter(t,e,s,i=!0,a=_.LIST){this.store&&i&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{if(!this.adapter)throw new Error("Adapter not initialized");return this._assertCanAccess(t,s),await this.adapter.query(t,e,s,a)}catch(r){const u=f(r,{userMessage:`Failed to query ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,u),u}finally{this.store&&i&&this.store.getState().setLoading(t,!1)}}_getEntityName(t){const e=c();let s=`entity-${t}`,i=e.t("name",{ns:s,defaultValue:null});return i&&i!=="name"&&i!==`${s}:name`||t.endsWith("s")&&t.length>1&&(s=`entity-${t.slice(0,-1)}`,i=e.t("name",{ns:s,defaultValue:null}),i&&i!=="name"&&i!==`${s}:name`)?i:t}async set(t,e,s,i,a){await this._ensureAdapter(),this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const u=this.getQueryClient().getQueryData(["crud",t,"get",e])??null;this.store&&this.store.getState().updateOptimistic(t,e,s,u);try{if(!this.adapter)throw new Error("Adapter not initialized");this._assertCanAccess(t,i),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const h=this._getPiiFields(i),d=h.length>0&&this.security?this.security.encryptPii(s,h):s;if(await this.adapter.set(t,e,d,i),this._updateListCaches(t,e,s,y.SET),this.store&&this.store.getState().confirmUpdate(t,e),this._auditCrud("crud.update",t,e),this._shouldShowSuccessToast(a)){const n=c(),o=this._getEntityName(t);m("success",n.t("messages.updateSuccess",{ns:"crud",entity:o}))}}catch(h){u&&this._updateListCaches(t,e,u,y.SET),this.store&&this.store.getState().rejectUpdate(t,e);const d=f(h,{userMessage:`Failed to save ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,d),d}finally{this.store&&this.store.getState().setLoading(t,!1)}}async update(t,e,s,i,a){await this._ensureAdapter(),this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const u=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e),h=u?{...u,...s}:{...s,id:e};this.store&&this.store.getState().updateOptimistic(t,e,h,u??null);try{if(!this.adapter)throw new Error("Adapter not initialized");i&&(this._assertCanAccess(t,i),F(i,s,`CrudService.update(${t})`)),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const d=i?this._getPiiFields(i):[],n=d.length>0&&this.security?this.security.encryptPii(s,d):s;if(await this.adapter.update(t,e,n),this._updateListCaches(t,e,h,y.UPDATE),this.store&&this.store.getState().confirmUpdate(t,e),this._auditCrud("crud.update",t,e),this._shouldShowSuccessToast(a)){const o=c(),g=this._getEntityName(t);m("success",o.t("messages.updateSuccess",{ns:"crud",entity:g}))}}catch(d){u?this._updateListCaches(t,e,u,y.UPDATE):this.invalidateCollection(t),this.store&&this.store.getState().rejectUpdate(t,e);const n=f(d,{userMessage:`Failed to update ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,n),n}finally{this.store&&this.store.getState().setLoading(t,!1)}}async delete(t,e,s){await this._ensureAdapter(),this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const a=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e);this.store&&a&&this.store.getState().deleteOptimistic(t,e,a);try{if(!this.adapter)throw new Error("Adapter not initialized");if(this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write"),await this.adapter.delete(t,e),this._updateListCaches(t,e,null,y.DELETE),this.store&&this.store.getState().confirmDelete(t,e),this._auditCrud("crud.delete",t,e),this.security?.recordAnomaly?.("bulk.deletes",this.currentUserId??void 0),this._shouldShowSuccessToast(s)){const r=c(),u=this._getEntityName(t);m("success",r.t("messages.deleteSuccess",{ns:"crud",entity:u}))}}catch(r){a&&this._updateListCaches(t,e,a,y.ADD),this.store&&this.store.getState().rejectDelete(t,e);const u=f(r,{userMessage:`Failed to delete ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,u),u}finally{this.store&&this.store.getState().setLoading(t,!1)}}async add(t,e,s,i){await this._ensureAdapter();const a=this._checkUniquenessFromCache(t,e,s);if(a)return a;this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const r=`temp_${crypto.randomUUID()}`;this.store&&this.store.getState().addOptimistic(t,r,{...e,id:r});try{if(!this.adapter)throw new Error("Adapter not initialized");this._assertCanAccess(t,s),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const u=this._getPiiFields(s),h=u.length>0&&this.security?this.security.encryptPii(e,u):e,d=await this.adapter.add(t,h,s),n=d.id,o=d.data;if(this._updateListCaches(t,r,null,y.DELETE),this._updateListCaches(t,n,o,y.ADD),this.store&&this.store.getState().confirmOptimistic(t,r,n,o),this._auditCrud("crud.create",t,n),this._shouldShowSuccessToast(i)){const g=c(),p=this._getEntityName(t);m("success",g.t("messages.createSuccess",{ns:"crud",entity:p}))}return n}catch(u){this._updateListCaches(t,r,null,y.DELETE),this.store&&this.store.getState().rejectOptimistic(t,r);const h=f(u,{userMessage:`Failed to create ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,h),h}finally{this.store&&this.store.getState().setLoading(t,!1)}}subscribe(t,e,s,i){if(!this.adapter){let a=!1,r=null;return this._ensureAdapter().then(()=>{a||(r=this._subscribeSync(t,e,s,i))}).catch(u=>{a||s(null,f(u))}),()=>{a=!0,r?.()}}return this._subscribeSync(t,e,s,i)}_subscribeSync(t,e,s,i){try{const a=this.adapter;return a?a.subscribe?(this._assertCanAccess(t,i),a.subscribe(t,e,(r,u)=>{r&&this.getQueryClient().setQueryData(["crud",t,"get",e],r),s(r,u)},i)):()=>{}:(s(null,new Error("Adapter not initialized")),()=>{})}catch(a){return s(null,f(a)),()=>{}}}subscribeToCollection(t,e,s,i){if(!this.adapter){let a=!1,r=null;return this._ensureAdapter().then(()=>{a||(r=this._subscribeToCollectionSync(t,e,s,i))}).catch(u=>{a||s([],f(u))}),()=>{a=!0,r?.()}}return this._subscribeToCollectionSync(t,e,s,i)}_subscribeToCollectionSync(t,e,s,i){try{const a=this.adapter;return a?a.subscribeToCollection?(this._assertCanAccess(t,i),a.subscribeToCollection(t,e,(r,u)=>{r&&this.getQueryClient().setQueryData(["crud",t,"query",JSON.stringify(e)],h=>h?{...h,items:r}:{items:r}),s(r,u)},i)):()=>{}:(s([],new Error("Adapter not initialized")),()=>{})}catch(a){return s([],f(a)),()=>{}}}async addOptimistic(t,e,s,i){await this._ensureAdapter();const a=this._checkUniquenessFromCache(t,e,s);if(a)return{...e,id:a};this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const r=`temp_${crypto.randomUUID()}`,u={...e,id:r,_optimistic:!0};this.store&&this.store.getState().addOptimistic(t,r,u);try{if(!this.adapter)throw new Error("Adapter not initialized");const h=await this.adapter.add(t,e,s),d=h.id,n=h.data;if(this._updateListCaches(t,r,null,y.DELETE),this._updateListCaches(t,d,n,y.ADD),this.store&&this.store.getState().confirmOptimistic(t,r,d,n),this._auditCrud("crud.create",t,d),this._shouldShowSuccessToast(i)){const o=c(),g=this._getEntityName(t);m("success",o.t("messages.createSuccess",{ns:"crud",entity:g}))}return n}catch(h){throw this._updateListCaches(t,r,null,y.DELETE),this.store&&this.store.getState().rejectOptimistic(t,r),f(h,{userMessage:`Failed to create ${t}`,showNotification:!0})}}async updateOptimistic(t,e,s,i,a){await this._ensureAdapter(),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const u=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e),h=u?{...u,...s,_optimistic:!0}:{...s,id:e,_optimistic:!0};this.store&&u&&this.store.getState().updateOptimistic(t,e,h,u);try{if(!this.adapter)throw new Error("Adapter not initialized");await this.adapter.update(t,e,s);const{_optimistic:d,...n}=h;if(this._updateListCaches(t,e,n,y.UPDATE),this.store&&this.store.getState().confirmUpdate(t,e),this._auditCrud("crud.update",t,e),this._shouldShowSuccessToast(a)){const o=c(),g=this._getEntityName(t);m("success",o.t("messages.updateSuccess",{ns:"crud",entity:g}))}return n}catch(d){throw u&&this._updateListCaches(t,e,u,y.UPDATE),this.store&&this.store.getState().rejectUpdate(t,e),f(d,{userMessage:`Failed to update ${t}`,showNotification:!0})}}async deleteOptimistic(t,e,s){await this._ensureAdapter(),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const a=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e);this.store&&a&&this.store.getState().deleteOptimistic(t,e,a);try{if(!this.adapter)throw new Error("Adapter not initialized");if(await this.adapter.delete(t,e),this._updateListCaches(t,e,null,y.DELETE),this.store&&this.store.getState().confirmDelete(t,e),this._auditCrud("crud.delete",t,e),this.security?.recordAnomaly?.("bulk.deletes",this.currentUserId??void 0),this._shouldShowSuccessToast(s)){const r=c(),u=this._getEntityName(t);m("success",r.t("messages.deleteSuccess",{ns:"crud",entity:u}))}}catch(r){throw a&&this._updateListCaches(t,e,a,y.ADD),this.store&&this.store.getState().rejectDelete(t,e),f(r,{userMessage:`Failed to delete ${t}`,showNotification:!0})}}}const O=A(()=>new b);export{O as getCrudService};
|
|
9
|
+
configureProviders({ crud: new SupabaseCrudAdapter(sb) }) // Supabase direct (RLS)`);this.adapter=T("crud"),this.store&&this.store.getState().setCrudService(this)}}getQueryClient(){return L()}getListQueryOptions(t,e,s,i,a=_.LIST){const r=this.adapter,u=()=>this._ensureAdapter();return{queryKey:["crud",t,"query",JSON.stringify(e)],queryFn:async()=>{if(r||await u(),!this.adapter)throw new Error("Adapter not initialized");return this.adapter.query(t,e,s,a)},staleTime:i?.staleTime??S}}getDocQueryOptions(t,e,s,i){const a=this.adapter,r=()=>this._ensureAdapter();return{queryKey:["crud",t,"get",e],queryFn:async()=>{if(a||await r(),!this.adapter)throw new Error("Adapter not initialized");return this.adapter.get(t,e,s)},staleTime:i?.staleTime??S}}_updateGetCache(t,e,s,i){const a=this.getQueryClient(),r=["crud",t,"get",e];i===y.DELETE?a.removeQueries({queryKey:r}):i===y.SET&&s?a.setQueryData(r,s):(i===y.UPDATE||i===y.ADD)&&s&&a.setQueryData(r,u=>u?{...u,...s}:s)}_updateListCaches(t,e,s,i){const a=this.getQueryClient();this._updateGetCache(t,e,s,i),a.setQueriesData({queryKey:["crud",t]},r=>{if(r&&typeof r=="object"&&"items"in r){const u=r;let h=u.items;return i===y.DELETE?(h=u.items.filter(d=>d!=null&&typeof d=="object"&&d.id!==e),{...u,items:h,total:Math.max(0,(u.total??h.length)-1)}):i===y.ADD&&s?(h=[...u.items,{...s,id:e}],{...u,items:h,total:(u.total??u.items.length)+1}):(i===y.UPDATE||i===y.SET)&&s&&u.items.some(n=>n!=null&&typeof n=="object"&&n.id===e)?(h=u.items.map(n=>n==null||typeof n!="object"||n.id!==e?n:i===y.SET?{...s,id:e}:{...n,...s}),{...u,items:h}):r}if(Array.isArray(r)){if(i===y.DELETE)return r.filter(u=>u.id!==e);if(i===y.ADD&&s)return[...r,{...s,id:e}];if((i===y.UPDATE||i===y.SET)&&s)return r.some(h=>h!=null&&typeof h=="object"&&h.id===e)?r.map(h=>h==null||typeof h!="object"||h.id!==e?h:i===y.SET?{...s,id:e}:{...h,...s}):r}return r})}_checkUniquenessFromCache(t,e,s){const a=s.metadata?.uniqueKeys;if(!a||a.length===0)return null;const u=this.getQueryClient().getQueriesData({queryKey:["crud",t]}),h=[];for(const[,n]of u)n&&typeof n=="object"&&"items"in n?h.push(...n.items):Array.isArray(n)&&h.push(...n);if(h.length===0)return null;const d=e;for(const n of a){if(!n.fields.every(p=>d[p]!=null&&d[p]!==""))continue;const g=h.find(p=>n.fields.every(w=>{const C=typeof d[w]=="string"?d[w].toLowerCase():d[w],E=typeof p[w]=="string"?p[w].toLowerCase():p[w];return C===E}));if(g){if(n.findOrCreate)return g.id;const p=n.fields.join(" + ");throw f(new Error(n.errorMessage||`Duplicate ${p}`),{userMessage:n.errorMessage||`A record with this ${p} already exists`,showNotification:!0})}}return null}_getItemFromListCache(t,e){const i=this.getQueryClient().getQueriesData({queryKey:["crud",t]});for(const[,a]of i)if(a?.items){const r=a.items.find(u=>u.id===e);if(r)return r}return null}async invalidateCollection(t){await this.getQueryClient().invalidateQueries({queryKey:["crud",t]})}async get(t,e,s,i){if(await this._ensureAdapter(),i?.noCache)return this._getFromAdapter(t,e,s);this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{return await this.getQueryClient().fetchQuery({queryKey:["crud",t,"get",e],queryFn:()=>this._getFromAdapter(t,e,s,!1),staleTime:i?.staleTime??S})}catch(a){const r=f(a,{userMessage:`Failed to fetch ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,r),r}finally{this.store&&this.store.getState().setLoading(t,!1)}}async _getFromAdapter(t,e,s,i=!0){this.store&&i&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{if(!this.adapter)throw new Error("Adapter not initialized");return this._assertCanAccess(t,s),await this.adapter.get(t,e,s)}catch(a){const r=f(a,{userMessage:`Failed to fetch ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,r),r}finally{this.store&&i&&this.store.getState().setLoading(t,!1)}}async query(t,e,s,i,a=_.LIST){if(await this._ensureAdapter(),i?.noCache)return this._queryFromAdapter(t,e,s,!0,a);this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{return await this.getQueryClient().fetchQuery({queryKey:["crud",t,"query",JSON.stringify(e)],queryFn:()=>this._queryFromAdapter(t,e,s,!1,a),staleTime:i?.staleTime??S})}catch(r){const u=f(r,{userMessage:`Failed to query ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,u),u}finally{this.store&&this.store.getState().setLoading(t,!1)}}async _queryFromAdapter(t,e,s,i=!0,a=_.LIST){this.store&&i&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));try{if(!this.adapter)throw new Error("Adapter not initialized");return this._assertCanAccess(t,s),await this.adapter.query(t,e,s,a)}catch(r){const u=f(r,{userMessage:`Failed to query ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,u),u}finally{this.store&&i&&this.store.getState().setLoading(t,!1)}}_getEntityName(t){const e=c();let s=`entity-${t}`,i=e.t("name",{ns:s,defaultValue:null});return i&&i!=="name"&&i!==`${s}:name`||t.endsWith("s")&&t.length>1&&(s=`entity-${t.slice(0,-1)}`,i=e.t("name",{ns:s,defaultValue:null}),i&&i!=="name"&&i!==`${s}:name`)?i:t}async set(t,e,s,i,a){await this._ensureAdapter(),this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const u=this.getQueryClient().getQueryData(["crud",t,"get",e])??null;this.store&&this.store.getState().updateOptimistic(t,e,s,u);try{if(!this.adapter)throw new Error("Adapter not initialized");this._assertCanAccess(t,i),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const h=this._getPiiFields(i),d=h.length>0&&this.security?this.security.encryptPii(s,h):s;if(await this.adapter.set(t,e,d,i),this._updateListCaches(t,e,s,y.SET),this.store&&this.store.getState().confirmUpdate(t,e),this._auditCrud("crud.update",t,e),this._shouldShowSuccessToast(a)){const n=c(),o=this._getEntityName(t);m("success",n.t("messages.updateSuccess",{ns:"crud",entity:o}))}}catch(h){u&&this._updateListCaches(t,e,u,y.SET),this.store&&this.store.getState().rejectUpdate(t,e);const d=f(h,{userMessage:`Failed to save ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,d),d}finally{this.store&&this.store.getState().setLoading(t,!1)}}async update(t,e,s,i,a){await this._ensureAdapter(),this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const u=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e),h=u?{...u,...s}:{...s,id:e};this.store&&this.store.getState().updateOptimistic(t,e,h,u??null);try{if(!this.adapter)throw new Error("Adapter not initialized");i&&this._assertCanAccess(t,i),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const d=i?this._getPiiFields(i):[],n=d.length>0&&this.security?this.security.encryptPii(s,d):s;if(await this.adapter.update(t,e,n),this._updateListCaches(t,e,h,y.UPDATE),this.store&&this.store.getState().confirmUpdate(t,e),this._auditCrud("crud.update",t,e),this._shouldShowSuccessToast(a)){const o=c(),g=this._getEntityName(t);m("success",o.t("messages.updateSuccess",{ns:"crud",entity:g}))}}catch(d){u?this._updateListCaches(t,e,u,y.UPDATE):this.invalidateCollection(t),this.store&&this.store.getState().rejectUpdate(t,e);const n=f(d,{userMessage:`Failed to update ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,n),n}finally{this.store&&this.store.getState().setLoading(t,!1)}}async delete(t,e,s){await this._ensureAdapter(),this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const a=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e);this.store&&a&&this.store.getState().deleteOptimistic(t,e,a);try{if(!this.adapter)throw new Error("Adapter not initialized");if(this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write"),await this.adapter.delete(t,e),this._updateListCaches(t,e,null,y.DELETE),this.store&&this.store.getState().confirmDelete(t,e),this._auditCrud("crud.delete",t,e),this.security?.recordAnomaly?.("bulk.deletes",this.currentUserId??void 0),this._shouldShowSuccessToast(s)){const r=c(),u=this._getEntityName(t);m("success",r.t("messages.deleteSuccess",{ns:"crud",entity:u}))}}catch(r){a&&this._updateListCaches(t,e,a,y.ADD),this.store&&this.store.getState().rejectDelete(t,e);const u=f(r,{userMessage:`Failed to delete ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,u),u}finally{this.store&&this.store.getState().setLoading(t,!1)}}async add(t,e,s,i){await this._ensureAdapter();const a=this._checkUniquenessFromCache(t,e,s);if(a)return a;this.store&&(this.store.getState().setLoading(t,!0),this.store.getState().setError(t,null));const r=`temp_${crypto.randomUUID()}`;this.store&&this.store.getState().addOptimistic(t,r,{...e,id:r});try{if(!this.adapter)throw new Error("Adapter not initialized");this._assertCanAccess(t,s),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const u=this._getPiiFields(s),h=u.length>0&&this.security?this.security.encryptPii(e,u):e,d=await this.adapter.add(t,h,s),n=d.id,o=d.data;if(this._updateListCaches(t,r,null,y.DELETE),this._updateListCaches(t,n,o,y.ADD),this.store&&this.store.getState().confirmOptimistic(t,r,n,o),this._auditCrud("crud.create",t,n),this._shouldShowSuccessToast(i)){const g=c(),p=this._getEntityName(t);m("success",g.t("messages.createSuccess",{ns:"crud",entity:p}))}return n}catch(u){this._updateListCaches(t,r,null,y.DELETE),this.store&&this.store.getState().rejectOptimistic(t,r);const h=f(u,{userMessage:`Failed to create ${t}`,showNotification:!0});throw this.store&&this.store.getState().setError(t,h),h}finally{this.store&&this.store.getState().setLoading(t,!1)}}subscribe(t,e,s,i){if(!this.adapter){let a=!1,r=null;return this._ensureAdapter().then(()=>{a||(r=this._subscribeSync(t,e,s,i))}).catch(u=>{a||s(null,f(u))}),()=>{a=!0,r?.()}}return this._subscribeSync(t,e,s,i)}_subscribeSync(t,e,s,i){try{const a=this.adapter;return a?a.subscribe?(this._assertCanAccess(t,i),a.subscribe(t,e,(r,u)=>{r&&this.getQueryClient().setQueryData(["crud",t,"get",e],r),s(r,u)},i)):()=>{}:(s(null,new Error("Adapter not initialized")),()=>{})}catch(a){return s(null,f(a)),()=>{}}}subscribeToCollection(t,e,s,i){if(!this.adapter){let a=!1,r=null;return this._ensureAdapter().then(()=>{a||(r=this._subscribeToCollectionSync(t,e,s,i))}).catch(u=>{a||s([],f(u))}),()=>{a=!0,r?.()}}return this._subscribeToCollectionSync(t,e,s,i)}_subscribeToCollectionSync(t,e,s,i){try{const a=this.adapter;return a?a.subscribeToCollection?(this._assertCanAccess(t,i),a.subscribeToCollection(t,e,(r,u)=>{r&&this.getQueryClient().setQueryData(["crud",t,"query",JSON.stringify(e)],h=>h?{...h,items:r}:{items:r}),s(r,u)},i)):()=>{}:(s([],new Error("Adapter not initialized")),()=>{})}catch(a){return s([],f(a)),()=>{}}}async addOptimistic(t,e,s,i){await this._ensureAdapter();const a=this._checkUniquenessFromCache(t,e,s);if(a)return{...e,id:a};this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const r=`temp_${crypto.randomUUID()}`,u={...e,id:r,_optimistic:!0};this.store&&this.store.getState().addOptimistic(t,r,u);try{if(!this.adapter)throw new Error("Adapter not initialized");const h=await this.adapter.add(t,e,s),d=h.id,n=h.data;if(this._updateListCaches(t,r,null,y.DELETE),this._updateListCaches(t,d,n,y.ADD),this.store&&this.store.getState().confirmOptimistic(t,r,d,n),this._auditCrud("crud.create",t,d),this._shouldShowSuccessToast(i)){const o=c(),g=this._getEntityName(t);m("success",o.t("messages.createSuccess",{ns:"crud",entity:g}))}return n}catch(h){throw this._updateListCaches(t,r,null,y.DELETE),this.store&&this.store.getState().rejectOptimistic(t,r),f(h,{userMessage:`Failed to create ${t}`,showNotification:!0})}}async updateOptimistic(t,e,s,i,a){await this._ensureAdapter(),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const u=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e),h=u?{...u,...s,_optimistic:!0}:{...s,id:e,_optimistic:!0};this.store&&u&&this.store.getState().updateOptimistic(t,e,h,u);try{if(!this.adapter)throw new Error("Adapter not initialized");await this.adapter.update(t,e,s);const{_optimistic:d,...n}=h;if(this._updateListCaches(t,e,n,y.UPDATE),this.store&&this.store.getState().confirmUpdate(t,e),this._auditCrud("crud.update",t,e),this._shouldShowSuccessToast(a)){const o=c(),g=this._getEntityName(t);m("success",o.t("messages.updateSuccess",{ns:"crud",entity:g}))}return n}catch(d){throw u&&this._updateListCaches(t,e,u,y.UPDATE),this.store&&this.store.getState().rejectUpdate(t,e),f(d,{userMessage:`Failed to update ${t}`,showNotification:!0})}}async deleteOptimistic(t,e,s){await this._ensureAdapter(),this.security&&await this.security.checkRateLimit(this._rateLimitKey(t),"write");const a=this.getQueryClient().getQueryData(["crud",t,"get",e])??this._getItemFromListCache(t,e);this.store&&a&&this.store.getState().deleteOptimistic(t,e,a);try{if(!this.adapter)throw new Error("Adapter not initialized");if(await this.adapter.delete(t,e),this._updateListCaches(t,e,null,y.DELETE),this.store&&this.store.getState().confirmDelete(t,e),this._auditCrud("crud.delete",t,e),this.security?.recordAnomaly?.("bulk.deletes",this.currentUserId??void 0),this._shouldShowSuccessToast(s)){const r=c(),u=this._getEntityName(t);m("success",r.t("messages.deleteSuccess",{ns:"crud",entity:u}))}}catch(r){throw a&&this._updateListCaches(t,e,a,y.ADD),this.store&&this.store.getState().rejectDelete(t,e),f(r,{userMessage:`Failed to delete ${t}`,showNotification:!0})}}}const O=A(()=>new F);export{O as getCrudService};
|
package/dist/CrudStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CrudStore.d.ts","sourceRoot":"","sources":["../src/CrudStore.ts"],"names":[],"mappings":"AAuBA,OAAO,KAAK,EAEV,SAAS,EACT,WAAW,EAIZ,MAAM,SAAS,CAAC;AA0BjB;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,
|
|
1
|
+
{"version":3,"file":"CrudStore.d.ts","sourceRoot":"","sources":["../src/CrudStore.ts"],"names":[],"mappings":"AAuBA,OAAO,KAAK,EAEV,SAAS,EACT,WAAW,EAIZ,MAAM,SAAS,CAAC;AA0BjB;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,iIAuZvB,CAAC"}
|
package/dist/FieldRegistry.d.ts
CHANGED
|
@@ -8,35 +8,9 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import type { EntityField, FieldType, ValueTypeForField } from '@donotdev/core';
|
|
10
10
|
import type { ComponentType, ReactElement } from 'react';
|
|
11
|
-
import type {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
* Use framework's `useController` hook (from @donotdev/crud) instead of react-hook-form's useController
|
|
16
|
-
* to ensure type compatibility.
|
|
17
|
-
*/
|
|
18
|
-
export interface ControlledFieldProps<T extends FieldValues = FieldValues, FT extends FieldType = FieldType> {
|
|
19
|
-
/** Control object from react-hook-form - use with framework's useController hook */
|
|
20
|
-
control: Control<T>;
|
|
21
|
-
errors: FieldErrors<T>;
|
|
22
|
-
fieldConfig: EntityField<FT>;
|
|
23
|
-
t: (key: string, options?: Record<string, unknown>) => string;
|
|
24
|
-
onChange?: (value: ValueTypeForField<FT>) => void;
|
|
25
|
-
placeholder?: string;
|
|
26
|
-
/** Field object from react-hook-form (provided by framework, no need to use useController) */
|
|
27
|
-
field?: ControllerRenderProps<T, Path<T>>;
|
|
28
|
-
/** Field state from react-hook-form (provided by framework, no need to use useController) */
|
|
29
|
-
fieldState?: {
|
|
30
|
-
error?: {
|
|
31
|
-
message?: string;
|
|
32
|
-
};
|
|
33
|
-
isDirty: boolean;
|
|
34
|
-
isTouched: boolean;
|
|
35
|
-
invalid: boolean;
|
|
36
|
-
};
|
|
37
|
-
/** Form ID for upload tracking - used by file upload fields */
|
|
38
|
-
formId?: string;
|
|
39
|
-
}
|
|
11
|
+
import type { FieldValues } from 'react-hook-form';
|
|
12
|
+
import type { ControlledFieldProps } from './components/controlled/types';
|
|
13
|
+
export type { ControlledFieldProps };
|
|
40
14
|
/**
|
|
41
15
|
* Props for uncontrolled field components
|
|
42
16
|
*/
|
|
@@ -185,5 +159,4 @@ export declare function registerFieldType<FT extends string>(registration: Field
|
|
|
185
159
|
* Check if a field type is registered
|
|
186
160
|
*/
|
|
187
161
|
export declare function isFieldTypeRegistered(type: string): boolean;
|
|
188
|
-
export {};
|
|
189
162
|
//# sourceMappingURL=FieldRegistry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FieldRegistry.d.ts","sourceRoot":"","sources":["../src/FieldRegistry.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEhF,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"FieldRegistry.d.ts","sourceRoot":"","sources":["../src/FieldRegistry.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEhF,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AM,GAAG,OAAO;IAI1B;;OAEG;IACH,kBAAkB,IAAI,MAAM,EAAE;IAI9B;;OAEG;IACH,KAAK,IAAI,IAAI;CAKd;AAKD;;;;;GAKG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAKhD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,SAAS,MAAM,EACjD,YAAY,EAAE,qBAAqB,CAAC,EAAE,CAAC,GACtC,IAAI,CAEN;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FunctionsAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/FunctionsAdapter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FunctionsAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/FunctionsAdapter.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EACV,WAAW,EAEX,YAAY,EACZ,cAAc,EACd,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAGxB;;;;;;GAMG;AACH,MAAM,WAAW,qBAAqB;IACpC,KAAK,CAAC,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IACnE,OAAO,CAAC,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,KAAK,GAAG,MAAM,CAAA;KAAE,CAAC,CAAC;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yFAAyF;IACzF,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAgBD,qBAAa,gBAAiB,YAAW,YAAY;IACnD,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rapCrudError as o,validateWithSchema as c}from"@donotdev/core";import{LIST_SCHEMA_TYPE as u}from"@donotdev/core";let l=null;class b{serverSideOnly=!0;async getCallable(){if(p("callable"))return f("callable");if(!l){const{FirebaseCallableProvider:t}=await import("@donotdev/firebase");l=new t}return l}async getCallableFn(t){const a=await this.getCallable();return async n=>({data:await a.call(t,n)})}async get(t,a,n){const i=`get_${t}`,d=await this.getCallableFn(i);try{const s=(await d({id:a})).data;return n?c(n,s,"FunctionsAdapter.get"):s}catch(r){throw o(r,"FunctionsAdapter","get",t,a)}}async set(t,a,n,i){const d=c(i,n,"FunctionsAdapter.set"),r=`update_${t}`,s=await this.getCallableFn(r);try{await s({id:a,payload:d})}catch(e){throw o(e,"FunctionsAdapter","set",t,a)}}async update(t,a,n){const i=`update_${t}`,d=await this.getCallableFn(i);try{await d({id:a,payload:n})}catch(r){throw o(r,"FunctionsAdapter","update",t,a)}}async delete(t,a){const n=`delete_${t}`,i=await this.getCallableFn(n);try{await i({id:a})}catch(d){throw o(d,"FunctionsAdapter","delete",t,a)}}async add(t,a,n){const i=c(n,a,"FunctionsAdapter.add"),d=`create_${t}`,r=await this.getCallableFn(d);try{const s=await r({payload:i});if(!s.data)throw o(new Error("Create function returned no data"),"FunctionsAdapter","add",t);return{id:s.data.id,data:s.data}}catch(s){throw o(s,"FunctionsAdapter","add",t)}}async query(t,a,n,i=u.LIST){const d=i===u.LIST_CARD?`listCard_${t}`:`list_${t}`,r={};a.where&&(r.where=a.where.map(e=>[e.field,e.operator,e.value])),a.orderBy&&(r.orderBy=a.orderBy.map(e=>[e.field,e.direction||"asc"])),a.limit&&(r.limit=a.limit),a.startAfter&&(r.startAfterId=a.startAfter);const s=await this.getCallableFn(d);try{const e=await s(r);if(!e.data)throw o(new Error("Query function returned no data"),"FunctionsAdapter","query",t);return{items:e.data.items,total:e.data.count,hasMore:e.data.hasMore,lastVisible:e.data.lastVisible}}catch(e){throw o(e,"FunctionsAdapter","query",t)}}subscribe(){return()=>{}}subscribeToCollection(){return()=>{}}}export{b as FunctionsAdapter};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CrudCard.d.ts","sourceRoot":"","sources":["../../src/components/CrudCard.tsx"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,aAAa,EAA2B,MAAM,gBAAgB,CAAC;AA4C7E;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,EACvB,IAAI,EACJ,MAAM,EACN,OAAO,EACP,WAAW,EAAE,eAAe,EAC5B,cAAc,EAAE,kBAAkB,EAClC,aAAa,EAAE,iBAAiB,EAChC,YAAY,EAAE,gBAAgB,EAC9B,UAAkB,EAClB,aAAa,EACb,QAAe,EACf,SAAS,GACV,EAAE,IAAI,CAAC,aAAa,EAAE,YAAY,CAAC,
|
|
1
|
+
{"version":3,"file":"CrudCard.d.ts","sourceRoot":"","sources":["../../src/components/CrudCard.tsx"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,aAAa,EAA2B,MAAM,gBAAgB,CAAC;AA4C7E;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,EACvB,IAAI,EACJ,MAAM,EACN,OAAO,EACP,WAAW,EAAE,eAAe,EAC5B,cAAc,EAAE,kBAAkB,EAClC,aAAa,EAAE,iBAAiB,EAChC,YAAY,EAAE,gBAAgB,EAC9B,UAAkB,EAClB,aAAa,EACb,QAAe,EACf,SAAS,GACV,EAAE,IAAI,CAAC,aAAa,EAAE,YAAY,CAAC,2CAqMnC;AAED,eAAe,QAAQ,CAAC"}
|
|
@@ -1,38 +1,5 @@
|
|
|
1
1
|
import type { Entity } from '@donotdev/core';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Shared filter utility - checks if an item matches a filter value
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
* Pattern for null/undefined handling:
|
|
9
|
-
|
|
10
|
-
* - MIN / undefined → Only show items with defined values >= MIN (exclude null/undefined)
|
|
11
|
-
|
|
12
|
-
* - undefined / MAX → Show items with value <= MAX OR null/undefined (include null/undefined)
|
|
13
|
-
|
|
14
|
-
* - MIN / MAX → Show items with MIN <= value <= MAX (exclude null/undefined)
|
|
15
|
-
|
|
16
|
-
*/
|
|
17
|
-
export declare function matchesFilter(itemValue: any, filterValue: string | {
|
|
18
|
-
min?: string;
|
|
19
|
-
max?: string;
|
|
20
|
-
} | string[] | {
|
|
21
|
-
date: string;
|
|
22
|
-
time?: string;
|
|
23
|
-
} | {
|
|
24
|
-
min?: {
|
|
25
|
-
date: string;
|
|
26
|
-
time?: string;
|
|
27
|
-
};
|
|
28
|
-
max?: {
|
|
29
|
-
date: string;
|
|
30
|
-
time?: string;
|
|
31
|
-
};
|
|
32
|
-
} | {
|
|
33
|
-
date: string;
|
|
34
|
-
time?: string;
|
|
35
|
-
}[], fieldType: string): boolean;
|
|
2
|
+
export { matchesFilter } from '../utils/matchesFilter';
|
|
36
3
|
export interface EntityFiltersProps<T extends Record<string, any> & {
|
|
37
4
|
id: string;
|
|
38
5
|
} = Record<string, any> & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityFilters.d.ts","sourceRoot":"","sources":["../../src/components/EntityFilters.tsx"],"names":[],"mappings":"AAyDA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"EntityFilters.d.ts","sourceRoot":"","sources":["../../src/components/EntityFilters.tsx"],"names":[],"mappings":"AAyDA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAW7C,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEvD,MAAM,WAAW,kBAAkB,CACjC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;IACrE,EAAE,EAAE,MAAM,CAAC;CACZ;IAED,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAC;IAEf,oFAAoF;IACpF,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAE1B,sFAAsF;IACtF,OAAO,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;IAE/B,8GAA8G;IAC9G,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;CACZ;AAED;;;;;;;;;;;;;;;;;;GAkBG;AAEH,wBAAgB,aAAa,CAC3B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;IACrE,EAAE,EAAE,MAAM,CAAC;CACZ,EACD,EACA,MAAM,EACN,IAAI,EAAE,QAAQ,EACd,cAAc,EACd,OAAkB,GACnB,EAAE,kBAAkB,CAAC,CAAC,CAAC,kDA+nBvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{jsx as
|
|
1
|
+
"use client";import{jsx as p,jsxs as B}from"react/jsx-runtime";import{useMemo as w}from"react";import{FilterX as Z}from"lucide-react";import{Button as N,Combobox as ee,Grid as te,RangeInput as ne,Slider as ie,Stack as re}from"@donotdev/components";import{DateFilter as se}from"./DateFilter";import{useTranslation as L,handleError as $}from"@donotdev/core";import{translateFieldLabel as le,translateLabel as I}from"../forms/utils";import{getFilterType as z,isFilterable as oe}from"../fieldTypeRegistry";import{useCrudFilters as ae}from"../hooks/useCrudFilters";import{useCrudCardList as ce}from"../useCrudCardList";import{matchesFilter as de}from"../utils/matchesFilter";import{matchesFilter as Ve}from"../utils/matchesFilter";function Ae({entity:c,data:k,fieldsToFilter:S,variant:_="inline"}){const G=_==="sidebar",{t:g,i18n:X}=L("crud"),{t:j}=L([c.namespace,"crud"]),q=X?.language||"en",{data:H}=ce(c,{enabled:!k}),f=k??(H?.items||[]),{filters:x,setFilters:R}=ae({collection:c.collection}),h=w(()=>(S&&S.length>0?S:c.listFields||Object.keys(c.fields)).filter(t=>{const n=c.fields[t]?.type||"text";return oe(n)}),[S,c.listFields,c.fields]),v=w(()=>{const i={};return h.forEach(t=>{const l=Object.fromEntries(Object.entries(x).filter(([n])=>n!==t));if(Object.keys(l).length===0){i[t]=f;return}i[t]=f.filter(n=>Object.entries(l).every(([d,b])=>{const F=n[d],s=c.fields[d]?.type||"text";return de(F,b,s)}))}),i},[f,x,c.fields,h]),J=w(()=>{const i={};return h.forEach(t=>{const l=c.fields[t];if(!l)return;const n=l.type||"text",d=z(n);if(!d){$(new Error(`Field type "${n}" not registered in field type registry`),{userMessage:`Field type "${n}" is missing from registry`,context:{fieldType:n,fieldName:t,operation:"minmax_computation",fix:"Add to registerBuiltinFieldTypes.ts or registerFieldType()"},severity:"warning"});return}const b=d==="range"&&(n==="date"||n==="datetime-local"||n==="timestamp"||n==="time"||n==="week"||n==="month"||n==="year"),F=d==="range"&&!b,C=n==="price";if(F||C){const s=f.map(r=>r[t]).filter(r=>r!=null&&r!=="").map(r=>C&&typeof r=="object"&&r!==null?Number(r.amount):typeof r=="number"?r:Number(r)).filter(r=>!isNaN(r));s.length>0&&(i[t]={min:Math.min(...s),max:Math.max(...s)})}else if(b){const s=f.map(r=>r[t]).filter(r=>r!=null&&r!=="").map(r=>r instanceof Date?r:new Date(r)).filter(r=>!isNaN(r.getTime()));if(s.length>0){const r=new Date(Math.min(...s.map(T=>T.getTime()))),u=new Date(Math.max(...s.map(T=>T.getTime()))),P=r.toISOString().split("T")[0],E=u.toISOString().split("T")[0];i[t]={min:P||"",max:E||""}}}}),i},[f,c.fields,h]),y=(i,t)=>{const l={...x};if(!t||t==="")delete l[i];else if(Array.isArray(t))l[i]=t;else if(typeof t=="object"&&"min"in t){const n=t.min&&t.min!=="",d=t.max&&t.max!=="";!n&&!d?delete l[i]:l[i]=t}else l[i]=t;R(l)},K=()=>{R({})},V=w(()=>h.length===0?null:h.map(i=>{const t=c.fields[i];if(!t)return null;const l=le(i,t,j),n=t.type||"text",d=z(n);if(!d)return $(new Error(`Field type "${n}" not registered in field type registry`),{userMessage:`Field type "${n}" is missing from registry`,context:{fieldType:n,fieldName:i,operation:"filter_ui_render",fix:"Add to registerBuiltinFieldTypes.ts or registerFieldType()"},severity:"warning"}),null;const b=d==="range"&&(n==="date"||n==="datetime-local"||n==="timestamp"||n==="time"||n==="week"||n==="month"||n==="year"),F=d==="range"&&!b,C=d==="select",s=x[i],u=typeof s=="object"&&s!==null&&"min"in s?s:{min:"",max:""};if(F){const o=J[i],a=o?.min??0,e=o?.max??100,m=u.min?Number(u.min):a,O=u.max?Number(u.max):e;return B(re,{gap:"tight",style:{gridColumn:"span 2"},children:[p(ne,{type:"number",label:l,minPlaceholder:g("filter.min",{defaultValue:"Min"}),maxPlaceholder:g("filter.max",{defaultValue:"Max"}),minValue:u.min||"",maxValue:u.max||"",actualMin:a,actualMax:e,onChange:(M,Y)=>{y(i,{min:M,max:Y})},onClear:()=>y(i,void 0)}),p(ie,{value:[m,O],min:a,max:e,step:1,onValueChange:M=>{y(i,{min:String(M[0]),max:String(M[1])})}})]},i)}if(b){const o=n==="week"||n==="month"||n==="year"?n:"date",a=(()=>{if(s){if(typeof s=="object"&&"min"in s){const e=s;return{min:e.min&&e.min.split("T")[0]||"",max:e.max&&e.max.split("T")[0]||""}}if(typeof s=="string"){const e=s.split("T")[0]||"";return{min:e,max:e}}}})();return p("div",{style:{gridColumn:"span 2",gridRow:G?void 0:"span 1"},children:p(se,{label:l,fieldType:o,value:a,locale:q,onChange:e=>{if(!e){y(i,void 0);return}typeof e=="object"&&"min"in e&&y(i,{min:e.min||"",max:e.max||""})},tCrud:g})},i)}const E=C&&t.validation&&"options"in t.validation&&t.validation?Array.isArray(t.validation.options)?t.validation.options:typeof t.validation.options=="function"?t.validation.options():[]:[],T=v[i]||f,D=new Set,W=d==="address";T.forEach(o=>{const a=o[i];if(a!=null)if(W&&typeof a=="object"&&"formatted_address"in a){const e=a;e.formatted_address&&D.add(String(e.formatted_address))}else D.add(String(a))});const A=[{value:"all",label:g("filter.selectPlaceholder",{defaultValue:"All"})}];if(E.length>0){const o=[],a=[];E.forEach(e=>{const m=typeof e=="string"?e:e.value,O=typeof e=="string"?e:e.label;D.has(m)?o.push({value:m,label:O}):a.push({value:m,label:O})}),o.sort((e,m)=>e.label.localeCompare(m.label)),a.sort((e,m)=>e.label.localeCompare(m.label)),o.forEach(e=>{A.push({value:e.value,label:I(e.label,j),disabled:!1})}),a.forEach(e=>{A.push({value:e.value,label:I(e.label,j),disabled:!0})})}else Array.from(D).sort().slice(0,100).forEach(o=>{A.push({value:o,label:o})});return p("div",{style:{gridColumn:"span 2",gridRow:"span 1"},children:p(ee,{label:l,value:typeof s=="string"?s:void 0,onValueChange:o=>{y(i,o==="all"||!o?"":String(o))},options:A,placeholder:g("filter.placeholder",{defaultValue:"Filter..."}),clearable:!0})},i)}),[h,c.fields,f,x,j,g,v]);if(!V||V.length===0)return null;const Q=V.filter(Boolean),U=Object.keys(x).length>0;return B(te,{cols:_==="sidebar"?2:[2,4,6,8],children:[Q,p(N,{variant:"outline",icon:p(Z,{size:18}),onClick:K,disabled:!U,style:{gridColumn:"1 / -1",gridRow:"span 1"},children:g("filter.clear",{defaultValue:"Clear Filters"})})]})}export{Ae as EntityFilters,Ve as matchesFilter};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as i}from"react/jsx-runtime";import{describe as
|
|
1
|
+
import{jsx as i}from"react/jsx-runtime";import{describe as d,it as r,expect as a,vi as t,beforeEach as c}from"vitest";import{render as n,screen as o}from"@testing-library/react";import{EntityFilters as s}from"../EntityFilters";import{defineEntity as u}from"@donotdev/core";t.mock("@donotdev/core",async()=>({...await t.importActual("@donotdev/core"),useTranslation:t.fn(()=>e=>e),handleError:t.fn()})),t.mock("@donotdev/components",()=>({Button:({children:e,...l})=>i("button",{...l,children:e}),Grid:({children:e})=>i("div",{children:e}),Combobox:({children:e})=>i("div",{children:e})})),t.mock("../hooks/useCrudFilters",()=>({useCrudFilters:t.fn(()=>({filters:{},setFilter:t.fn(),clearFilters:t.fn()}))})),d("EntityFilters",()=>{const e=u({name:"Product",collection:"products",fields:{title:{name:"title",label:"Title",type:"text",visibility:"user"},status:{name:"status",label:"Status",type:"select",visibility:"user",validation:{options:[{value:"active",label:"Active"},{value:"inactive",label:"Inactive"}]}}}});c(()=>{t.clearAllMocks()}),r("renders filters for entity fields",()=>{n(i(s,{entity:e})),a(o.getByRole("button",{name:/clear/i})).toBeDefined()}),r("renders with data prop",()=>{n(i(s,{entity:e,data:[{id:"1",title:"Product 1",status:"active"},{id:"2",title:"Product 2",status:"inactive"}]})),a(o.getByRole("button",{name:/clear/i})).toBeDefined()}),r("renders with sidebar variant",()=>{n(i(s,{entity:e,variant:"sidebar"})),a(o.getByRole("button",{name:/clear/i})).toBeDefined()})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as n}from"react/jsx-runtime";import{describe as
|
|
1
|
+
import{jsx as n}from"react/jsx-runtime";import{describe as d,it as i,expect as a,vi as e,beforeEach as f}from"vitest";import{render as l}from"@testing-library/react";import{FormFieldRenderer as s}from"../FormFieldRenderer";e.mock("react-hook-form",()=>({useFormContext:e.fn(()=>({register:e.fn(),control:{},formState:{errors:{}}})),Controller:({render:t})=>{const o={onChange:e.fn(),onBlur:e.fn(),value:"",name:"test-field"};return t({field:o})}})),e.mock("../FieldRegistry",()=>({getFieldRegistry:e.fn(()=>({getComponent:e.fn(()=>()=>n("div",{children:"Mock Field"}))}))})),d("FormFieldRenderer",()=>{const t={name:"title",label:"Title",type:"text",visibility:"user",validation:{required:!0}},o=e.fn(r=>r);f(()=>{e.clearAllMocks()}),i("renders field with correct props",()=>{const{container:r}=l(n(s,{name:"title",config:t,t:o,control:{},errors:{}}));a(r).toBeDefined()}),i("renders textarea field for textarea type",()=>{const r={...t,type:"textarea",name:"description",label:"Description"},{container:c}=l(n(s,{name:"description",config:r,t:o,control:{},errors:{}}));a(c).toBeDefined()}),i("handles uncontrolled mode",()=>{const{container:r}=l(n(s,{name:"title",config:t,t:o,value:"",onChange:e.fn()}));a(r).toBeDefined()})});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FieldType } from '@donotdev/core';
|
|
2
|
+
import type { ControlledFieldProps } from '../types';
|
|
3
|
+
import type { ReactElement } from 'react';
|
|
4
|
+
/** Sub-field definition inside fieldSpecific.fields */
|
|
5
|
+
export interface SubFieldDef {
|
|
6
|
+
name: string;
|
|
7
|
+
type: FieldType;
|
|
8
|
+
label: string;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
options?: Record<string, unknown>;
|
|
11
|
+
validation?: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* ControlledFieldArrayField — nested object array with per-row sub-fields.
|
|
15
|
+
* Each sub-field is rendered via the field type registry so custom types work automatically.
|
|
16
|
+
*/
|
|
17
|
+
export declare function ControlledFieldArrayField(props: ControlledFieldProps): ReactElement;
|
|
18
|
+
//# sourceMappingURL=ControlledFieldArrayField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ControlledFieldArrayField.d.ts","sourceRoot":"","sources":["../../../../src/components/controlled/complex/ControlledFieldArrayField.tsx"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAe,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAK7D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAErD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAE1C,uDAAuD;AACvD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC;AAmBD;;;GAGG;AACH,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,oBAAoB,GAC1B,YAAY,CA2Kd"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";import{jsxs as t,jsx as n}from"react/jsx-runtime";import{Plus as j,Trash2 as R}from"lucide-react";import{useFieldArray as $,Controller as q}from"react-hook-form";import{Button as y}from"@donotdev/components";import{getFieldRegistry as A}from"../../../FieldRegistry";import{convertValidationRules as B}from"../types";function D(s){switch(s){case"number":return 0;case"boolean":return!1;default:return""}}function N(s){const{control:d,errors:h,fieldConfig:c,t:o}=s,{name:m,label:p,validation:b}=c,f=c.options?.fieldSpecific??{},r=f.fields??[],l=f.direction??"row",{fields:x,append:C,remove:S}=$({control:d,name:m}),w=A(),z=()=>{if(r.length===0)return;const a={};for(const i of r)a[i.name]=D(i.type);C(a)};return r.length===0?t("div",{style:{fontSize:"var(--font-size-sm)",color:"var(--muted-foreground)"},children:[o(p),": no sub-fields configured"]}):t("div",{style:{display:"flex",flexDirection:"column",gap:"var(--gap-sm)"},children:[t("div",{style:{display:"flex",alignItems:"center",gap:"var(--gap-sm)"},children:[t("span",{style:{fontSize:"var(--font-size-sm)",fontWeight:"var(--font-weight-medium)"},children:[o(p),b?.required&&n("span",{style:{color:"var(--destructive)",marginInlineStart:"var(--gap-tight)"},children:"*"})]}),n(y,{type:"button",variant:"ghost",display:"compact",icon:j,onClick:z,"aria-label":o(p)+" \u2014 add"})]}),x.map((a,i)=>t("div",{style:{display:"flex",flexDirection:l,gap:"var(--gap-sm)",alignItems:l==="row"?"flex-start":void 0},children:[r.map(e=>{const g=`${m}.${i}.${e.name}`,F={name:g,type:e.type,label:e.label,visibility:c.visibility,validation:{...e.validation??{},...e.required?{required:!0}:{}},options:e.options??{}},v=w.getControlledComponent(e.type);return v?n("div",{style:{flex:l==="row"?1:void 0},children:n(v,{control:d,errors:h,fieldConfig:F,t:o})},e.name):n("div",{style:{flex:l==="row"?1:void 0},children:n(q,{name:g,control:d,rules:e.validation?B(e.validation):void 0,render:({field:u})=>t("label",{style:{display:"flex",flexDirection:"column",gap:"var(--gap-tight)"},children:[n("span",{style:{fontSize:"var(--font-size-xs)"},children:o(e.label)}),n("input",{className:"dndev-input",value:String(u.value??""),onChange:u.onChange,onBlur:u.onBlur})]})})},e.name)}),n(y,{type:"button",variant:"ghost",display:"compact",icon:R,onClick:()=>S(i),"aria-label":`Remove row ${i+1}`,style:{alignSelf:l==="row"?"center":"flex-end"}})]},a.id))]})}export{N as ControlledFieldArrayField};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { ControlledAddressField } from './ControlledAddressField';
|
|
2
|
+
export { ControlledFieldArrayField, type SubFieldDef, } from './ControlledFieldArrayField';
|
|
2
3
|
export { ControlledDateField } from './ControlledDateField';
|
|
3
4
|
export { ControlledGeoPointField } from './ControlledGeoPointField';
|
|
4
5
|
export { ControlledMapField } from './ControlledMapField';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/controlled/complex/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/controlled/complex/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EACL,yBAAyB,EACzB,KAAK,WAAW,GACjB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ControlledAddressField as
|
|
1
|
+
import{ControlledAddressField as r}from"./ControlledAddressField";import{ControlledFieldArrayField as t}from"./ControlledFieldArrayField";import{ControlledDateField as i}from"./ControlledDateField";import{ControlledGeoPointField as m}from"./ControlledGeoPointField";import{ControlledMapField as x}from"./ControlledMapField";import{ControlledMultiInputField as f}from"./ControlledMultiInputField";import{ControlledRichTextField as a}from"./ControlledRichTextField";import{ControlledTimestampField as u}from"./ControlledTimestampField";export{r as ControlledAddressField,i as ControlledDateField,t as ControlledFieldArrayField,m as ControlledGeoPointField,x as ControlledMapField,f as ControlledMultiInputField,a as ControlledRichTextField,u as ControlledTimestampField};
|
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
import type { EntityField, FieldType, ValueTypeForField } from '@donotdev/core';
|
|
2
|
-
import type { Control, FieldErrors, FieldValues, RegisterOptions } from 'react-hook-form';
|
|
2
|
+
import type { Control, ControllerRenderProps, FieldErrors, FieldValues, Path, RegisterOptions } from 'react-hook-form';
|
|
3
3
|
/**
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
|
|
4
|
+
* Props for controlled field components (react-hook-form)
|
|
5
|
+
*
|
|
6
|
+
* Use framework's `useController` hook (from @donotdev/crud) instead of react-hook-form's useController
|
|
7
|
+
* to ensure type compatibility.
|
|
7
8
|
*/
|
|
8
9
|
export interface ControlledFieldProps<T extends FieldValues = FieldValues, FT extends FieldType = FieldType> {
|
|
9
|
-
/** Control object from react-hook-form -
|
|
10
|
+
/** Control object from react-hook-form - use with framework's useController hook */
|
|
10
11
|
control: Control<T>;
|
|
11
12
|
errors: FieldErrors<T>;
|
|
12
13
|
fieldConfig: EntityField<FT>;
|
|
13
14
|
t: (key: string, options?: Record<string, unknown>) => string;
|
|
14
15
|
onChange?: (value: ValueTypeForField<FT>) => void;
|
|
15
16
|
placeholder?: string;
|
|
17
|
+
/** Field object from react-hook-form (provided by framework, no need to use useController) */
|
|
18
|
+
field?: ControllerRenderProps<T, Path<T>>;
|
|
19
|
+
/** Field state from react-hook-form (provided by framework, no need to use useController) */
|
|
20
|
+
fieldState?: {
|
|
21
|
+
error?: {
|
|
22
|
+
message?: string;
|
|
23
|
+
};
|
|
24
|
+
isDirty: boolean;
|
|
25
|
+
isTouched: boolean;
|
|
26
|
+
invalid: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** Form ID for upload tracking - used by file upload fields */
|
|
29
|
+
formId?: string;
|
|
16
30
|
}
|
|
17
31
|
/**
|
|
18
32
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/components/controlled/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEhF,OAAO,KAAK,EACV,OAAO,EACP,WAAW,EACX,WAAW,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/components/controlled/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEhF,OAAO,KAAK,EACV,OAAO,EACP,qBAAqB,EACrB,WAAW,EACX,WAAW,EACX,IAAI,EACJ,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAEzB;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB,CACnC,CAAC,SAAS,WAAW,GAAG,WAAW,EACnC,EAAE,SAAS,SAAS,GAAG,SAAS;IAEhC,oFAAoF;IACpF,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACvB,WAAW,EAAE,WAAW,CAAC,EAAE,CAAC,CAAC;IAC7B,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IAC9D,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;IAClD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,8FAA8F;IAC9F,KAAK,CAAC,EAAE,qBAAqB,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C,6FAA6F;IAC7F,UAAU,CAAC,EAAE;QACX,KAAK,CAAC,EAAE;YAAE,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;QAC7B,OAAO,EAAE,OAAO,CAAC;QACjB,SAAS,EAAE,OAAO,CAAC;QACnB,OAAO,EAAE,OAAO,CAAC;KAClB,CAAC;IACF,+DAA+D;IAC/D,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AAEH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,GACnC,MAAM,GAAG,SAAS,CAsBpB;AAED;;;;GAIG;AAIH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,GAAG,GAAG,eAAe,CA4BvE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DocumentFieldComponent.d.ts","sourceRoot":"","sources":["../../../../src/components/form/fields/DocumentFieldComponent.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DocumentFieldComponent.d.ts","sourceRoot":"","sources":["../../../../src/components/form/fields/DocumentFieldComponent.tsx"],"names":[],"mappings":"AAyDA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAG5D,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,IAAI,CAAC;IACX,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,SAAS,GAAG,IAAI,CAAC;IAC3B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,2BAA2B;IAC1C,2CAA2C;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kBAAkB;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,qEAAqE;IACrE,KAAK,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,GAAG,IAAI,CAAC;IACvC,qBAAqB;IACrB,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS,EAAE,GAAG,IAAI,KAAK,IAAI,CAAC;IAC1D,kBAAkB;IAClB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,kBAAkB;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0BAA0B;IAC1B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mDAAmD;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2DAA2D;IAC3D,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oCAAoC;IACpC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,yBAAyB;IACzB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,yBAAyB;IACxC,8CAA8C;IAC9C,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,qCAAqC;IACrC,YAAY,EAAE,MAAM,gBAAgB,EAAE,CAAC;CACxC;AA2CD;;;GAGG;AACH,QAAA,MAAM,sBAAsB,mIAshB3B,CAAC;AAIF,eAAe,sBAAsB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{jsx as n,jsxs as l,Fragment as
|
|
1
|
+
"use client";import{jsx as n,jsxs as l,Fragment as ie}from"react/jsx-runtime";import{Upload as ae,X as se,FileText as P,FileSpreadsheet as le,File as de,Loader2 as ce,Eye as ue}from"lucide-react";import{useCallback as w,useState as E,useRef as F,useEffect as W,useImperativeHandle as pe,forwardRef as me}from"react";import{Button as _,BUTTON_VARIANT as G,Text as g,Stack as x,Progress as fe,Dialog as ge,DialogContent as ve,DialogHeader as he,DialogTitle as ye}from"@donotdev/components";import{handleError as N,useTranslation as we}from"@donotdev/core";import{useUploadContext as xe}from"../../../contexts/UploadContext";import{useUploadStore as V}from"../../../stores/UploadStore";import{uploadFile as Te,deleteFile as be,generateFileId as X,getFileIcon as Pe,formatFileSize as Fe,getDocumentAcceptString as Ne}from"../../../utils/fileStorage";function De({type:f,className:c}){switch(f){case"pdf":return n(P,{className:c,style:{color:"var(--destructive)"}});case"doc":return n(P,{className:c,style:{color:"var(--primary)"}});case"xls":return n(le,{className:c,style:{color:"var(--success)"}});case"ppt":return n(P,{className:c,style:{color:"var(--warning)"}});case"text":case"html":return n(P,{className:c});default:return n(de,{className:c})}}const q=me(({name:f,label:c,value:v,onChange:D,error:k,helperText:M,multiple:p=!1,maxFiles:A=10,maxSize:C=25*1024*1024,storagePath:R="uploads/documents",required:K,enablePreview:L=!0},J)=>{const{t:Q}=we("dndev"),s=Q,h=xe(),H=!!h,[a,u]=E([]),[O,I]=E(!1),[S,j]=E(null),y=F(null),U=F(!1),T=F(void 0),Y=Ne(),b=w(e=>{const t=e.filter(r=>r.uploaded&&!r.error).map(r=>r.uploaded);D(p?t:t[0]||null)},[p,D]);W(()=>{if(U.current){U.current=!1;return}if(v){const e=[];(Array.isArray(v)?v:[v]).forEach(r=>{r.url&&e.push({id:X(),file:new File([],r.filename),uploadProgress:null,uploaded:r,error:null})}),u(e)}else u([])},[v]);const Z=w(async()=>{const e=a.filter(t=>!t.uploaded&&!t.error&&t.uploadProgress===null);if(e.length!==0){u(t=>t.map(r=>e.some(i=>i.id===r.id)?{...r,uploadProgress:0}:r));for(const t of e)try{const r=await Te(t.file,{storagePath:R,onProgress:i=>{u(o=>o.map(d=>d.id===t.id?{...d,uploadProgress:i.progress}:d))}});u(i=>{const o=i.map(d=>d.id===t.id?{...d,uploaded:r,uploadProgress:100,error:null}:d);return queueMicrotask(()=>b(o)),o})}catch{u(i=>i.map(o=>o.id===t.id?{...o,error:"Upload failed",uploadProgress:null}:o))}}},[a,R,b]);T.current=Z,W(()=>{if(!(!h||!f))return V.getState().registerUpload(h,f,async()=>{await T.current?.()}),()=>{V.getState().unregisterUpload(h,f)}},[h,f]),pe(J,()=>({upload:async()=>{await T.current?.()},getDocuments:()=>B.current||[]}),[]);const B=F(a);B.current=a;const $=e=>{const t=[".pdf",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".txt",".md",".html",".csv"],r="."+e.name.split(".").pop()?.toLowerCase();return t.includes(r)},z=w(e=>{const r=(p?A:1)-a.length;if(r<=0){N(new Error("Maximum documents reached"),{userMessage:s("document.errors.maxFiles",{max:A}),severity:"warning",showNotification:!0});return}const i=e.slice(0,r),o=[],d=(C/(1024*1024)).toFixed(0);for(const m of i){if(!$(m)){N(new Error("Invalid document type"),{userMessage:s("document.errors.invalidType",{fileName:m.name}),severity:"warning",showNotification:!0});continue}if(m.size>C){N(new Error("Document too large"),{userMessage:s("document.errors.exceedsSize",{fileName:m.name,size:d}),severity:"warning",showNotification:!0});continue}o.push({id:X(),file:m,uploadProgress:null,uploaded:null,error:null})}o.length!==0&&(u(m=>p?[...m,...o]:o),H||setTimeout(()=>T.current?.(),0))},[a,p,A,C,s,H]),ee=e=>{const t=Array.from(e.target.files||[]);t.length>0&&z(t),y.current&&(y.current.value="")},re=w(e=>{e.preventDefault(),I(!1);const t=Array.from(e.dataTransfer.files);z(t)},[z]),te=w(async e=>{const t=a.find(r=>r.id===e);if(t)try{t.uploaded&&await be(t.uploaded);const r=a.filter(i=>i.id!==e);U.current=!0,u(r),setTimeout(()=>b(r),0)}catch(r){N(r,{userMessage:s("document.delete.failed"),severity:"error",showNotification:!0})}},[a,b,s]),ne=e=>{e.uploaded?.url&&e.uploaded.mimeType==="application/pdf"&&j(e.uploaded.url)},oe=p||a.length===0;return l(ie,{children:[l(x,{gap:"tight",children:[l(g,{level:"body",align:"start",children:[c,K?"*":""]}),oe&&l("div",{role:"button",tabIndex:0,"aria-label":s("document.upload.ariaLabel"),className:"dndev-surface","data-variant":k?"destructive":"default",style:{border:"var(--border-width) dashed",borderColor:O?"var(--primary)":k?"var(--destructive)":"var(--border)",borderRadius:"var(--radius-lg)",padding:"var(--gap-lg)",backgroundColor:O?"color-mix(in oklab, var(--primary) 5%, transparent)":"var(--surface)",transition:"border-color var(--dur-fast), background-color var(--dur-fast)",cursor:"pointer",outline:"none"},onDragOver:e=>{e.preventDefault(),I(!0)},onDragLeave:()=>I(!1),onDrop:re,onClick:()=>y.current?.click(),onKeyDown:e=>{(e.key==="Enter"||e.key===" ")&&(e.preventDefault(),y.current?.click())},children:[n("input",{ref:y,type:"file",accept:Y,multiple:p,style:{display:"none"},onChange:ee}),l(x,{align:"center",justify:"center",children:[n(ae,{className:"dndev-size-lg",style:{color:"var(--muted-foreground)"}}),n(g,{as:"p",variant:"muted",level:"small",align:"center",children:s("document.upload.hint")}),n(g,{as:"p",variant:"muted",level:"caption",align:"center",children:"PDF, Word, Excel, PowerPoint, TXT, Markdown, HTML"})]})]}),a.length>0&&n(x,{gap:"tight",children:a.map((e,t)=>{const r=Pe(e.uploaded?.mimeType||e.file.type,e.file.name),i=e.uploadProgress!==null&&e.uploadProgress<100,o=e.uploaded?.mimeType==="application/pdf"||e.file.name.toLowerCase().endsWith(".pdf");return l("div",{className:"dndev-surface","data-variant":"muted",style:{padding:"var(--gap-md)",borderRadius:"var(--radius-md)",display:"flex",alignItems:"center",gap:"var(--gap-md)"},children:[n(De,{type:r,className:"dndev-size-md"}),l(x,{gap:"none",style:{flex:1,minWidth:0},children:[n(g,{level:"small",style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e.file.name}),l(g,{level:"caption",variant:"muted",children:[Fe(e.uploaded?.size||e.file.size),e.error&&l("span",{style:{color:"var(--destructive)"},children:[" ","\xB7 ",e.error]})]}),i&&n(fe,{value:e.uploadProgress||0,max:100,style:{marginTop:"var(--gap-xs)"}})]}),l(x,{direction:"row",gap:"tight",children:[L&&o&&e.uploaded&&n(_,{variant:G.GHOST,onClick:()=>ne(e),"aria-label":s("document.preview.ariaLabel"),children:n(ue,{className:"dndev-size-sm"})}),i?n(ce,{className:"dndev-size-sm",style:{animation:"spin 1s linear infinite"}}):n(_,{variant:G.GHOST,onClick:()=>te(e.id),"aria-label":s("document.delete.ariaLabel"),children:n(se,{className:"dndev-size-sm"})})]})]},e.id)})}),M&&n(g,{level:"caption",variant:k?"destructive":"muted",style:{marginTop:"var(--gap-xs)"},children:M})]}),L&&S&&n(ge,{open:!!S,onOpenChange:()=>j(null),children:l(ve,{style:{maxWidth:"90vw",maxHeight:"90vh",width:"900px",height:"80vh"},children:[n(he,{children:n(ye,{children:s("document.preview.title")})}),n("iframe",{src:S,sandbox:"allow-same-origin",style:{width:"100%",height:"100%",border:"none",borderRadius:"var(--radius-md)"},title:"PDF Preview"})]})})]})});q.displayName="DocumentFieldComponent";var Me=q;export{Me as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FileFieldComponent.d.ts","sourceRoot":"","sources":["../../../../src/components/form/fields/FileFieldComponent.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FileFieldComponent.d.ts","sourceRoot":"","sources":["../../../../src/components/form/fields/FileFieldComponent.tsx"],"names":[],"mappings":"AAyDA,OAAO,KAAK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@ -1 +1 @@
|
|
|
1
|
-
"use client";import{jsx as
|
|
1
|
+
"use client";import{jsx as t,jsxs as p}from"react/jsx-runtime";import{Upload as Z,X as $,File as ee,Loader2 as re,FileText as z,FileSpreadsheet as se,FileImage as te,FileVideo as oe,FileAudio as ae,FileArchive as ne,FileCode as le}from"lucide-react";import{useCallback as y,useState as B,useRef as b,useEffect as O,useImperativeHandle as ie,forwardRef as de}from"react";import{Button as ue,BUTTON_VARIANT as ce,Text as F,Stack as P,Progress as pe}from"@donotdev/components";import{handleError as C,useTranslation as fe}from"@donotdev/core";import{useUploadContext as ge}from"../../../contexts/UploadContext";import{useUploadStore as H}from"../../../stores/UploadStore";import{uploadFile as me,deleteFile as ve,generateFileId as _,getFileIcon as he,formatFileSize as ye}from"../../../utils/fileStorage";function Fe({type:f,className:l}){switch(f){case"pdf":return t(z,{className:l,style:{color:"var(--destructive)"}});case"doc":return t(z,{className:l,style:{color:"var(--primary)"}});case"xls":return t(se,{className:l,style:{color:"var(--success)"}});case"ppt":return t(z,{className:l,style:{color:"var(--warning)"}});case"image":return t(te,{className:l});case"video":return t(oe,{className:l});case"audio":return t(ae,{className:l});case"archive":return t(ne,{className:l});case"html":case"text":return t(le,{className:l});default:return t(ee,{className:l})}}const V=de(({name:f,label:l,value:g,onChange:T,error:A,helperText:M,accept:q,multiple:d=!1,maxFiles:N=10,maxSize:k=50*1024*1024,storagePath:R="uploads/files",required:D},G)=>{const{t:K}=fe("dndev"),i=K,m=ge(),E=!!m,[n,c]=B([]),[j,I]=B(!1),v=b(null),S=b(!1),w=b(void 0),x=y(e=>{const s=e.filter(r=>r.uploaded&&!r.error).map(r=>r.uploaded);T(d?s:s[0]||null)},[d,T]);O(()=>{if(S.current){S.current=!1;return}if(g){const e=[];(Array.isArray(g)?g:[g]).forEach(r=>{r.url&&e.push({id:_(),file:new File([],r.filename),uploadProgress:null,uploaded:r,error:null})}),c(e)}else c([])},[g]);const W=y(async()=>{const e=n.filter(s=>!s.uploaded&&!s.error&&s.uploadProgress===null);if(e.length!==0){c(s=>s.map(r=>e.some(o=>o.id===r.id)?{...r,uploadProgress:0}:r));for(const s of e)try{const r=await me(s.file,{storagePath:R,onProgress:o=>{c(a=>a.map(u=>u.id===s.id?{...u,uploadProgress:o.progress}:u))}});c(o=>{const a=o.map(u=>u.id===s.id?{...u,uploaded:r,uploadProgress:100,error:null}:u);return queueMicrotask(()=>x(a)),a})}catch{c(o=>o.map(a=>a.id===s.id?{...a,error:"Upload failed",uploadProgress:null}:a))}}},[n,R,x]);w.current=W,O(()=>{if(!(!m||!f))return H.getState().registerUpload(m,f,async e=>{await w.current?.()}),()=>{H.getState().unregisterUpload(m,f)}},[m,f]),ie(G,()=>({upload:async()=>{await w.current?.()},getFiles:()=>L.current||[]}),[]);const L=b(n);L.current=n;const U=y(e=>{const r=(d?N:1)-n.length;if(r<=0){C(new Error("Maximum files reached"),{userMessage:i("file.errors.maxFiles",{max:N}),severity:"warning",showNotification:!0});return}const o=e.slice(0,r),a=[],u=(k/(1024*1024)).toFixed(0);for(const h of o){if(h.size>k){C(new Error("File too large"),{userMessage:i("file.errors.exceedsSize",{fileName:h.name,size:u}),severity:"warning",showNotification:!0});continue}a.push({id:_(),file:h,uploadProgress:null,uploaded:null,error:null})}a.length!==0&&(c(h=>d?[...h,...a]:a),E||setTimeout(()=>w.current?.(),0))},[n,d,N,k,i,E]),X=e=>{const s=Array.from(e.target.files||[]);s.length>0&&U(s),v.current&&(v.current.value="")},J=y(e=>{e.preventDefault(),I(!1);const s=Array.from(e.dataTransfer.files);U(s)},[U]),Q=y(async e=>{const s=n.find(r=>r.id===e);if(s)try{s.uploaded&&await ve(s.uploaded);const r=n.filter(o=>o.id!==e);S.current=!0,c(r),setTimeout(()=>x(r),0)}catch(r){C(r,{userMessage:i("file.delete.failed"),severity:"error",showNotification:!0})}},[n,x,i]),Y=d||n.length===0,we=n.some(e=>!e.uploaded&&!e.error&&e.uploadProgress===null);return p(P,{gap:"tight",children:[p(F,{level:"body",align:"start",children:[l,D?"*":""]}),Y&&p("div",{role:"button",tabIndex:0,"aria-label":i(d?"file.upload.ariaLabelMultiple":"file.upload.ariaLabelSingle"),className:"dndev-surface","data-variant":A?"destructive":"default",style:{border:"var(--border-width) dashed",borderColor:j?"var(--primary)":A?"var(--destructive)":"var(--border)",borderRadius:"var(--radius-lg)",padding:"var(--gap-lg)",backgroundColor:j?"color-mix(in oklab, var(--primary) 5%, transparent)":"var(--surface)",transition:"border-color var(--dur-fast), background-color var(--dur-fast)",cursor:"pointer",outline:"none"},onDragOver:e=>{e.preventDefault(),I(!0)},onDragLeave:()=>I(!1),onDrop:J,onClick:()=>v.current?.click(),onKeyDown:e=>{(e.key==="Enter"||e.key===" ")&&(e.preventDefault(),v.current?.click())},children:[t("input",{ref:v,type:"file",accept:q,multiple:d,style:{display:"none"},onChange:X}),p(P,{align:"center",justify:"center",children:[t(Z,{className:"dndev-size-lg",style:{color:"var(--muted-foreground)"}}),t(F,{as:"p",variant:"muted",level:"small",align:"center",children:i(d?"file.upload.dragDropMultiple":"file.upload.dragDropSingle")})]})]}),n.length>0&&t(P,{gap:"tight",children:n.map((e,s)=>{const r=he(e.uploaded?.mimeType||e.file.type,e.file.name),o=e.uploadProgress!==null&&e.uploadProgress<100,a=e.uploaded&&!e.error;return p("div",{className:"dndev-surface","data-variant":"muted",style:{padding:"var(--gap-md)",borderRadius:"var(--radius-md)",display:"flex",alignItems:"center",gap:"var(--gap-md)"},children:[t(Fe,{type:r,className:"dndev-size-md"}),p(P,{gap:"none",style:{flex:1,minWidth:0},children:[t(F,{level:"small",style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e.file.name}),p(F,{level:"caption",variant:"muted",children:[ye(e.uploaded?.size||e.file.size),e.error&&p("span",{style:{color:"var(--destructive)"},children:[" ","\xB7 ",e.error]})]}),o&&t(pe,{value:e.uploadProgress||0,max:100,style:{marginTop:"var(--gap-xs)"}})]}),o?t(re,{className:"dndev-size-sm",style:{animation:"spin 1s linear infinite"}}):t(ue,{variant:ce.GHOST,onClick:()=>Q(e.id),"aria-label":i("file.delete.ariaLabel"),children:t($,{className:"dndev-size-sm"})})]},e.id)})}),M&&t(F,{level:"caption",variant:A?"destructive":"muted",style:{marginTop:"var(--gap-xs)"},children:M})]})});V.displayName="FileFieldComponent";var Ue=V;export{Ue as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TiptapEditor.d.ts","sourceRoot":"","sources":["../../../../../src/components/form/fields/internal/TiptapEditor.tsx"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AACvD,OAAO,KAAK,EAAe,aAAa,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"TiptapEditor.d.ts","sourceRoot":"","sources":["../../../../../src/components/form/fields/internal/TiptapEditor.tsx"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AACvD,OAAO,KAAK,EAAe,aAAa,EAAE,MAAM,OAAO,CAAC;AA0KxD;;;;;;;;;GASG;AACH,QAAA,MAAM,YAAY,EAAE,aAAa,CAAC,sBAAsB,CAuDvD,CAAC;AAEF,eAAe,YAAY,CAAC"}
|