UNPKG

analytica-frontend-lib

Version:

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

292 lines (276 loc) 13.7 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/modulesStore.ts var _zustand = require('zustand'); var _middleware = require('zustand/middleware'); // src/types/modulesConfig.ts var DEFAULT_SIMULATIONS = { enabled: true, enem: "ENABLED", prova: "ENABLED", simuladao: "ENABLED", vestibular: "ENABLED" }; var DEFAULT_EXAMS = true; var DEFAULT_PERFORMANCE_GRAPHS = { aulas: true, acessos: true, simulados: true, atividades: true, questoes: true, ranking: true }; var DEFAULT_REPORTS = { simulatedReports: true, simulatedGenericReports: false, // New reports default to disabled activitiesReports: true, questionnairesReports: false, // New reports default to disabled lessonsReports: true, essayReports: true }; var DEFAULT_SIMULATED_SCORE = { tri: true, absoluto: true }; var DEFAULT_MODULES = { // Core modules simulator: true, essay: true, forum: true, support: true, chat: true, recommendedLessons: true, activities: true, questionBanks: true, comparator: true, performance: true, dashboard: true, lessons: true, // Tutorial off by default (opt-in per institution, requires a url) tutorial: false, tutorialUrl: "", // Reading-fluency mode off by default (opt-in per institution) readingFluency: false, // Nested configurations exams: DEFAULT_EXAMS, simulations: DEFAULT_SIMULATIONS, performanceGraphs: DEFAULT_PERFORMANCE_GRAPHS, reports: DEFAULT_REPORTS, simulatedScore: DEFAULT_SIMULATED_SCORE, // Legacy flat fields (for backwards compatibility) simulatedReports: true, simulatedGenericReports: false, activitiesReports: true, questionnairesReports: false, lessonsReports: true, essayReports: true, simulatedScoreTri: true, simulatedScoreAbsoluto: true }; var mergeModulesConfig = (version) => { const v = _nullishCoalesce(version, () => ( {})); return { ...DEFAULT_MODULES, ...v, // exams is now a simple boolean, use default if not provided exams: _nullishCoalesce(v.exams, () => ( DEFAULT_EXAMS)), simulations: { ...DEFAULT_SIMULATIONS, ...v.simulations }, performanceGraphs: { ...DEFAULT_PERFORMANCE_GRAPHS, ...v.performanceGraphs }, reports: { ...DEFAULT_REPORTS, ...v.reports }, simulatedScore: { ...DEFAULT_SIMULATED_SCORE, ...v.simulatedScore } }; }; // src/store/modulesStore.ts var defaultModules = DEFAULT_MODULES; var latestRequestId = 0; var MAX_RETRIES = 3; var INITIAL_RETRY_DELAY = 1e3; var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); var readLocalStorage = (key) => { try { return localStorage.getItem(key); } catch (e) { return null; } }; var hasCachedModules = (profileType) => { const cached = readLocalStorage("@modules-storage:analytica:v1" /* MODULES_STORAGE */); if (!cached) return false; try { const parsed = JSON.parse(cached); const hasInstitution = Boolean(_optionalChain([parsed, 'access', _ => _.state, 'optionalAccess', _2 => _2.ownerInstitutionId])); const profileMatches = !profileType || _optionalChain([parsed, 'access', _3 => _3.state, 'optionalAccess', _4 => _4.ownerProfileType]) === profileType; return hasInstitution && profileMatches; } catch (e2) { return false; } }; var isStaleRequest = (requestId) => requestId !== latestRequestId; var fetchWithRetry = async (institutionId, api, requestId, profileType) => { for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) { if (attempt > 0) { await delay(INITIAL_RETRY_DELAY * Math.pow(2, attempt - 1)); } if (isStaleRequest(requestId)) return null; try { const endpoint = profileType ? `/featureFlags/institution/${institutionId}/page/MODULES/profile/${profileType}` : `/featureFlags/institution/${institutionId}/page/MODULES`; const response = await api.get(endpoint); if (isStaleRequest(requestId)) return null; return _nullishCoalesce(_optionalChain([response, 'access', _5 => _5.data, 'optionalAccess', _6 => _6.data, 'optionalAccess', _7 => _7.featureFlags, 'optionalAccess', _8 => _8.version]), () => ( {})); } catch (e3) { } } console.warn("[modulesStore] Failed to fetch modules after retries"); return null; }; var useModulesStore = _zustand.create.call(void 0, )( _middleware.persist.call(void 0, (set) => ({ modules: defaultModules, loading: false, ownerInstitutionId: null, ownerProfileType: null, /** * Fetch modules configuration from the API * Only fetches if: * 1. No modules data exists in localStorage for this profile * 2. User made a new login (data cleared by auth subscriber) * Implements retry with exponential backoff on failure * * @param institutionId - The institution UUID * @param api - Axios instance for API calls * @param profileType - Optional profile type (STUDENT, TEACHER, etc.) */ fetchModules: async (institutionId, api, profileType) => { if (hasCachedModules(profileType)) return; const requestId = ++latestRequestId; set({ loading: true }); const version = await fetchWithRetry( institutionId, api, requestId, profileType ); if (isStaleRequest(requestId)) return; if (version === null) { set({ modules: defaultModules, loading: false }); } else { set({ modules: mergeModulesConfig(version), ownerInstitutionId: institutionId, ownerProfileType: _nullishCoalesce(profileType, () => ( null)), loading: false }); } }, /** * Clear modules data (useful when user/institution/profile changes) * Also invalidates any in-flight requests to prevent stale data overwriting cleared state */ clearModules: () => { latestRequestId++; set({ modules: defaultModules, loading: false, ownerInstitutionId: null, ownerProfileType: null }); } }), { name: "@modules-storage:analytica:v1" /* MODULES_STORAGE */, storage: _middleware.createJSONStorage.call(void 0, () => localStorage), partialize: (state) => ({ modules: state.modules, ownerInstitutionId: state.ownerInstitutionId, ownerProfileType: state.ownerProfileType }), onRehydrateStorage: () => (rehydrated) => { if (!rehydrated) return; const mergedModules = mergeModulesConfig(rehydrated.modules); useModulesStore.setState({ modules: mergedModules }); const currentInstitutionId = _nullishCoalesce(_optionalChain([_chunkDRIFBRUSjs.useAuthStore, 'access', _9 => _9.getState, 'call', _10 => _10(), 'access', _11 => _11.sessionInfo, 'optionalAccess', _12 => _12.institutionId]), () => ( null)); const currentProfile = _nullishCoalesce(_optionalChain([_chunkDRIFBRUSjs.useAuthStore, 'access', _13 => _13.getState, 'call', _14 => _14(), 'access', _15 => _15.sessionInfo, 'optionalAccess', _16 => _16.profileName]), () => ( null)); if (rehydrated.ownerInstitutionId && rehydrated.ownerInstitutionId !== currentInstitutionId || rehydrated.ownerProfileType && rehydrated.ownerProfileType !== currentProfile) { useModulesStore.getState().clearModules(); } } } ) ); var lastInstitutionId = _nullishCoalesce(_optionalChain([_chunkDRIFBRUSjs.useAuthStore, 'access', _17 => _17.getState, 'call', _18 => _18(), 'access', _19 => _19.sessionInfo, 'optionalAccess', _20 => _20.institutionId]), () => ( null)); var lastProfileType = _nullishCoalesce(_optionalChain([_chunkDRIFBRUSjs.useAuthStore, 'access', _21 => _21.getState, 'call', _22 => _22(), 'access', _23 => _23.sessionInfo, 'optionalAccess', _24 => _24.profileName]), () => ( null)); _chunkDRIFBRUSjs.useAuthStore.subscribe((state) => { const nextInstitutionId = _nullishCoalesce(_optionalChain([state, 'access', _25 => _25.sessionInfo, 'optionalAccess', _26 => _26.institutionId]), () => ( null)); const nextProfileType = _nullishCoalesce(_optionalChain([state, 'access', _27 => _27.sessionInfo, 'optionalAccess', _28 => _28.profileName]), () => ( null)); if (nextInstitutionId !== lastInstitutionId || nextProfileType !== lastProfileType) { if (lastInstitutionId !== null || lastProfileType !== null) { useModulesStore.getState().clearModules(); } lastInstitutionId = nextInstitutionId; lastProfileType = nextProfileType; } }); // src/hooks/useModules.ts var useModules = () => { const { modules, loading } = useModulesStore(); const simulations = _nullishCoalesce(modules.simulations, () => ( DEFAULT_SIMULATIONS)); const performanceGraphs = _nullishCoalesce(modules.performanceGraphs, () => ( DEFAULT_PERFORMANCE_GRAPHS)); const reports = _nullishCoalesce(modules.reports, () => ( DEFAULT_REPORTS)); const simulatedScore = _nullishCoalesce(modules.simulatedScore, () => ( DEFAULT_SIMULATED_SCORE)); const tutorialUrl = (_nullishCoalesce(modules.tutorialUrl, () => ( ""))).trim(); return { modules, loading, // Core modules hasSimulator: _nullishCoalesce(modules.simulator, () => ( true)), hasEssay: _nullishCoalesce(modules.essay, () => ( true)), hasForum: _nullishCoalesce(modules.forum, () => ( true)), hasSupport: _nullishCoalesce(modules.support, () => ( true)), hasChat: _nullishCoalesce(modules.chat, () => ( true)), hasRecommendedLessons: _nullishCoalesce(modules.recommendedLessons, () => ( true)), hasActivities: _nullishCoalesce(modules.activities, () => ( true)), hasQuestionBanks: _nullishCoalesce(modules.questionBanks, () => ( true)), hasComparator: _nullishCoalesce(modules.comparator, () => ( true)), hasPerformance: _nullishCoalesce(modules.performance, () => ( true)), hasDashboard: _nullishCoalesce(modules.dashboard, () => ( true)), hasLessons: _nullishCoalesce(modules.lessons, () => ( true)), // Tutorial: only show the menu link when enabled AND a url is configured hasTutorial: (_nullishCoalesce(modules.tutorial, () => ( false))) && tutorialUrl.length > 0, tutorialUrl, // Reading-fluency mode (opt-in per institution, defaults off) hasReadingFluency: _nullishCoalesce(modules.readingFluency, () => ( false)), // Exams (simple boolean, with backwards compatibility for old object format) hasExams: typeof modules.exams === "object" ? _nullishCoalesce(modules.exams.enabled, () => ( true)) : _nullishCoalesce(modules.exams, () => ( true)), // Simulations hasSimulations: simulations.enabled, simulations, // Performance graphs hasPerformanceAulas: _nullishCoalesce(performanceGraphs.aulas, () => ( true)), hasPerformanceAcessos: _nullishCoalesce(performanceGraphs.acessos, () => ( true)), hasPerformanceSimulados: _nullishCoalesce(performanceGraphs.simulados, () => ( true)), hasPerformanceAtividades: _nullishCoalesce(performanceGraphs.atividades, () => ( true)), hasPerformanceQuestoes: _nullishCoalesce(performanceGraphs.questoes, () => ( true)), hasPerformanceRanking: _nullishCoalesce(performanceGraphs.ranking, () => ( true)), performanceGraphs, // Reports (support both nested and flat for backwards compatibility) hasSimulatedReports: _nullishCoalesce(_nullishCoalesce(reports.simulatedReports, () => ( modules.simulatedReports)), () => ( true)), hasSimulatedGenericReports: _nullishCoalesce(_nullishCoalesce(reports.simulatedGenericReports, () => ( modules.simulatedGenericReports)), () => ( false)), hasActivitiesReports: _nullishCoalesce(_nullishCoalesce(reports.activitiesReports, () => ( modules.activitiesReports)), () => ( true)), hasQuestionnairesReports: _nullishCoalesce(_nullishCoalesce(reports.questionnairesReports, () => ( modules.questionnairesReports)), () => ( false)), hasLessonsReports: _nullishCoalesce(_nullishCoalesce(reports.lessonsReports, () => ( modules.lessonsReports)), () => ( true)), hasEssayReports: _nullishCoalesce(reports.essayReports, () => ( true)), reports, // Score types (support both nested and flat for backwards compatibility) hasSimulatedScoreTri: _nullishCoalesce(_nullishCoalesce(simulatedScore.tri, () => ( modules.simulatedScoreTri)), () => ( false)), hasSimulatedScoreAbsoluto: _nullishCoalesce(_nullishCoalesce(simulatedScore.absoluto, () => ( modules.simulatedScoreAbsoluto)), () => ( false)), simulatedScore }; }; exports.DEFAULT_SIMULATIONS = DEFAULT_SIMULATIONS; exports.DEFAULT_EXAMS = DEFAULT_EXAMS; exports.DEFAULT_PERFORMANCE_GRAPHS = DEFAULT_PERFORMANCE_GRAPHS; exports.DEFAULT_REPORTS = DEFAULT_REPORTS; exports.DEFAULT_SIMULATED_SCORE = DEFAULT_SIMULATED_SCORE; exports.DEFAULT_MODULES = DEFAULT_MODULES; exports.mergeModulesConfig = mergeModulesConfig; exports.useModulesStore = useModulesStore; exports.useModules = useModules; //# sourceMappingURL=chunk-MJX4EXOO.js.map