@@ -38,11 +38,44 @@ function Learn() {
3838 useEffect ( ( ) => {
3939 if ( ! jsonId ) return ;
4040
41+ // First, fetch the roadmap data to check if it has an id
4142 const existingMap = getLearningMap ( jsonId ) ;
4243
4344 if ( existingMap ) {
44- // Already have the data, just update last accessed
45- addLearningMap ( jsonId , existingMap . roadmapData ) ;
45+ // Check if the roadmap has a storage ID and handle potential conflicts
46+ const storageId = existingMap . roadmapData . settings ?. id ;
47+ if ( storageId && storageId !== jsonId ) {
48+ // There's a custom storage ID - check if a different map exists with that ID
49+ const mapWithStorageId = getLearningMap ( storageId ) ;
50+ if ( mapWithStorageId && mapWithStorageId . roadmapData !== existingMap . roadmapData ) {
51+ // Ask user if they want to replace
52+ const shouldReplace = window . confirm (
53+ `A learning map with the storage ID "${ storageId } " already exists. Would you like to replace it with this map? Your progress will not be removed.`
54+ ) ;
55+ if ( shouldReplace ) {
56+ // Keep the existing state but update the roadmap data
57+ const existingState = mapWithStorageId . state ;
58+ addLearningMap ( storageId , existingMap . roadmapData ) ;
59+ if ( existingState ) {
60+ updateState ( storageId , existingState ) ;
61+ }
62+ // Remove the old jsonId entry to avoid duplicates
63+ if ( jsonId !== storageId ) {
64+ removeLearningMap ( jsonId ) ;
65+ }
66+ }
67+ } else {
68+ // No conflict, just update
69+ addLearningMap ( storageId , existingMap . roadmapData ) ;
70+ // Remove the old jsonId entry if different
71+ if ( jsonId !== storageId ) {
72+ removeLearningMap ( jsonId ) ;
73+ }
74+ }
75+ } else {
76+ // No custom storage ID, just use jsonId
77+ addLearningMap ( jsonId , existingMap . roadmapData ) ;
78+ }
4679 return ;
4780 }
4881
@@ -57,26 +90,54 @@ function Learn() {
5790 . then ( ( r ) => r . text ( ) )
5891 . then ( ( text ) => {
5992 const json = JSON . parse ( text ) ;
60- addLearningMap ( jsonId , json ) ;
93+ const storageId = json . settings ?. id ;
94+
95+ if ( storageId && storageId !== jsonId ) {
96+ // Check if a map with this storage ID already exists
97+ const existingMapWithStorageId = getLearningMap ( storageId ) ;
98+ if ( existingMapWithStorageId ) {
99+ // Ask user if they want to replace
100+ const shouldReplace = window . confirm (
101+ `A learning map with the storage ID "${ storageId } " already exists. Would you like to replace it with this map? Your progress will not be removed.`
102+ ) ;
103+ if ( shouldReplace ) {
104+ // Keep the existing state but update the roadmap data
105+ const existingState = existingMapWithStorageId . state ;
106+ addLearningMap ( storageId , json ) ;
107+ if ( existingState ) {
108+ updateState ( storageId , existingState ) ;
109+ }
110+ } else {
111+ // User chose not to replace, just use jsonId as key
112+ addLearningMap ( jsonId , json ) ;
113+ }
114+ } else {
115+ // No conflict, use storage ID
116+ addLearningMap ( storageId , json ) ;
117+ }
118+ } else {
119+ // No custom storage ID, use jsonId
120+ addLearningMap ( jsonId , json ) ;
121+ }
61122 setLoading ( false ) ;
62123 } )
63124 . catch ( ( ) => {
64125 setError ( 'Failed to load learning map. Please check the URL and try again.' ) ;
65126 setLoading ( false ) ;
66127 } ) ;
67- } , [ jsonId , getLearningMap , addLearningMap ] ) ;
128+ } , [ jsonId , getLearningMap , addLearningMap , updateState , removeLearningMap ] ) ;
68129
69- const handleStateChange = useCallback ( ( state : RoadmapState ) => {
70- if ( jsonId ) {
130+ const handleStateChange = useCallback ( ( state : RoadmapState , key : string ) => {
131+ if ( key ) {
71132 // Debounce state updates to prevent infinite loops
72133 if ( updateTimeoutRef . current ) {
73134 clearTimeout ( updateTimeoutRef . current ) ;
74135 }
75136 updateTimeoutRef . current = setTimeout ( ( ) => {
76- updateState ( jsonId , state ) ;
137+ updateState ( key , state ) ;
77138 } , 500 ) ;
78139 }
79- } , [ jsonId , updateState ] ) ;
140+ } , [ updateState ] ) ;
80141
81142 const handleAddMap = ( ) => {
82143 // Parse URL to extract json ID
@@ -98,7 +159,23 @@ function Learn() {
98159
99160 // If there's a json ID, show the learning map
100161 if ( jsonId ) {
101- const learningMap = getLearningMap ( jsonId ) ;
162+ // First try to get by storage ID if present, otherwise use jsonId
163+ let learningMap = getLearningMap ( jsonId ) ;
164+
165+ // If not found by jsonId, check if there's a storage ID in any map
166+ if ( ! learningMap ) {
167+ const allMaps = getAllLearningMaps ( ) ;
168+ const mapWithJsonId = allMaps . find ( m => m . id === jsonId ) ;
169+ if ( mapWithJsonId ) {
170+ learningMap = mapWithJsonId ;
171+ }
172+ }
173+
174+ // Try to determine the storage key (either custom id or jsonId)
175+ const storageKey = learningMap ?. roadmapData ?. settings ?. id || jsonId ;
176+ if ( storageKey !== jsonId ) {
177+ learningMap = getLearningMap ( storageKey ) ;
178+ }
102179
103180 if ( loading ) {
104181 return (
@@ -143,10 +220,10 @@ function Learn() {
143220 </ div >
144221 </ div >
145222 < LearningMap
146- key = { jsonId }
223+ key = { storageKey }
147224 roadmapData = { learningMap . roadmapData }
148225 initialState = { learningMap . state }
149- onChange = { handleStateChange }
226+ onChange = { ( state ) => handleStateChange ( state , storageKey ) }
150227 />
151228 </ div >
152229 ) ;
0 commit comments