Compare commits

...

2 Commits

Author SHA1 Message Date
Ammar Ahmed
0ecdbe5ff0 web: fix multiple notes created on typing new note 2025-03-11 13:06:45 +05:00
Ammar Ahmed
79c71f1cc6 web: fix editor rerendering on session type change from new to default 2025-03-10 14:08:27 +05:00
2 changed files with 28 additions and 15 deletions

View File

@@ -224,13 +224,14 @@ export default function TabsView() {
);
}
const MemoizedEditorView = React.memo(
EditorView,
(prev, next) =>
const MemoizedEditorView = React.memo(EditorView, (prev, next) => {
return (
prev.session.id === next.session.id &&
prev.session.type === next.session.type &&
prev.session.needsHydration === next.session.needsHydration
);
(prev.session.type === next.session.type ||
(prev.session.type === "new" && next.session.type === "default")) &&
!!prev.session.needsHydration === !!next.session.needsHydration
);
});
function EditorView({
session
}: {
@@ -251,10 +252,14 @@ function EditorView({
const event = db.eventManager.subscribe(
EVENTS.syncItemMerged,
async (item?: MaybeDeletedItem<Item>) => {
const defaultSession = session as DefaultEditorSession;
const sessionType = useEditorStore
.getState()
.getSession(defaultSession.id)?.type;
if (
session.type === "new" ||
sessionType === "new" ||
!editor ||
!session.note ||
!defaultSession.note ||
!item ||
isDeleted(item) ||
(item.type !== "tiptap" && item.type !== "note") ||
@@ -265,8 +270,9 @@ function EditorView({
}
const isContent =
item.type === "tiptap" && item.noteId === session.note.id;
const isNote = item.type === "note" && item.id === session.note.id;
item.type === "tiptap" && item.noteId === defaultSession.note.id;
const isNote =
item.type === "note" && item.id === defaultSession.note.id;
if (isContent && lastChangedTime.current < item.dateModified) {
if (!item.locked) return editor.updateContent(item.data);
@@ -275,7 +281,7 @@ function EditorView({
.catch(() => EV.publish(EVENTS.vaultLocked));
if (!result) return;
editor.updateContent(result.data);
} else if (isNote && session.note.title !== item.title) {
} else if (isNote && defaultSession.note.title !== item.title) {
AppEventManager.publish(AppEvents.changeNoteTitle, {
sessionId: session.id,
title: item.title,

View File

@@ -910,8 +910,8 @@ class EditorStore extends BaseStore<EditorStore> {
ignoreEdit?: boolean;
}
) => {
const currentSession = this.getSession(id, ["new", "default"]);
if (!currentSession) return;
const session = this.getSession(id, ["new", "default"]);
if (!session) return;
// do not allow saving of readonly session
if (partial.note?.readonly) return;
@@ -919,6 +919,13 @@ class EditorStore extends BaseStore<EditorStore> {
await saveMutex.runExclusive(async () => {
this.setSaveState(id, 0);
try {
// Get session again as it might have changed to default.
const currentSession =
session.type === "new"
? this.getSession(id, ["new", "default"])
: session;
if (!currentSession) return;
const sessionId = getSessionId(currentSession);
let noteId =
"note" in currentSession ? currentSession.note.id : partial.note?.id;
@@ -1041,8 +1048,8 @@ class EditorStore extends BaseStore<EditorStore> {
this.setSaveState(id, SaveState.NotSaved);
console.error(err);
if (err instanceof Error) logger.error(err);
if (isLockedSession(currentSession) && "note" in currentSession) {
this.get().openSession(currentSession.note, { force: true });
if (isLockedSession(session) && "note" in session) {
this.get().openSession(session.note, { force: true });
}
}
});