@@ -34,7 +34,6 @@ import { cn } from '@/lib/utils';
3434import { getDisplayMessage , parseAPIError } from '@/utils/api' ;
3535import { CradleEditor } from '@/utils/editor/enhancements' ;
3636import extractHeaderHierarchy , { HeaderNode } from '@/utils/editor/outline' ;
37- import { logger } from '@/utils/logger' ;
3837import { Prec } from '@codemirror/state' ;
3938import { keymap } from '@codemirror/view' ;
4039import { BookOpenIcon , InfoIcon , PencilSimpleIcon } from '@phosphor-icons/react' ;
@@ -313,7 +312,6 @@ export default function NoteViewer() {
313312 setEnrichmentDialogOpen ( true ) ;
314313 } , [ editorUtils ] ) ;
315314
316- // Query for note metadata
317315 const {
318316 data : noteData ,
319317 isLoading,
@@ -340,18 +338,22 @@ export default function NoteViewer() {
340338 enableEditingRef . current = { enableEditing, router, location, search } ;
341339 } , [ enableEditing , router , location , search ] ) ;
342340
343- // Update state when note data is loaded
341+ const editorDraftRef = useRef ( { markdownContent : '' , initialMarkdown : '' } ) ;
342+ editorDraftRef . current = { markdownContent, initialMarkdown } ;
343+
344+ const prevNoteIdForDetailRef = useRef < string | null > ( null ) ;
345+
344346 useEffect ( ( ) => {
345347 if ( ! noteData ) {
346348 setNote ( null ) ;
347349 setIsFleeting ( false ) ;
348350 setFileData ( [ ] ) ;
349351 setMarkdownContent ( '' ) ;
350352 setInitialMarkdown ( '' ) ;
353+ prevNoteIdForDetailRef . current = null ;
351354 return ;
352355 }
353356
354- logger . info ( 'NoteViewer - Note loaded successfully' , { noteData } ) ;
355357 setNote ( noteData ) ;
356358 const nextIsFleeting = Boolean ( noteData . fleeting ) ;
357359 setIsFleeting ( nextIsFleeting ) ;
@@ -363,13 +365,24 @@ export default function NoteViewer() {
363365 replace : true ,
364366 } ) ;
365367 }
366- setMarkdownContent ( noteData . content ) ;
367- setInitialMarkdown ( noteData . content ) ;
368+
369+ const switchedNote = prevNoteIdForDetailRef . current !== noteId ;
370+ prevNoteIdForDetailRef . current = noteId ;
371+
372+ const { markdownContent : md , initialMarkdown : init } = editorDraftRef . current ;
373+ const applyServerBody =
374+ switchedNote ||
375+ ( md === init && noteData . content !== md ) ;
376+
377+ if ( applyServerBody ) {
378+ setMarkdownContent ( noteData . content ) ;
379+ setInitialMarkdown ( noteData . content ) ;
380+ setHasUnsavedChanges ( false ) ;
381+ }
382+
368383 setFileData ( noteData . files || EMPTY_FILES ) ;
369- setHasUnsavedChanges ( false ) ;
370- } , [ noteData ] ) ;
384+ } , [ noteData , noteId ] ) ;
371385
372- // Mutation for deleting note
373386 const deleteMutation = useMutation ( {
374387 mutationFn : async ( ) => {
375388 const { error, response } = await fetchClient . DELETE ( '/notes/{note_id}/' , {
@@ -386,14 +399,8 @@ export default function NoteViewer() {
386399 } ,
387400 } ) ;
388401
389- // Use a ref to store the latest values for the save function
390- const saveDataRef = useRef ( { markdownContent } ) ;
391402 const lastSaveFailedRef = useRef ( false ) ;
392403
393- useEffect ( ( ) => {
394- saveDataRef . current = { markdownContent } ;
395- } , [ markdownContent ] ) ;
396-
397404 useEffect ( ( ) => {
398405 lastSaveFailedRef . current = false ;
399406 } , [ noteId ] ) ;
@@ -422,7 +429,7 @@ export default function NoteViewer() {
422429 async ( showAlert = false ) => {
423430 if ( ! noteId ) return ;
424431
425- const { markdownContent : content } = saveDataRef . current ;
432+ const content = editorDraftRef . current . markdownContent ;
426433
427434 if ( ! content || content . trim ( ) . length === 0 ) {
428435 toast . error ( 'Cannot save empty note.' ) ;
0 commit comments