UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

119 lines (114 loc) 4.29 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkDRIFBRUSjs = require('./chunk-DRIFBRUS.js'); // src/store/userStore.ts var _zustand = require('zustand'); var _middleware = require('zustand/middleware'); var DEFAULT_CACHE_TTL = 5 * 60 * 1e3; var isCacheValid = (lastFetched, cacheTTL) => { if (!lastFetched) return false; return Date.now() - lastFetched < cacheTTL; }; function createUserStore(config) { const { apiClient, storageKey = "user-data-storage", cacheTTL = DEFAULT_CACHE_TTL } = config; const getMyData = async () => { const response = await apiClient.get("/auth/me"); return response.data; }; const getCurrentUserId = () => { return _nullishCoalesce(_optionalChain([_chunkDRIFBRUSjs.useAuthStore, 'access', _ => _.getState, 'call', _2 => _2(), 'access', _3 => _3.user, 'optionalAccess', _4 => _4.id]), () => ( null)); }; const isCacheForCurrentUser = (cachedUserId) => { const currentUserId = getCurrentUserId(); if (!currentUserId) { return false; } if (!cachedUserId) { return false; } return cachedUserId === currentUserId; }; return _zustand.create.call(void 0, )( _middleware.persist.call(void 0, (set, get) => ({ // Initial state data: null, cachedUserId: null, lastFetched: null, isLoading: false, error: null, /** * Fetch user data from API with caching */ fetchUserData: async (force = false) => { const { data, cachedUserId, lastFetched, isLoading } = get(); if (isLoading) return; const cacheValidForUser = isCacheForCurrentUser(cachedUserId); if (!force && data && cacheValidForUser && isCacheValid(lastFetched, cacheTTL)) { return; } if (!cacheValidForUser && data) { set({ data: null, cachedUserId: null, lastFetched: null }); } try { set({ isLoading: true, error: null }); const userData = await getMyData(); set({ data: userData, cachedUserId: _nullishCoalesce(_optionalChain([userData, 'access', _5 => _5.user, 'optionalAccess', _6 => _6.id]), () => ( null)), lastFetched: Date.now(), isLoading: false, error: null }); } catch (error) { const errorMessage = error instanceof Error ? error.message : "Failed to fetch user data"; set({ isLoading: false, error: errorMessage }); throw error; } }, /** * Clear all user data from store */ clearUserData: () => { set({ data: null, cachedUserId: null, lastFetched: null, isLoading: false, error: null }); }, /** * Set loading state */ setLoading: (loading) => { set({ isLoading: loading }); }, /** * Set error state */ setError: (error) => { set({ error }); } }), { name: storageKey, storage: _middleware.createJSONStorage.call(void 0, () => localStorage), // Persist data, cachedUserId and lastFetched (not loading/error states) partialize: (state) => ({ data: state.data, cachedUserId: state.cachedUserId, lastFetched: state.lastFetched }) } ) ); } exports.createUserStore = createUserStore; //# sourceMappingURL=chunk-HOSSBEUE.js.map