From 62bdee7789b2458aaeb425f40368af817bb58f65 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Mon, 11 Mar 2024 10:45:43 +0500 Subject: [PATCH] web: set session id on saving note --- apps/web/src/components/editor/index.tsx | 19 ++++------------- apps/web/src/stores/editor-store.ts | 27 ++++++++---------------- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx index b5919ec9e..d20dd1143 100644 --- a/apps/web/src/components/editor/index.tsx +++ b/apps/web/src/components/editor/index.tsx @@ -73,16 +73,10 @@ type DocumentPreview = { hash: string; }; -function saveContent( - noteId: string, - sessionId: string, - ignoreEdit: boolean, - content: () => string -) { - console.log("SAVE to note"); - useEditorStore.getState().saveSessionContent(noteId, sessionId, ignoreEdit, { +function saveContent(noteId: string, ignoreEdit: boolean, content: string) { + useEditorStore.getState().saveSessionContent(noteId, ignoreEdit, { type: "tiptap", - data: content() + data: content }); } const deferredSave = debounceWithId(saveContent, 100); @@ -277,12 +271,7 @@ function EditorView({ id }: { id: string }) { onPreviewDocument={(url) => setDocPreview(url)} onContentChange={() => (lastSavedTime.current = Date.now())} onSave={(content, ignoreEdit) => { - const session = useEditorStore - .getState() - .getSession(id, ["default"]); - console.log("ON SAVE", session); - if (!session) return; - deferredSave(id, id, session.sessionId, ignoreEdit, content); + deferredSave(session.id, session.id, ignoreEdit, data); }} options={{ readonly: isReadonly || isPreviewSession, diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts index b4bdf8ada..2aece99a4 100644 --- a/apps/web/src/stores/editor-store.ts +++ b/apps/web/src/stores/editor-store.ts @@ -468,11 +468,15 @@ class EditorStore extends BaseStore { this.setSaveState(id, 0); try { - // if (partial.content) currentSession.content = partial.content; + const sessionId = + "sessionId" in currentSession + ? currentSession.sessionId + : `${Date.now()}`; + if (isLockedSession(currentSession)) { await db.vault.save({ content: partial.content, - sessionId: partial.sessionId, + sessionId, id }); } else { @@ -483,22 +487,11 @@ class EditorStore extends BaseStore { ? currentSession.note.dateEdited : undefined, content: partial.content, - sessionId: partial.sessionId, + sessionId, id }); } - // if (currentSession && currentSession.id !== id) { - // noteStore.refresh(); - // throw new Error("Aborting save operation: old session."); - // } - // if (!id) throw new Error("Note not saved."); - - // let note = await db.notes.note(id); - // if (!note) throw new Error("Note not saved."); - - // hashNavigate(`/notes/${id}/edit`, { replace: true, notify: false }); - const defaultNotebook = db.settings.getDefaultNotebook(); if (currentSession.type === "new" && currentSession.context) { const { type } = currentSession.context; @@ -531,7 +524,7 @@ class EditorStore extends BaseStore { id, note, saveState: SaveState.Saved, - sessionId: partial.sessionId || `${Date.now()}`, + sessionId, attachmentsLength, pinned: currentSession.pinned, content: partial.content @@ -609,12 +602,10 @@ class EditorStore extends BaseStore { saveSessionContent = ( id: string, - sessionId: string, ignoreEdit: boolean, content: NoteContent ) => { - // const dateEdited = ignoreEdit ? this.get().session.dateEdited : undefined; - return this.saveSession(id, { sessionId, content, ignoreEdit }); + return this.saveSession(id, { content, ignoreEdit }); }; setSaveState = (id: string, saveState: SaveState) => {