-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Fixes #6416: Switching a note using Sidebar is slow and gray…
…ed out (#6430)
- Loading branch information
Showing
2 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/app-desktop/gui/NoteEditor/utils/useEffectiveNoteId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { NoteEditorProps } from './types'; | ||
|
||
export default function useEffectiveNoteId(props: NoteEditorProps) { | ||
// When a notebook is changed without any selected note, | ||
// no note is selected for a moment, and then a new note gets selected. | ||
// In this short transient period, the last displayed note id should temporarily | ||
// be used to prevent NoteEditor's body from being unmounted. | ||
// See https://github.com/laurent22/joplin/issues/6416 | ||
// and https://github.com/laurent22/joplin/pull/6430 for details. | ||
|
||
const lastDisplayedNoteId = useRef<string>(null); | ||
const whenNoteIdIsTransientlyAbsent = !props.noteId && props.notes.length > 0; | ||
const effectiveNoteId = whenNoteIdIsTransientlyAbsent ? lastDisplayedNoteId.current : props.noteId; | ||
|
||
useEffect(() => { | ||
if (props.noteId) lastDisplayedNoteId.current = props.noteId; | ||
}, [props.noteId]); | ||
|
||
return effectiveNoteId; | ||
} |