Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix locked notes getting overwritten with unencrypted content

Abdullah Attafe0129e1a48b
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 63aabb056..e14595bdf 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -104,10 +104,12 @@ export default function TabsView() {
title={session.note.title}
unlock={async (password) => {
const note = await db.vault.open(session.id, password);
- if (!note) throw new Error("note with this id does not exist.");
+ if (!note || !note.content)
+ throw new Error("note with this id does not exist.");
useEditorStore.getState().addSession({
type: "default",
+ locked: true,
id: session.id,
note: session.note,
saveState: SaveState.Saved,
diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts
index 9bea691b9..19d55b961 100644
--- a/apps/web/src/stores/editor-store.ts
+++ b/apps/web/src/stores/editor-store.ts
@@ -92,6 +92,7 @@ export type DefaultEditorSession = BaseEditorSession & {
sessionId: string;
color?: string;
tags?: Tag[];
+ locked?: boolean;
attachmentsLength?: number;
saveState: SaveState;
};
@@ -131,6 +132,7 @@ type SessionTypeMap = {
export function isLockedSession(session: EditorSession): boolean {
return (
session.type === "locked" ||
+ (session.type === "default" && !!session.locked) ||
("content" in session &&
!!session.content &&
"locked" in session.content &&
@@ -337,8 +339,9 @@ class EditorStore extends BaseStore<EditorStore> {
: (await db.notes.note(noteId)) || (await db.notes.trashed(noteId));
if (!note) return;
const isPreview = session ? session.preview : true;
+ const isLocked = await db.vaults.itemExists(note);
- if (note.locked && note.type !== "trash") {
+ if (isLocked && note.type !== "trash") {
this.addSession({
type: "locked",
id: note.id,
@@ -382,8 +385,7 @@ class EditorStore extends BaseStore<EditorStore> {
: undefined;
if (content?.locked) {
- note.locked = true;
- await db.notes.add({ id: note.id, locked: true });
+ await db.vault.add(noteId);
return this.openSession(note, { ...options, force: true });
}
@@ -534,6 +536,23 @@ class EditorStore extends BaseStore<EditorStore> {
content: partial.content
});
} else {
+ // update any conflicted session that has the same content opened
+ if (partial.content) {
+ this.set((state) => {
+ const session = state.sessions.find(
+ (s): s is ConflictedEditorSession =>
+ (s.type === "diff" || s.type === "conflicted") &&
+ !!s.content.conflicted &&
+ s.content.conflicted.id === currentSession.note.contentId &&
+ s.content.conflicted.dateEdited ===
+ currentSession.note.dateEdited
+ );
+ if (!session || !session.content.conflicted) return;
+ session.content.conflicted.data = partial.content!.data;
+ session.content.conflicted.dateEdited = note.dateEdited;
+ });
+ }
+
this.updateSession(id, ["default"], {
preview: false,
attachmentsLength: attachmentsLength,
@@ -641,7 +660,7 @@ class EditorStore extends BaseStore<EditorStore> {
};
}
-const [useEditorStore] = createPersistedStore(EditorStore, {
+const useEditorStore = createPersistedStore(EditorStore, {
name: "editor-sessions",
partialize: (state) => ({
history: state.history,
@@ -655,9 +674,11 @@ const [useEditorStore] = createPersistedStore(EditorStore, {
needsHydration: session.type === "new" ? false : true,
preview: session.preview,
pinned: session.pinned,
+ title: session.title,
note:
"note" in session
? {
+ id: session.note.id,
title: session.note.title
}
: undefined