From 6cfc695bbeaa82be6ad9ebf9eb84598a0442f3e3 Mon Sep 17 00:00:00 2001 From: Ivan <8611739+IRBorisov@users.noreply.github.com> Date: Thu, 6 Nov 2025 15:38:38 +0300 Subject: [PATCH] R: Prevent potential infinite loops --- .../editor-constituenta/editor-constituenta.tsx | 8 ++++++-- rsconcept/frontend/src/hooks/use-reset-on-change.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/rsconcept/frontend/src/features/rsform/pages/rsform-page/editor-constituenta/editor-constituenta.tsx b/rsconcept/frontend/src/features/rsform/pages/rsform-page/editor-constituenta/editor-constituenta.tsx index 77ab0892..5008e011 100644 --- a/rsconcept/frontend/src/features/rsform/pages/rsform-page/editor-constituenta/editor-constituenta.tsx +++ b/rsconcept/frontend/src/features/rsform/pages/rsform-page/editor-constituenta/editor-constituenta.tsx @@ -52,9 +52,13 @@ export function EditorConstituenta() { const role = useRoleStore(state => state.role); const listHeight = useFitHeight(!isNarrow ? '8.2rem' : role !== UserRole.READER ? '42rem' : '35rem', '10rem'); + const prevActiveCstId = useRef(null); useEffect(() => { - if (activeCst && selected.length !== 1) { - setSelected([activeCst.id]); + if (activeCst && prevActiveCstId.current !== activeCst.id) { + prevActiveCstId.current = activeCst.id; + if (selected.length !== 1 || selected[0] !== activeCst.id) { + setSelected([activeCst.id]); + } } }, [activeCst, selected, setSelected]); diff --git a/rsconcept/frontend/src/hooks/use-reset-on-change.ts b/rsconcept/frontend/src/hooks/use-reset-on-change.ts index 517ae1a0..ee48b7b1 100644 --- a/rsconcept/frontend/src/hooks/use-reset-on-change.ts +++ b/rsconcept/frontend/src/hooks/use-reset-on-change.ts @@ -1,9 +1,14 @@ -import { useEffect, useMemo } from 'react'; +import { useEffect, useMemo, useRef } from 'react'; export function useResetOnChange(deps: T[], resetFn: () => void) { const depsKey = useMemo(() => JSON.stringify(deps), [deps]); + const resetFnRef = useRef(resetFn); useEffect(() => { - resetFn(); - }, [depsKey, resetFn]); + resetFnRef.current = resetFn; + }, [resetFn]); + + useEffect(() => { + resetFnRef.current(); + }, [depsKey]); }