Skip to content

Commit 58ef742

Browse files
committed
fix new map not added to store
1 parent b5c203d commit 58ef742

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

platforms/web/src/TeacherEditor.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,33 @@ 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 reset = useEditorStore(state => state.reset);
1617
const nodes = useEditorStore(state => state.nodes);
1718
const edges = useEditorStore(state => state.edges);
1819
const settings = useEditorStore(state => state.settings);
1920
const lastSavedRef = useRef<string | null>(null);
2021
const saveTimeoutRef = useRef<number | null>(null);
22+
const initializedRef = useRef(false);
2123

2224
// Extract jsonId from URL
2325
const jsonId = location.hash.startsWith('#json=') ? location.hash.replace('#json=', '') : null;
2426

27+
// Reset editor when creating a new map (no jsonId in URL)
28+
useEffect(() => {
29+
if (!jsonId && !initializedRef.current) {
30+
reset();
31+
initializedRef.current = true;
32+
}
33+
// Reset the flag when jsonId changes (navigating to edit an existing map)
34+
if (jsonId) {
35+
initializedRef.current = false;
36+
}
37+
}, [jsonId, reset]);
38+
2539
// Auto-save changes with debouncing
2640
useEffect(() => {
27-
if (!jsonId) return;
41+
// Skip auto-save if the map is empty (no nodes)
42+
if (nodes.length === 0) return;
2843

2944
// Clear any pending save
3045
if (saveTimeoutRef.current) {
@@ -34,15 +49,26 @@ function TeacherEditorInner() {
3449
// Debounce saves to avoid too many writes
3550
saveTimeoutRef.current = window.setTimeout(async () => {
3651
const roadmapData = getRoadmapData();
37-
const storageId = roadmapData.settings?.id || jsonId;
52+
53+
// Generate a storage ID for this map
54+
let storageId: string;
55+
if (jsonId) {
56+
// Use jsonId if available
57+
storageId = roadmapData.settings?.id || jsonId;
58+
} else {
59+
// For new maps without jsonId, use or create a unique ID
60+
storageId = roadmapData.settings?.id || `local-${Date.now()}`;
61+
// Update settings with the generated ID if it doesn't have one
62+
if (!roadmapData.settings?.id) {
63+
roadmapData.settings = { ...roadmapData.settings, id: storageId };
64+
}
65+
}
3866

3967
try {
4068
// Check if this map exists in teacher's collection
4169
const existingMap = await db.getTeacherMap(storageId);
42-
if (existingMap) {
43-
// Update existing map
44-
await db.addTeacherMap(storageId, roadmapData, existingMap.jsonId);
45-
}
70+
// Save or update the map
71+
await db.addTeacherMap(storageId, roadmapData, existingMap?.jsonId || jsonId || undefined);
4672
} catch (err) {
4773
console.error('Failed to auto-save map:', err);
4874
}

0 commit comments

Comments
 (0)