From 00f26f11f2524102177bade03ecca8510ea14db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Thu, 7 Sep 2023 17:12:20 +0200 Subject: [PATCH] Fix infinite loop --- .../ChiffreAffairesActivitéMixte.tsx | 4 ++- .../components/Simulation/Questions.tsx | 12 ++++--- site/source/components/Simulation/index.tsx | 9 +++-- .../components/conversation/AnswerList.tsx | 36 +++++++------------ .../conversation/MulipleChoicesInput.tsx | 23 +++++++----- .../components/conversation/RuleInput.tsx | 22 +++--------- .../conversation/useNavigateQuestions.ts | 4 ++- site/source/hooks/useNextQuestion.tsx | 11 +++--- 8 files changed, 54 insertions(+), 67 deletions(-) diff --git a/site/source/components/ChiffreAffairesActivitéMixte.tsx b/site/source/components/ChiffreAffairesActivitéMixte.tsx index 5c9ec8085..7ec75e2a3 100644 --- a/site/source/components/ChiffreAffairesActivitéMixte.tsx +++ b/site/source/components/ChiffreAffairesActivitéMixte.tsx @@ -196,7 +196,9 @@ function ActivitéMixte() { Activité mixte - {rule && } + {rule && ( + + )} ) diff --git a/site/source/components/Simulation/Questions.tsx b/site/source/components/Simulation/Questions.tsx index cf2d8c96b..671fd34b3 100644 --- a/site/source/components/Simulation/Questions.tsx +++ b/site/source/components/Simulation/Questions.tsx @@ -1,5 +1,4 @@ -import { DottedName } from 'modele-social' -import Engine from 'publicodes' +import { WorkerEngine } from '@publicodes/worker-react' import { Trans } from 'react-i18next' import { styled } from 'styled-components' @@ -27,10 +26,10 @@ const Notice = styled(Body)` export function Questions({ customEndMessages, - engines, + workerEngines, }: { customEndMessages?: ConversationProps['customEndMessages'] - engines?: Array> + workerEngines?: WorkerEngine[] }) { const { numberCurrentStep, numberSteps } = useSimulationProgress() @@ -47,7 +46,10 @@ export function Questions({ )} - + ) diff --git a/site/source/components/Simulation/index.tsx b/site/source/components/Simulation/index.tsx index 9e0834931..173dd6be8 100644 --- a/site/source/components/Simulation/index.tsx +++ b/site/source/components/Simulation/index.tsx @@ -1,5 +1,4 @@ -import { DottedName } from 'modele-social' -import Engine from 'publicodes' +import { WorkerEngine } from '@publicodes/worker-react' import React from 'react' import { Trans, useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' @@ -29,7 +28,7 @@ export { SimulationGoals } from './SimulationGoals' type SimulationProps = { explanations?: React.ReactNode - engines?: Array> + workerEngines?: WorkerEngine[] results?: React.ReactNode children?: React.ReactNode afterQuestionsSlot?: React.ReactNode @@ -57,7 +56,7 @@ export default function Simulation({ afterQuestionsSlot, customEndMessages, showQuestionsFromBeginning, - engines, + workerEngines, hideDetails = false, fullWidth, id, @@ -83,7 +82,7 @@ export default function Simulation({ {results} diff --git a/site/source/components/conversation/AnswerList.tsx b/site/source/components/conversation/AnswerList.tsx index b15d3a8b7..0887152de 100644 --- a/site/source/components/conversation/AnswerList.tsx +++ b/site/source/components/conversation/AnswerList.tsx @@ -1,4 +1,4 @@ -import { useWorkerEngine } from '@publicodes/worker-react' +import { usePromise, useWorkerEngine } from '@publicodes/worker-react' import { DottedName } from 'modele-social' import { PublicodesExpression, RuleNode, utils } from 'publicodes' import { useCallback, useMemo } from 'react' @@ -17,7 +17,6 @@ import { Link } from '@/design-system/typography/link' import { Body, Intro } from '@/design-system/typography/paragraphs' import { useCurrentSimulatorData } from '@/hooks/useCurrentSimulatorData' import { useNextQuestions } from '@/hooks/useNextQuestion' -import { usePromise } from '@/hooks/usePromise' import { answerQuestion, resetSimulation } from '@/store/actions/actions' import { resetCompany } from '@/store/actions/companyActions' import { isCompanyDottedName } from '@/store/reducers/companySituationReducer' @@ -79,30 +78,21 @@ export default function AnswerList({ onClose, children }: AnswerListProps) { const situationQuestions = useMemo( () => answeredAndPassedQuestions.filter( - ({ dottedName }) => !isCompanyDottedName(dottedName) + ({ dottedName }) => !isCompanyDottedName(dottedName as DottedName) ), [answeredAndPassedQuestions] ) - const companyQuestions = usePromise( - () => - Promise.all( - Array.from( - new Set( - ( - [ - ...answeredAndPassedQuestions.map( - ({ dottedName }) => dottedName - ), - ...Object.keys(situation), - ...Object.keys(companySituation), - ] as Array - ).filter(isCompanyDottedName) - ) - ).map((dottedName) => workerEngine.getRule(dottedName)) - ), - [answeredAndPassedQuestions, companySituation, situation, workerEngine], - [] as RuleNode[] - ) + const companyQuestions = Array.from( + new Set( + ( + [ + ...answeredAndPassedQuestions.map(({ dottedName }) => dottedName), + ...Object.keys(situation), + ...Object.keys(companySituation), + ] as Array + ).filter(isCompanyDottedName) + ) + ).map((dottedName) => workerEngine.getRule(dottedName)) const siret = usePromise( async () => diff --git a/site/source/components/conversation/MulipleChoicesInput.tsx b/site/source/components/conversation/MulipleChoicesInput.tsx index 5d659e485..b975ace26 100644 --- a/site/source/components/conversation/MulipleChoicesInput.tsx +++ b/site/source/components/conversation/MulipleChoicesInput.tsx @@ -1,4 +1,8 @@ -import { useWorkerEngine, WorkerEngine } from '@publicodes/worker-react' +import { + usePromise, + useWorkerEngine, + WorkerEngine, +} from '@publicodes/worker-react' import { DottedName } from 'modele-social' import { PublicodesExpression, RuleNode } from 'publicodes' import { Fragment } from 'react' @@ -6,14 +10,13 @@ import { useTranslation } from 'react-i18next' import { Checkbox } from '@/design-system' import { Emoji } from '@/design-system/emoji' -import { usePromise } from '@/hooks/usePromise' import { ExplicableRule } from './Explicable' import { InputProps, RuleWithMultiplePossibilities } from './RuleInput' export function MultipleChoicesInput( props: Omit, 'onChange'> & { - engineId: number + // engineId: number onChange: (value: PublicodesExpression, name: DottedName) => void } ) { @@ -21,7 +24,7 @@ export function MultipleChoicesInput( const workerEngine = useWorkerEngine() const choices = getMultiplePossibilitiesOptions( workerEngine, - engineId, + // engineId, dottedName ) @@ -47,7 +50,7 @@ export function MultipleChoicesInput( onChange={(isSelected) => void handleChange(isSelected, node.dottedName) } - engineId={engineId} + // engineId={engineId} /> ))} @@ -57,10 +60,14 @@ export function MultipleChoicesInput( type CheckBoxRuleProps = { node: RuleNode - engineId: number + // engineId: number onChange: (isSelected: boolean) => void } -function CheckBoxRule({ node, engineId, onChange }: CheckBoxRuleProps) { +function CheckBoxRule({ + node, + // engineId, + onChange, +}: CheckBoxRuleProps) { const workerEngine = useWorkerEngine() const evaluation = usePromise( @@ -95,7 +102,7 @@ function CheckBoxRule({ node, engineId, onChange }: CheckBoxRuleProps) { function getMultiplePossibilitiesOptions( workerEngine: WorkerEngine, - engineId: number, + // engineId: number, // engine: Engine, dottedName: DottedName ): RuleNode[] { diff --git a/site/source/components/conversation/RuleInput.tsx b/site/source/components/conversation/RuleInput.tsx index 2a412c99d..429165921 100644 --- a/site/source/components/conversation/RuleInput.tsx +++ b/site/source/components/conversation/RuleInput.tsx @@ -63,7 +63,7 @@ export type InputProps = Omit< value: EvaluatedNode['nodeValue'] onChange: (value: PublicodesExpression | undefined) => void // engine: Engine - engineId: number + // engineId: number } export const binaryQuestion = [ @@ -84,7 +84,7 @@ export default function RuleInput({ missing, inputType, modifiers, - engineId = 0, + // engineId = 0, // TODO engine id is in worker engine so we need to accept workerEngine from props ...props }: Props) { // const defaultEngine = useContext(EngineContext) @@ -109,7 +109,7 @@ export default function RuleInput({ const value = evaluation?.nodeValue const isMultipleChoices = - rule && isMultiplePossibilities(workerEngine, engineId, dottedName) + rule && isMultiplePossibilities(workerEngine, dottedName) const choice = usePromise( () => getOnePossibilityOptions(workerEngine, dottedName), @@ -132,7 +132,7 @@ export default function RuleInput({ question: rule.rawNode.question, suggestions: showSuggestions ? rule.suggestions : {}, // engine: engineValue, - engineId, + // engineId, ...props, // Les espaces ne sont pas autorisés dans un id, les points sont assimilés à une déclaration de class CSS par Cypress id: props?.id?.replace(/\s|\.]/g, '_') ?? dottedName.replace(/\s|\./g, '_'), @@ -143,7 +143,7 @@ export default function RuleInput({ return ( ) @@ -245,11 +245,6 @@ const getOnePossibilityOptions = async ( ): Promise => { const node = workerEngine.getRule(path) - // if (path === 'entreprise . activité . nature') debugger - - if (!node) { - throw new Error(`La règle ${path} est introuvable`) - } const variant = isOnePossibility(node) const canGiveUp = variant && @@ -265,8 +260,6 @@ const getOnePossibilityOptions = async ( ( variant.explanation as (ASTNode & { nodeKind: 'reference' })[] ).map(async (explanation) => { - console.log('=>>>>', explanation) - const evaluate = await workerEngine.asyncEvaluate(explanation) return evaluate.nodeValue !== null @@ -293,13 +286,8 @@ export type RuleWithMultiplePossibilities = RuleNode & { function isMultiplePossibilities( workerEngine: WorkerEngine, - engineId: number, - // Engine, dottedName: DottedName ): boolean { - // return !!(engine.getRule(dottedName) as RuleWithMultiplePossibilities) - // .rawNode['plusieurs possibilités'] - return !!(workerEngine.getRule(dottedName) as RuleWithMultiplePossibilities) .rawNode['plusieurs possibilités'] } diff --git a/site/source/components/conversation/useNavigateQuestions.ts b/site/source/components/conversation/useNavigateQuestions.ts index 3a7e2ad8b..0d5444763 100644 --- a/site/source/components/conversation/useNavigateQuestions.ts +++ b/site/source/components/conversation/useNavigateQuestions.ts @@ -1,4 +1,5 @@ import { WorkerEngine } from '@publicodes/worker-react' +import { DottedName } from 'modele-social' import { useCallback, useEffect, useRef } from 'react' import { useDispatch, useSelector } from 'react-redux' @@ -48,7 +49,8 @@ export function useNavigateQuestions(workerEngines?: WorkerEngine[]) { }, [currentQuestion, dispatch, nextQuestions]) return { - currentQuestion: currentQuestion ?? nextQuestions[0], + currentQuestion: + currentQuestion ?? (nextQuestions[0] as DottedName | undefined), currentQuestionIsAnswered, goToPrevious, goToNext, diff --git a/site/source/hooks/useNextQuestion.tsx b/site/source/hooks/useNextQuestion.tsx index 9d75e45ab..1c95c160c 100644 --- a/site/source/hooks/useNextQuestion.tsx +++ b/site/source/hooks/useNextQuestion.tsx @@ -74,7 +74,7 @@ export const useNextQuestions = function ( const workerEngine = useWorkerEngine() const missingVariables = useMissingVariables(workerEngines) - return useMemo(() => { + const nextQuestions = useMemo(() => { const next = getNextQuestions( missingVariables, config.questions ?? {}, @@ -83,13 +83,10 @@ export const useNextQuestions = function ( const rules = next.map((question) => workerEngine.getRule(question)) - const nextQuestions = next.filter( - (_, i) => rules[i].rawNode.question !== undefined - ) - console.log('nextQuestions', nextQuestions) + return next.filter((_, i) => rules[i].rawNode.question !== undefined) + }, [missingVariables, config.questions, answeredQuestions, workerEngine]) - return nextQuestions - }, [answeredQuestions, config, missingVariables]) + return nextQuestions } export function useSimulationProgress(): {