Skip to content

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

feat: add att. store for handling attachments

thecodrr438e5a531218
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index 910c3c260..9fa0ff01a 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -5,6 +5,7 @@ import { store as notebookStore } from "./notebook-store";
import { store as trashStore } from "./trash-store";
import { store as tagStore } from "./tag-store";
import { store as editorstore } from "./editor-store";
+import { store as attachmentStore } from "./attachment-store";
import BaseStore from "./index";
import { showToast } from "../utils/toast";
import { resetReminders } from "../common/reminders";
@@ -30,6 +31,7 @@ class AppStore extends BaseStore {
notebookStore.refresh();
trashStore.refresh();
tagStore.refresh();
+ attachmentStore.refresh();
this.refreshNavItems();
};
diff --git a/apps/web/src/stores/attachment-store.js b/apps/web/src/stores/attachment-store.js
new file mode 100644
index 000000000..75aba5113
--- /dev/null
+++ b/apps/web/src/stores/attachment-store.js
@@ -0,0 +1,115 @@
+import createStore from "../common/store";
+import { db } from "../common/db";
+import BaseStore from "./index";
+import { AppEventManager, AppEvents } from "../common/app-events";
+import { store as editorStore } from "./editor-store";
+import { checkAttachment } from "../common/attachments";
+
+class AttachmentStore extends BaseStore {
+ attachments = [];
+
+ init = () => {
+ AppEventManager.subscribe(
+ AppEvents.UPDATE_ATTACHMENT_PROGRESS,
+ ({ hash, type, total, loaded }) => {
+ this.set((state) => {
+ const index = state.attachments.findIndex(
+ (a) => a.metadata.hash === hash
+ );
+ if (index <= -1) return;
+ const percent = Math.round((loaded / total) * 100);
+ const status =
+ percent < 100 ? { type, loaded, total, progress: percent } : null;
+ if (!status) return this.refresh();
+ state.attachments[index] = {
+ ...state.attachments[index],
+ status,
+ };
+ });
+ }
+ );
+ this.refresh();
+ };
+
+ refresh = () => {
+ this.set((state) => (state.attachments = db.attachments.all));
+ };
+
+ filter = (query) => {
+ if (!query || !query.trim().length) return this.refresh();
+
+ this.set(
+ (state) =>
+ (state.attachments = db.lookup.attachments(db.attachments.all, query))
+ );
+ };
+
+ recheck = async (hashes) => {
+ const attachments = this.get().attachments;
+ for (let hash of hashes) {
+ const index = attachments.findIndex((a) => a.metadata.hash === hash);
+ try {
+ this._changeWorkingStatus(index, "recheck", undefined);
+
+ const { failed, success } = await checkAttachment(hash);
+ this._changeWorkingStatus(index, false, success ? null : failed);
+ } catch (e) {
+ console.error(e);
+ this._changeWorkingStatus(
+ index,
+ false,
+ `Rechecking failed: ${e.message}`
+ );
+ }
+ }
+ };
+
+ rename = async (hash, newName) => {
+ await db.attachments.add({
+ hash,
+ filename: newName,
+ });
+ this.get().refresh();
+ };
+
+ permanentDelete = async (hash) => {
+ const index = this.get().attachments.findIndex(
+ (a) => a.metadata.hash === hash
+ );
+ if (index <= -1) return;
+ const noteIds = this.get().attachments[index].noteIds.slice();
+
+ try {
+ this._changeWorkingStatus(index, "delete", undefined);
+ if (await db.attachments.remove(hash, false)) {
+ this.get().refresh();
+
+ if (noteIds.includes(editorStore.get().session.id)) {
+ await editorStore.clearSession();
+ }
+ }
+ } catch (e) {
+ console.error(e);
+ this._changeWorkingStatus(index, false, `Failed to delete: ${e.message}`);
+ }
+ };
+
+ /**
+ *
+ * @param {*} index
+ * @param {"delete"|"recheck"} workType
+ * @param {*} failed
+ */
+ _changeWorkingStatus = (index, workType, failed) => {
+ this.set((state) => {
+ state.attachments[index].failed = failed;
+ state.attachments[index].working = workType;
+ });
+ };
+}
+
+/**
+ * @type {[import("zustand").UseStore<AttachmentStore>, AttachmentStore]}
+ */
+const [useStore, store] = createStore(AttachmentStore);
+export { useStore, store };
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index 169c6cd50..ac07a7fcb 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -1,5 +1,6 @@
import createStore from "../common/store";
import { store as noteStore } from "./note-store";
+import { store as attachmentStore } from "./attachment-store";
import { store as appStore } from "./app-store";
import { store as tagStore } from "./tag-store";
import { db } from "../common/db";
@@ -35,7 +36,7 @@ const getDefaultSession = (sessionId = Date.now()) => {
context: undefined,
color: undefined,
dateEdited: 0,
- attachments: [],
+ attachmentsLength: 0,
};
};
@@ -133,7 +134,7 @@ class EditorStore extends BaseStore {
...defaultSession,
...note,
state: SESSION_STATES.new,
- attachments: db.attachments.ofNote(note.id, "all") || [],
+ attachmentsLength: db.attachments.ofNote(note.id, "all")?.length || 0,
};
});
appStore.setIsEditorOpen(true);
@@ -180,17 +181,19 @@ class EditorStore extends BaseStore {
this.set((state) => {
state.session.id = note.id;
state.session.notebooks = note.notebooks;
- state.session.attachments = attachments;
state.session.saveState = 1;
+ state.session.attachmentsLength = attachments.length;
});
+ if (attachments?.length !== oldSession.attachmentsLength)
+ attachmentStore.refresh();
+
if (!oldSession) {
noteStore.refresh();
return;
}
if (
- attachments?.length !== oldSession.attachments.length ||
note.headline !== oldSession.headline ||
note.title !== oldSession.title
)
diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.js
index d5a17c9ab..e8cf896c0 100644
--- a/apps/web/src/stores/note-store.js
+++ b/apps/web/src/stores/note-store.js
@@ -33,7 +33,7 @@ class NoteStore extends BaseStore {
};
setSelectedNote = (id) => {
- if (id === 0) selectionStore.get().toggleSelectionMode(false);
+ if (!id) selectionStore.get().toggleSelectionMode(false);
this.set((state) => (state.selectedNote = id));
};