Skip to content

Commit 6c389f9

Browse files
AmirMohammad CheraghaliAmirMohammad Cheraghali
authored andcommitted
fix: remove derived state from LabNotebook to prevent sync race conditions
1 parent c14298b commit 6c389f9

1 file changed

Lines changed: 8 additions & 25 deletions

File tree

src/components/dashboard/LabNotebook.tsx

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
2020

2121
const [searchQuery, setSearchQuery] = useState('');
2222

23-
// Editor State
24-
const [editTitle, setEditTitle] = useState('');
25-
const [editContent, setEditContent] = useState('');
2623
const [isSaving, setIsSaving] = useState(false);
2724
const [lastSaved, setLastSaved] = useState<Date | null>(null);
2825
const saveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
@@ -36,6 +33,8 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
3633

3734
const [showDrawerList, setShowDrawerList] = useState(isDrawer);
3835

36+
const activeNotebook = notebooks.find(n => n.id === activeId);
37+
3938
useEffect(() => {
4039
if (!user) return;
4140
const fetchNotebooks = async () => {
@@ -49,6 +48,7 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
4948
setAllStructures(structureData);
5049
if (notebookData.length > 0 && !activeId) {
5150
setActiveId(notebookData[0].id);
51+
setLastSaved(new Date(notebookData[0].updated_at));
5252
}
5353
} catch (err: any) {
5454
setError(err.message || 'Failed to load data');
@@ -66,6 +66,7 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
6666
const newEntry = await createNotebook(user.id);
6767
setNotebooks(prev => [newEntry, ...prev]);
6868
setActiveId(newEntry.id);
69+
setLastSaved(new Date(newEntry.updated_at));
6970
} catch (err: any) {
7071
setError(err.message || 'Failed to create notebook');
7172
}
@@ -85,26 +86,8 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
8586
}
8687
};
8788

88-
const activeNotebook = notebooks.find(n => n.id === activeId);
89-
90-
// Sync local editor state when active notebook changes
91-
useEffect(() => {
92-
if (activeNotebook) {
93-
setEditTitle(activeNotebook.title);
94-
setEditContent(activeNotebook.content);
95-
setLastSaved(new Date(activeNotebook.updated_at));
96-
} else {
97-
setEditTitle('');
98-
setEditContent('');
99-
setLastSaved(null);
100-
}
101-
}, [activeId]);
102-
10389
// Auto-save logic
10490
const handleEditorChange = (field: 'title' | 'content', value: string) => {
105-
if (field === 'title') setEditTitle(value);
106-
if (field === 'content') setEditContent(value);
107-
10891
// Optimistically update the list view so the sidebar reflects changes immediately
10992
setNotebooks(prev => prev.map(n => n.id === activeId ? { ...n, [field]: value, updated_at: new Date().toISOString() } : n));
11093

@@ -402,7 +385,7 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
402385
<div className={`flex-1 overflow-y-auto ${isDrawer ? 'p-4' : 'px-4 sm:px-12 py-8'} min-h-0`}>
403386
<input
404387
type="text"
405-
value={editTitle}
388+
value={activeNotebook.title}
406389
onChange={(e) => handleEditorChange('title', e.target.value)}
407390
className={`bg-transparent border-none font-bold font-serif text-[var(--text-primary)] focus:outline-none w-full mb-4 sm:mb-8 placeholder-[var(--text-muted)] ${isDrawer ? 'text-2xl' : 'text-4xl'}`}
408391
placeholder="Title..."
@@ -413,7 +396,7 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
413396
key={activeId}
414397
ref={editorRef}
415398
noteId={activeId || undefined}
416-
content={editContent}
399+
content={activeNotebook.content}
417400
onChange={(markdown) => handleEditorChange('content', markdown)}
418401
allStructures={allStructures}
419402
placeholder="Write notes... Type @ to mention a structure..."
@@ -447,8 +430,8 @@ export const LabNotebook: React.FC<{ isDrawer?: boolean }> = ({ isDrawer = false
447430
{activeNotebook && (
448431
<LabReportTemplate
449432
ref={reportRef}
450-
title={editTitle}
451-
content={editContent}
433+
title={activeNotebook.title}
434+
content={activeNotebook.content}
452435
date={activeNotebook.created_at}
453436
author={user?.email || 'Quercus User'}
454437
id={activeNotebook.id.slice(0, 8)}

0 commit comments

Comments
 (0)