diff --git a/.eslintrc.cjs b/.eslintrc.cjs index c6a97c2e6..5701aaa77 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -84,6 +84,7 @@ module.exports = { '@typescript-eslint/no-unsafe-member-access': 'warn', '@typescript-eslint/no-unsafe-return': 'warn', '@typescript-eslint/no-unsafe-assignment': 'warn', + '@typescript-eslint/no-unused-vars': 'warn', '@typescript-eslint/member-delimiter-style': [ 'error', diff --git a/site/build/vite-build-simulation-data.config.ts b/site/build/vite-build-simulation-data.config.ts index 314fb078a..4e658facd 100644 --- a/site/build/vite-build-simulation-data.config.ts +++ b/site/build/vite-build-simulation-data.config.ts @@ -3,7 +3,7 @@ import { join, resolve } from 'path' import { defineConfig } from 'vite' -import { PageConfig } from '@/pages/simulateurs/_configs/types' +import type { PageConfig } from '@/pages/simulateurs/_configs/types' import { objectTransform } from '../source/utils' diff --git a/site/source/components/App.tsx b/site/source/components/App.tsx index dc8df2d7b..c3808a0f2 100644 --- a/site/source/components/App.tsx +++ b/site/source/components/App.tsx @@ -1,33 +1,23 @@ import { ErrorBoundary } from '@sentry/react' import { FallbackRender } from '@sentry/react/types/errorboundary' -import rules from 'modele-social' -import { ComponentProps, StrictMode, useMemo } from 'react' +import { ComponentProps, StrictMode, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { Route, Routes } from 'react-router-dom' import { css, styled } from 'styled-components' import Footer from '@/components/layout/Footer/Footer' import Header from '@/components/layout/Header' -import { - engineFactory, - EngineProvider, - Rules, - useEngine, - useSetupSafeSituation, -} from '@/components/utils/EngineContext' import { Container } from '@/design-system/layout' import { useAxeCoreAnalysis } from '@/hooks/useAxeCoreAnalysis' import { useGetFullURL } from '@/hooks/useGetFullURL' import { useIsEmbedded } from '@/hooks/useIsEmbedded' +import { useLazyPromise } from '@/hooks/usePromise' import { useSaveAndRestoreScrollPosition } from '@/hooks/useSaveAndRestoreScrollPosition' import Landing from '@/pages/_landing/Landing' import Page404 from '@/pages/404' import Accessibilité from '@/pages/Accessibilité' -import Assistants from '@/pages/assistants/index' import Budget from '@/pages/budget/index' import IntegrationTest from '@/pages/dev/IntegrationTest' -import Documentation from '@/pages/Documentation' -import Iframes from '@/pages/iframes' import Integration from '@/pages/integration/index' import Nouveautés from '@/pages/nouveautés/index' import Offline from '@/pages/Offline' @@ -36,53 +26,274 @@ import Simulateurs from '@/pages/simulateurs' import SimulateursEtAssistants from '@/pages/simulateurs-et-assistants' import Stats from '@/pages/statistiques/LazyStats' import { useSitePaths } from '@/sitePaths' +import { + useAsyncGetRule, + useAsyncParsedRules, + useLazyPromiseOnSituationChange, + usePromiseOnSituationChange, + useShallowCopy, + useWorkerEngine, +} from '@/worker/socialWorkerEngineClient' import Provider, { ProviderProps } from './Provider' import Redirections from './Redirections' type RootProps = { basename: ProviderProps['basename'] - rulesPreTransform?: (rules: Rules) => Rules + // rulesPreTransform?: (rules: Rules) => Rules +} + +const TestWorkerEngine = () => { + const [refresh, setRefresh] = useState(0) + const workerEngine = useWorkerEngine() + // const workerEngineCtx = useWorkerEngineContext() + + const [, trigger] = useLazyPromise( + async () => + workerEngine?.asyncSetSituationWithEngineId({ + SMIC: '1000€/mois', + }), + [workerEngine], + { defaultValue: 'loading...' } + ) + + const date = useAsyncGetRule('date', { defaultValue: 'loading...' }) + + const parsedRules = useAsyncParsedRules() + + const resultSmic = usePromiseOnSituationChange( + () => workerEngine.asyncEvaluateWithEngineId('SMIC'), + [workerEngine], + { defaultValue: 'loading...' } + ) + + const [resultLazySmic, triggerLazySmic] = useLazyPromiseOnSituationChange( + () => workerEngine.asyncEvaluateWithEngineId('SMIC'), + [workerEngine], + { defaultValue: 'wait 2sec...' } + ) + + useEffect(() => { + console.log('??? useEffect') + + void (async () => { + await workerEngine.isWorkerReady + setTimeout(() => { + void triggerLazySmic() + }, 3000) + })() + }, [triggerLazySmic, workerEngine.isWorkerReady]) + + const workerEngineCopy = useShallowCopy(workerEngine) + // // const workerEngineCopy = workerEngine + console.log('=========>', workerEngine, workerEngineCopy) + + const [, triggerCopy] = useLazyPromise(async () => { + // console.log('+++++++++>', workerEngineCopy) + + await workerEngineCopy?.asyncSetSituationWithEngineId({ + SMIC: '2000€/mois', + }) + }, [workerEngineCopy]) + + const dateCopy = useAsyncGetRule('date', { + defaultValue: 'loading...', + // workerEngine: workerEngineCopy, + }) + + const parsedRulesCopy = useAsyncParsedRules({ + workerEngine: workerEngineCopy, + }) + + const resultSmicCopy = usePromiseOnSituationChange( + async () => workerEngineCopy?.asyncEvaluateWithEngineId('SMIC'), + [workerEngineCopy], + { + defaultValue: 'loading...', + workerEngine: workerEngineCopy, + } + ) + + const [resultLazySmicCopy, triggerLazySmicCopy] = + useLazyPromiseOnSituationChange( + async () => workerEngineCopy?.asyncEvaluateWithEngineId('SMIC'), + [workerEngineCopy], + { + defaultValue: 'wait 2sec...', + workerEngine: workerEngineCopy, + } + ) + + useEffect(() => { + // console.log('useEffect') + + void (async () => { + await workerEngine.isWorkerReady + setTimeout(() => { + void triggerLazySmicCopy() + }, 3000) + })() + }, [triggerLazySmicCopy, workerEngine.isWorkerReady]) + + const { asyncSetSituationWithEngineId } = workerEngineCopy ?? {} + usePromiseOnSituationChange(async () => { + // console.log('**************>', workerEngineCopy, resultSmic) + + if ( + typeof resultSmic !== 'string' && + typeof resultSmic.nodeValue === 'number' + ) { + // console.log('ooooooooooooooooooo', resultSmic) + + await asyncSetSituationWithEngineId?.({ + SMIC: resultSmic.nodeValue + '€/mois', + }) + } + }, [asyncSetSituationWithEngineId, resultSmic]) + + return ( +
+

Test worker engine

+ + + + +

+ date title:{' '} + {JSON.stringify(typeof date === 'string' ? date : date?.title)} +

+

+ parsedRules length:{' '} + {JSON.stringify(Object.entries(parsedRules ?? {}).length)} +

+

+ resultSmic:{' '} + {JSON.stringify( + typeof resultSmic === 'string' ? resultSmic : resultSmic?.nodeValue + )} +

+

+ resultLazySmic:{' '} + {JSON.stringify( + typeof resultLazySmic === 'string' + ? resultLazySmic + : resultLazySmic?.nodeValue + )} +

+ +

workerEngineCopy: {JSON.stringify(workerEngineCopy?.engineId)}

+ +

+ dateCopy title:{' '} + {JSON.stringify( + typeof dateCopy === 'string' ? dateCopy : dateCopy?.title + )} +

+

+ parsedRulesCopy length:{' '} + {JSON.stringify(Object.entries(parsedRulesCopy ?? {}).length)} +

+

+ resultSmicCopy:{' '} + {JSON.stringify( + typeof resultSmicCopy === 'string' + ? resultSmicCopy + : resultSmicCopy?.nodeValue + )} +

+

+ resultLazySmicCopy:{' '} + {JSON.stringify( + typeof resultLazySmicCopy === 'string' + ? resultLazySmicCopy + : resultLazySmicCopy?.nodeValue + )} +

+
+ ) } export default function Root({ basename, - rulesPreTransform = (r) => r, -}: RootProps) { - const engine = useMemo( - () => engineFactory(rulesPreTransform(rules)), +}: // rulesPreTransform = (r) => r, +RootProps) { + // const situationVersion = useCreateWorkerEngine(basename) + // const engine = useMemo( + // () => engineFactory(rulesPreTransform(rules)), - // We need to keep [rules] in the dependency list for hot reload of the rules - // in dev mode, even if ESLint think it is unnecessary since `rules` isn't - // defined in the component scope. - // - // eslint-disable-next-line react-hooks/exhaustive-deps - [rules] - ) + // // We need to keep [rules] in the dependency list for hot reload of the rules + // // in dev mode, even if ESLint think it is unnecessary since `rules` isn't + // // defined in the component scope. + // // + // // eslint-disable-next-line react-hooks/exhaustive-deps + // [rules] + // ) return ( - - - - - - - + {/* */} + + + + + + {/* */} ) } - const Router = () => { - const engine = useEngine() + /* + const exampleSyncValue = usePromiseOnSituationChange( + () => asyncEvaluate('SMIC'), + [] + )?.nodeValue - useSetupSafeSituation(engine) + const exampleSyncValueWithDefault = usePromiseOnSituationChange( + async () => (await asyncEvaluate('SMIC')).nodeValue, + [], + 'loading...' + ) + + const [exampleAsyncValue, fireEvaluate] = useLazyPromise( + async (param: PublicodesExpression) => + (await asyncEvaluate(param)).nodeValue, + [], + 42 + ) + + usePromise(async () => { + let count = 0 + const interval = setInterval(() => { + void fireEvaluate(count++ % 2 === 0 ? 'date' : 'SMIC') + if (count === 7) clearInterval(interval) + }, 1000) + + await new Promise((resolve) => setTimeout(resolve, 3000)) + await asyncSetSituation({ date: '01/01/2022' }) + await new Promise((resolve) => setTimeout(resolve, 3000)) + await asyncSetSituation({ date: '01/01/2021' }) + await new Promise((resolve) => setTimeout(resolve, 3000)) + }, [fireEvaluate]) + + */ return ( - - } /> - } /> - + <> + {/* exemple sans valeur par defaut : {JSON.stringify(exampleSyncValue)} +
+ exemple avec valeur par defaut :{' '} + {JSON.stringify(exampleSyncValueWithDefault)}
+ exemple d'execution manuel : {JSON.stringify(exampleAsyncValue)} */} + {/* */} + + } /> + + {/* } /> */} + } /> + + ) } @@ -96,11 +307,8 @@ const CatchOffline = ({ error }: ComponentProps) => { const App = () => { const { relativeSitePaths } = useSitePaths() - const { t } = useTranslation() - const fullURL = useGetFullURL() - useSaveAndRestoreScrollPosition() const isEmbedded = useIsEmbedded() if (!import.meta.env.PROD && import.meta.env.VITE_AXE_CORE_ENABLED) { @@ -108,7 +316,6 @@ const App = () => { useAxeCoreAnalysis() } const documentationPath = useSitePaths().absoluteSitePaths.documentation.index - const engine = useEngine() return ( @@ -137,10 +344,10 @@ const App = () => { } /> - } - /> + /> */} } @@ -149,15 +356,15 @@ const App = () => { path={relativeSitePaths.simulateursEtAssistants + '/*'} element={} /> - - } - /> + {/* + } + /> */} } @@ -172,12 +379,10 @@ const App = () => { path={relativeSitePaths.accessibilité} element={} /> - } /> - } /> } /> @@ -185,7 +390,6 @@ const App = () => { - {!isEmbedded &&