1- import { useEffect , useState } from 'react' ;
1+ import { useEffect , useState , useCallback , useRef } from 'react' ;
22import { useNavigate , useLocation } from 'react-router-dom' ;
33import { LearningMap } from '@learningmap/learningmap' ;
44import type { RoadmapState } from '@learningmap/learningmap' ;
@@ -12,6 +12,7 @@ function Learn() {
1212 const [ loading , setLoading ] = useState ( false ) ;
1313 const [ error , setError ] = useState < string | null > ( null ) ;
1414 const [ newMapUrl , setNewMapUrl ] = useState ( '' ) ;
15+ const updateTimeoutRef = useRef < number | null > ( null ) ;
1516
1617 const {
1718 addLearningMap,
@@ -64,11 +65,17 @@ function Learn() {
6465 } ) ;
6566 } , [ jsonId , getLearningMap , addLearningMap ] ) ;
6667
67- const handleStateChange = ( state : RoadmapState ) => {
68+ const handleStateChange = useCallback ( ( state : RoadmapState ) => {
6869 if ( jsonId ) {
69- updateState ( jsonId , state ) ;
70+ // Debounce state updates to prevent infinite loops
71+ if ( updateTimeoutRef . current ) {
72+ clearTimeout ( updateTimeoutRef . current ) ;
73+ }
74+ updateTimeoutRef . current = setTimeout ( ( ) => {
75+ updateState ( jsonId , state ) ;
76+ } , 500 ) ;
7077 }
71- } ;
78+ } , [ jsonId , updateState ] ) ;
7279
7380 const handleAddMap = ( ) => {
7481 // Parse URL to extract json ID
@@ -120,6 +127,7 @@ function Learn() {
120127 ) ;
121128 }
122129
130+
123131 return (
124132 < div className = "learn-container" >
125133 < div className = "learn-header" >
@@ -129,6 +137,7 @@ function Learn() {
129137 < h1 > { learningMap . roadmapData . settings ?. title || 'Learning Map' } </ h1 >
130138 </ div >
131139 < LearningMap
140+ key = { jsonId }
132141 roadmapData = { learningMap . roadmapData }
133142 initialState = { learningMap . state }
134143 onChange = { handleStateChange }
0 commit comments