@@ -21,6 +21,7 @@ function TeacherEditorInner() {
2121 const lastSavedRef = useRef < string | null > ( null ) ;
2222 const saveTimeoutRef = useRef < number | null > ( null ) ;
2323 const initializedRef = useRef ( false ) ;
24+ const currentDbIdRef = useRef < string | null > ( null ) ; // Track the current database ID
2425
2526 // Extract id from URL - supports both #id= and #json= for backwards compatibility
2627 const urlHash = location . hash . slice ( 1 ) ; // Remove the #
@@ -34,6 +35,7 @@ function TeacherEditorInner() {
3435 // No ID in URL - reset for new map creation
3536 if ( ! initializedRef . current ) {
3637 reset ( ) ;
38+ currentDbIdRef . current = null ; // Reset database ID for new map
3739 initializedRef . current = true ;
3840 }
3941 return ;
@@ -45,6 +47,7 @@ function TeacherEditorInner() {
4547 const teacherMap = await db . getTeacherMap ( mapId ) ;
4648 if ( teacherMap ) {
4749 loadRoadmapData ( teacherMap . roadmapData ) ;
50+ currentDbIdRef . current = mapId ; // Set the database ID
4851 initializedRef . current = true ;
4952 }
5053 } catch ( err ) {
@@ -60,6 +63,7 @@ function TeacherEditorInner() {
6063 // Reset the flag when mapId changes (navigating to a different map)
6164 return ( ) => {
6265 initializedRef . current = false ;
66+ currentDbIdRef . current = null ;
6367 } ;
6468 } , [ mapId , reset , loadRoadmapData ] ) ;
6569
@@ -79,25 +83,18 @@ function TeacherEditorInner() {
7983
8084 // Determine the database ID for this map
8185 let dbId : string ;
82- if ( mapId ) {
83- // Editing existing map - use the database ID from URL
84- dbId = mapId ;
86+
87+ if ( currentDbIdRef . current ) {
88+ // We already have a database ID for this editing session - use it
89+ dbId = currentDbIdRef . current ;
8590 } else {
86- // New map - check if we already created a database entry
87- // Look for existing map with matching settings.id
88- const settingsId = roadmapData . settings ?. id ;
89- const existingMap = settingsId ? await db . findTeacherMapBySettingsId ( settingsId ) : undefined ;
91+ // New map - generate a new database ID and remember it
92+ dbId = `map-${ Date . now ( ) } ` ;
93+ currentDbIdRef . current = dbId ;
9094
91- if ( existingMap ) {
92- // Already saved - update existing
93- dbId = existingMap . id ;
94- } else {
95- // First save - generate new database ID
96- dbId = `map-${ Date . now ( ) } ` ;
97- // Ensure settings.id is set
98- if ( ! roadmapData . settings ?. id ) {
99- roadmapData . settings = { ...roadmapData . settings , id : `map-${ Date . now ( ) } ` } ;
100- }
95+ // Ensure settings.id is set
96+ if ( ! roadmapData . settings ?. id ) {
97+ roadmapData . settings = { ...roadmapData . settings , id : dbId } ;
10198 }
10299 }
103100
@@ -116,7 +113,7 @@ function TeacherEditorInner() {
116113 clearTimeout ( saveTimeoutRef . current ) ;
117114 }
118115 } ;
119- } , [ nodes , edges , settings , mapId , getRoadmapData ] ) ;
116+ } , [ nodes , edges , settings , getRoadmapData ] ) ;
120117
121118 // When a map is shared, save it to the teacher's collection
122119 useEffect ( ( ) => {
@@ -133,17 +130,15 @@ function TeacherEditorInner() {
133130 // Get current roadmap data and save to teacher collection
134131 const saveSharedMap = async ( ) => {
135132 const roadmapData = getRoadmapData ( ) ;
136- const settingsId = roadmapData . settings ?. id ;
137133
138- // Find existing map by database ID or settings ID
134+ // Use the current database ID from this editing session
139135 let dbId : string ;
140- if ( mapId ) {
141- // Editing existing map - use the database ID from URL
142- dbId = mapId ;
136+ if ( currentDbIdRef . current ) {
137+ dbId = currentDbIdRef . current ;
143138 } else {
144- // New map - look for existing by settings.id
145- const existingMap = settingsId ? await db . findTeacherMapBySettingsId ( settingsId ) : undefined ;
146- dbId = existingMap ?. id || `map- ${ Date . now ( ) } ` ;
139+ // New map that hasn't been saved yet - create database ID
140+ dbId = `map- ${ Date . now ( ) } ` ;
141+ currentDbIdRef . current = dbId ;
147142 }
148143
149144 await db . addTeacherMap ( dbId , roadmapData , jsonId ) ;
@@ -152,7 +147,7 @@ function TeacherEditorInner() {
152147 saveSharedMap ( ) ;
153148 }
154149 }
155- } , [ shareLink , shareDialogOpen , getRoadmapData , mapId ] ) ;
150+ } , [ shareLink , shareDialogOpen , getRoadmapData ] ) ;
156151
157152 return null ;
158153}
0 commit comments