@logistically/i18n-react-core
Version:
React integration for @logistically/i18n with state management agnostic design
3 lines (2 loc) • 5.9 kB
JavaScript
import{createAsyncThunk as t,createSlice as e}from"@reduxjs/toolkit";const a=t=>t?{message:t.message,name:t.name,stack:t.stack}:null,s=t=>{if(!t)return null;const e=new Error(t.message);return e.name=t.name,e.stack=t.stack,e},n=t("translation/loadTranslations",async(t,{dispatch:e,getState:a})=>{const s=performance.now();try{const n=a(),r=a.core||n.core||n.translation.core;if(!r)throw new Error("Translation core not initialized");await r.loadTranslations(t);const o=performance.now();return e(h({translationTime:o-s})),{locale:t,success:!0}}catch(t){const a=performance.now();throw e(h({translationTime:a-s})),t}}),r=t("translation/reloadTranslations",async(t,{dispatch:e,getState:a})=>{const s=performance.now();try{const t=a(),n=a.core||t.core||t.translation.core;if(!n)throw new Error("Translation core not initialized");await n.reloadTranslations();const r=performance.now();return e(h({translationTime:r-s})),{success:!0}}catch(t){const a=performance.now();throw e(h({translationTime:a-s})),t}}),o=e({name:"translation",initialState:{locale:"en",translations:{},isLoading:!1,error:null,isInitialized:!1,lastUpdated:Date.now(),pendingActions:[],performanceMetrics:{translationTime:0,cacheHitRate:0,lastCacheCleanup:Date.now()}},reducers:{setLocale:(t,e)=>{t.locale=e.payload,t.lastUpdated=Date.now()},setLoading:(t,e)=>{t.isLoading=e.payload},setError:(t,e)=>{e.payload instanceof Error?t.error=a(e.payload):t.error=e.payload??null,t.lastUpdated=Date.now()},setInitialized:(t,e)=>{t.isInitialized=e.payload},setTranslations:(t,e)=>{t.translations=e.payload,t.lastUpdated=Date.now()},setPerformanceMetrics:(t,e)=>{t.performanceMetrics={...t.performanceMetrics,...e.payload}},addPendingAction:(t,e)=>{t.pendingActions.includes(e.payload)||t.pendingActions.push(e.payload)},removePendingAction:(t,e)=>{t.pendingActions=t.pendingActions.filter(t=>t!==e.payload)},updateLastUpdated:t=>{t.lastUpdated=Date.now()},clearCache:t=>{t.translations={},t.performanceMetrics.lastCacheCleanup=Date.now(),t.lastUpdated=Date.now()}},extraReducers:t=>{t.addCase(n.pending,t=>{t.isLoading=!0,t.error=null,t.pendingActions.includes("loadTranslations")||t.pendingActions.push("loadTranslations")}).addCase(n.fulfilled,(t,e)=>{t.isLoading=!1,t.error=null,t.pendingActions=t.pendingActions.filter(t=>"loadTranslations"!==t),t.lastUpdated=Date.now()}).addCase(n.rejected,(t,e)=>{t.isLoading=!1,t.error=a(e.error),t.pendingActions=t.pendingActions.filter(t=>"loadTranslations"!==t),t.lastUpdated=Date.now()}).addCase(r.pending,t=>{t.isLoading=!0,t.error=null,t.pendingActions.includes("reloadTranslations")||t.pendingActions.push("reloadTranslations")}).addCase(r.fulfilled,t=>{t.isLoading=!1,t.error=null,t.pendingActions=t.pendingActions.filter(t=>"reloadTranslations"!==t),t.lastUpdated=Date.now()}).addCase(r.rejected,(t,e)=>{t.isLoading=!1,t.error=a(e.error),t.pendingActions=t.pendingActions.filter(t=>"reloadTranslations"!==t),t.lastUpdated=Date.now()})}}),{setLocale:i,setLoading:c,setError:l,setInitialized:d,setTranslations:p,setPerformanceMetrics:h,addPendingAction:u,removePendingAction:g,updateLastUpdated:m,clearCache:f}=o.actions,w=o.reducer;class T{constructor(t,e){this.unsubscribe=null,this.core=t,this.store=e,this.unsubscribe=this.core.subscribe(t=>{this.store.dispatch(p(t.translations)),this.store.dispatch(i(t.locale)),this.store.dispatch(c(t.isLoading)),this.store.dispatch(l(t.error)),this.store.dispatch(d(t.isInitialized))})}getState(){const t=this.store.getState().translation;return{...t,error:s(t.error)}}getActions(){const t=this.store.dispatch;return{setLocale:async e=>{await this.core.setLocale(e),t(i(e))},loadTranslations:async e=>{await this.core.loadTranslations(e),t(n(e))},translate:(e,a)=>{const s=this.core.translate(e,a);return t(m()),s},translatePlural:(e,a,s)=>{const n=this.core.translatePlural(e,a,s);return t(m()),n},formatDate:(e,a)=>{const s=this.core.formatDate(e,a);return t(m()),s},formatNumber:(e,a)=>{const s=this.core.formatNumber(e,a);return t(m()),s},getTextDirection:t=>this.core.getTextDirection(t),isRTLLocale:t=>this.core.isRTLLocale(t),reloadTranslations:async()=>{await this.core.reloadTranslations(),t(r())},clearCache:()=>{this.core.clearCache(),t(f())}}}subscribe(t){let e=this.getState();return this.store.subscribe(()=>{const a=this.getState();a!==e&&(e=a,t(a))})}async initialize(t){try{this.store.dispatch(c(!0)),this.store.dispatch(l(null)),await this.core.initialize(),this.store.dispatch(d(!0)),this.store.dispatch(c(!1))}catch(t){throw this.store.dispatch(l(t)),this.store.dispatch(c(!1)),t}}destroy(){this.unsubscribe&&"function"==typeof this.unsubscribe&&(this.unsubscribe(),this.unsubscribe=null),this.core.destroy()}}const A=(t,e)=>new T(t,e),L=()=>{const{useSelector:t,useDispatch:e}=require("react-redux"),a=e(),o=t(t=>t.translation),c={setLocale:t=>a(i(t)),loadTranslations:t=>a(n(t)),reloadTranslations:()=>a(r()),clearCache:()=>a(f()),setPerformanceMetrics:t=>a(h(t))};return{...o,error:s(o.error),...c}},y=()=>{const{useSelector:t,useDispatch:e}=require("react-redux"),a=e(),o=t(t=>t.translation),c={setLocale:t=>{const e=performance.now();a(i(t));const s=performance.now();a(h({translationTime:s-e}))},loadTranslations:t=>a(n(t)),reloadTranslations:()=>a(r()),clearCache:()=>a(f()),setPerformanceMetrics:t=>a(h(t))};return{...o,error:s(o.error),...c,getPerformanceMetrics:()=>o.performanceMetrics,isActionPending:t=>o.pendingActions.includes(t),getLastUpdated:()=>o.lastUpdated}};export{T as ReduxAdapter,u as addPendingAction,f as clearCache,A as createReduxAdapter,n as loadTranslationsAsync,r as reloadTranslationsAsync,g as removePendingAction,l as setError,d as setInitialized,c as setLoading,i as setLocale,h as setPerformanceMetrics,p as setTranslations,w as translationReducer,o as translationSlice,m as updateLastUpdated,L as useReduxTranslation,y as useReduxTranslationWithMetrics};
//# sourceMappingURL=redux.esm.js.map