@@ -13,6 +13,7 @@ function TeacherEditorInner() {
1313 const shareLink = useEditorStore ( state => state . shareLink ) ;
1414 const shareDialogOpen = useEditorStore ( state => state . shareDialogOpen ) ;
1515 const getRoadmapData = useEditorStore ( state => state . getRoadmapData ) ;
16+ const loadRoadmapData = useEditorStore ( state => state . loadRoadmapData ) ;
1617 const reset = useEditorStore ( state => state . reset ) ;
1718 const nodes = useEditorStore ( state => state . nodes ) ;
1819 const edges = useEditorStore ( state => state . edges ) ;
@@ -21,20 +22,46 @@ function TeacherEditorInner() {
2122 const saveTimeoutRef = useRef < number | null > ( null ) ;
2223 const initializedRef = useRef ( false ) ;
2324
24- // Extract jsonId from URL
25- const jsonId = location . hash . startsWith ( '#json=' ) ? location . hash . replace ( '#json=' , '' ) : null ;
25+ // Extract id from URL - supports both #id= and #json= for backwards compatibility
26+ const urlHash = location . hash . slice ( 1 ) ; // Remove the #
27+ const idMatch = urlHash . match ( / i d = ( [ ^ & ] + ) / ) ;
28+ const jsonMatch = urlHash . match ( / j s o n = ( [ ^ & ] + ) / ) ;
29+ const mapId = idMatch ? idMatch [ 1 ] : jsonMatch ? jsonMatch [ 1 ] : null ;
2630
27- // Reset editor when creating a new map (no jsonId in URL)
31+ // Load map from database when editing an existing map
2832 useEffect ( ( ) => {
29- if ( ! jsonId && ! initializedRef . current ) {
30- reset ( ) ;
33+ if ( ! mapId ) {
34+ // No ID in URL - reset for new map creation
35+ if ( ! initializedRef . current ) {
36+ reset ( ) ;
37+ initializedRef . current = true ;
38+ }
39+ return ;
40+ }
41+
42+ // Load map from database
43+ const loadMap = async ( ) => {
44+ try {
45+ const teacherMap = await db . getTeacherMap ( mapId ) ;
46+ if ( teacherMap ) {
47+ loadRoadmapData ( teacherMap . roadmapData ) ;
48+ initializedRef . current = true ;
49+ }
50+ } catch ( err ) {
51+ console . error ( 'Failed to load map:' , err ) ;
52+ }
53+ } ;
54+
55+ if ( ! initializedRef . current ) {
56+ loadMap ( ) ;
3157 initializedRef . current = true ;
3258 }
33- // Reset the flag when jsonId changes (navigating to edit an existing map)
34- if ( jsonId ) {
59+
60+ // Reset the flag when mapId changes (navigating to a different map)
61+ return ( ) => {
3562 initializedRef . current = false ;
36- }
37- } , [ jsonId , reset ] ) ;
63+ } ;
64+ } , [ mapId , reset , loadRoadmapData ] ) ;
3865
3966 // Auto-save changes with debouncing
4067 useEffect ( ( ) => {
@@ -52,11 +79,11 @@ function TeacherEditorInner() {
5279
5380 // Generate a storage ID for this map
5481 let storageId : string ;
55- if ( jsonId ) {
56- // Use jsonId if available
57- storageId = roadmapData . settings ?. id || jsonId ;
82+ if ( mapId ) {
83+ // Use mapId if available (editing existing map)
84+ storageId = roadmapData . settings ?. id || mapId ;
5885 } else {
59- // For new maps without jsonId , use or create a unique ID
86+ // For new maps without mapId , use or create a unique ID
6087 storageId = roadmapData . settings ?. id || `local-${ Date . now ( ) } ` ;
6188 // Update settings with the generated ID if it doesn't have one
6289 if ( ! roadmapData . settings ?. id ) {
@@ -68,7 +95,7 @@ function TeacherEditorInner() {
6895 // Check if this map exists in teacher's collection
6996 const existingMap = await db . getTeacherMap ( storageId ) ;
7097 // Save or update the map
71- await db . addTeacherMap ( storageId , roadmapData , existingMap ?. jsonId || jsonId || undefined ) ;
98+ await db . addTeacherMap ( storageId , roadmapData , existingMap ?. jsonId || undefined ) ;
7299 } catch ( err ) {
73100 console . error ( 'Failed to auto-save map:' , err ) ;
74101 }
@@ -79,7 +106,7 @@ function TeacherEditorInner() {
79106 clearTimeout ( saveTimeoutRef . current ) ;
80107 }
81108 } ;
82- } , [ nodes , edges , settings , jsonId , getRoadmapData ] ) ;
109+ } , [ nodes , edges , settings , mapId , getRoadmapData ] ) ;
83110
84111 // When a map is shared, save it to the teacher's collection
85112 useEffect ( ( ) => {
0 commit comments