Skip to content

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

refactor: migrate all stores to new class system

thecodrr59deba6e6c75
diff --git a/apps/web/src/common/store.js b/apps/web/src/common/store.js
index ab1efc95a..66b3f477e 100644
--- a/apps/web/src/common/store.js
+++ b/apps/web/src/common/store.js
@@ -18,9 +18,12 @@ function immer(config) {
};
}
+/**
+ * @returns {[import("zustand").UseStore<any>, any]}
+ */
function createStore(store) {
- store = store.new ? store.new.bind(store) : store;
- return create(immer(store));
+ const [useStore, api] = create(immer(store.new.bind(store)));
+ return [useStore, api.getState()];
}
export default createStore;
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index 73822e573..023cb176e 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -3,6 +3,7 @@ import { db } from "../common";
import { store as noteStore } from "./note-store";
import { store as notebookStore } from "./notebook-store";
import { store as trashStore } from "./trash-store";
+import { store as tagStore } from "./tag-store";
import BaseStore from "./index";
class AppStore extends BaseStore {
@@ -12,10 +13,10 @@ class AppStore extends BaseStore {
colors = [];
refresh = () => {
- noteStore.getState().refresh();
- notebookStore.getState().refresh();
- noteStore.getState().refreshContext();
- trashStore.getState().refresh();
+ noteStore.refresh();
+ notebookStore.refresh();
+ trashStore.refresh();
+ tagStore.refresh();
this.refreshColors();
};
@@ -32,6 +33,8 @@ class AppStore extends BaseStore {
};
}
+/**
+ * @type {[import("zustand").UseStore<AppStore>, AppStore]}
+ */
const [useStore, store] = createStore(AppStore);
-
export { useStore, store };
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index 8743d12d6..5dd7bd830 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -149,9 +149,7 @@ function saveLastOpenedNote(id) {
}
/**
- * @type {[import("zustand").UseStore<EditorStore>,
- * import("zustand").StoreApi<EditorStore>]}
+ * @type {[import("zustand").UseStore<EditorStore>, EditorStore]}
*/
-const [useStore, api] = createStore(EditorStore);
-const store = api.getState();
+const [useStore, store] = createStore(EditorStore);
export { useStore, store };
diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.js
index 767c237c6..310c0984d 100644
--- a/apps/web/src/stores/note-store.js
+++ b/apps/web/src/stores/note-store.js
@@ -18,7 +18,8 @@ class NoteStore extends BaseStore {
};
refresh = () => {
- this.set(state => (state.notes = db.notes.group(undefined, true)));
+ if (this.context) this.refreshContext();
+ else this.set(state => (state.notes = db.notes.group(undefined, true)));
};
refreshContext = () => {
@@ -53,7 +54,7 @@ class NoteStore extends BaseStore {
await db.notes.delete(id);
this.refreshContext();
this.refresh();
- const { session, newSession } = editorStore.getState();
+ const { session, newSession } = editorStore;
if (session.id === id) {
newSession();
}
@@ -97,12 +98,15 @@ class NoteStore extends BaseStore {
* @private
*/
_syncEditor = (noteId, action) => {
- const { session, setSession } = editorStore.getState();
+ const { session, setSession } = editorStore;
if (session.id === noteId) {
setSession(state => (state.session[action] = !state.session[action]));
}
};
}
+/**
+ * @type {[import("zustand").UseStore<NoteStore>, NoteStore]}
+ */
const [useStore, store] = createStore(NoteStore);
export { useStore, store };
diff --git a/apps/web/src/stores/notebook-store.js b/apps/web/src/stores/notebook-store.js
index 1862923a3..4e67baf20 100644
--- a/apps/web/src/stores/notebook-store.js
+++ b/apps/web/src/stores/notebook-store.js
@@ -1,42 +1,45 @@
import { db } from "../common/index";
import createStore from "../common/store";
import { store as trashStore } from "./trash-store";
+import BaseStore from "./index";
-function notebookStore(set, get) {
- return {
- refresh: function() {
- set(state => {
- state.notebooks = db.notebooks.all;
- });
- },
- notebooks: [],
- add: async function(nb) {
- let notebook = await db.notebooks.add(nb);
- if (notebook) {
- get().refresh();
- }
- },
- delete: async function(id, index) {
- await db.notebooks.delete(id);
- set(state => {
- state.notebooks.splice(index, 1);
- trashStore.getState().refresh();
- });
- },
- update: function() {},
- pin: async function(notebook, index) {
- await db.notebooks.notebook(notebook).pin();
- get().refresh();
- },
- selectedNotebookTopics: [],
- setSelectedNotebookTopics: function(id) {
- set(state => {
- state.selectedNotebookTopics = db.notebooks.notebook(id).topics.all;
- });
+class NotebookStore extends BaseStore {
+ notebooks = [];
+ selectedNotebookTopics = [];
+
+ refresh = () => {
+ this.set(state => (state.notebooks = db.notebooks.all));
+ };
+
+ add = async nb => {
+ let notebook = await db.notebooks.add(nb);
+ if (notebook) {
+ this.refresh();
}
};
-}
-const [useStore, store] = createStore(notebookStore);
+ delete = async (id, index) => {
+ await db.notebooks.delete(id);
+ this.set(state => {
+ state.notebooks.splice(index, 1);
+ trashStore.refresh();
+ });
+ };
+
+ pin = async notebook => {
+ await db.notebooks.notebook(notebook).pin();
+ this.refresh();
+ };
+
+ setSelectedNotebookTopics = id => {
+ this.set(state => {
+ state.selectedNotebookTopics = db.notebooks.notebook(id).topics.all;
+ });
+ };
+}
+/**
+ * @type {[import("zustand").UseStore<NotebookStore>, NotebookStore]}
+ */
+const [useStore, store] = createStore(NotebookStore);
export { useStore, store };
diff --git a/apps/web/src/stores/search-store.js b/apps/web/src/stores/search-store.js
index a3f6186ed..0eb47b43e 100644
--- a/apps/web/src/stores/search-store.js
+++ b/apps/web/src/stores/search-store.js
@@ -1,30 +1,31 @@
import createStore from "../common/store";
import { db } from "../common";
+import BaseStore from "..ts";
-function searchStore(set, get) {
- return {
- type: "",
- items: [],
- item: undefined,
- results: [],
- setSearchContext: function(context) {
- set(state => {
- state.type = context.type;
- state.items = context.items;
- state.item = context.item;
- });
- },
- search: async function(query) {
- const { items, type } = get();
- const results = await db.lookup[type](items, query);
- console.log(query, results);
- set(state => {
- state.results = results;
- });
- }
+class SearchStore extends BaseStore {
+ type = "";
+ items = [];
+ item = undefined;
+ results = [];
+
+ setSearchContext = context => {
+ this.set(state => {
+ state.type = context.type;
+ state.items = context.items;
+ state.item = context.item;
+ });
};
-}
-const [useStore, store] = createStore(searchStore);
+ search = async query => {
+ const { items, type } = this;
+ const results = await db.lookup[type](items, query);
+ console.log(query, results);
+ this.set(state => (state.results = results));
+ };
+}
+/**
+ * @type {[import("zustand").UseStore<SearchStore>, SearchStore]}
+ */
+const [useStore, store] = createStore(SearchStore);
export { useStore, store };
diff --git a/apps/web/src/stores/selection-store.js b/apps/web/src/stores/selection-store.js
index 4bbc0fc2d..221b1e734 100644
--- a/apps/web/src/stores/selection-store.js
+++ b/apps/web/src/stores/selection-store.js
@@ -40,6 +40,8 @@ class SelectionStore extends BaseStore {
}
}
+/**
+ * @type {[import("zustand").UseStore<SelectionStore>, SelectionStore]}
+ */
const [useStore, store] = createStore(SelectionStore);
-
export { useStore, store };
diff --git a/apps/web/src/stores/tag-store.js b/apps/web/src/stores/tag-store.js
index abce60bbf..afb7b6ef9 100644
--- a/apps/web/src/stores/tag-store.js
+++ b/apps/web/src/stores/tag-store.js
@@ -1,17 +1,17 @@
import createStore from "../common/store";
import { db } from "../common";
+import BaseStore from "./index";
-function tagStore(set, get) {
- return {
- tags: [],
- refreshTags: function() {
- set(state => {
- state.tags = db.tags.all;
- });
- }
+class TagStore extends BaseStore {
+ tags = [];
+
+ refresh = () => {
+ this.set(state => (state.tags = db.tags.all));
};
}
-const [useStore, store] = createStore(tagStore);
-
+/**
+ * @type {[import("zustand").UseStore<TagStore>, TagStore]}
+ */
+const [useStore, store] = createStore(TagStore);
export { useStore, store };
diff --git a/apps/web/src/stores/theme-store.js b/apps/web/src/stores/theme-store.js
index 8d71ae80a..fade9013e 100644
--- a/apps/web/src/stores/theme-store.js
+++ b/apps/web/src/stores/theme-store.js
@@ -1,26 +1,26 @@
import createStore from "../common/store";
+import BaseStore from "./index";
-function themeStore(set, get) {
- return {
- theme: "light",
- accent: "#0560ff",
- setTheme: function(theme) {
- set(state => {
- state.theme = theme;
- });
- },
- toggleNightMode: function() {
- const theme = get().theme;
- get().setTheme(theme === "dark" ? "light" : "dark");
- },
- setAccent: function(accent) {
- set(state => {
- state.accent = accent;
- });
- }
+class ThemeStore extends BaseStore {
+ theme = "light";
+ accent = "#0560ff";
+
+ setTheme = theme => {
+ this.set(state => (state.theme = theme));
+ };
+
+ toggleNightMode = () => {
+ const theme = this.theme;
+ this.setTheme(theme === "dark" ? "light" : "dark");
};
-}
-const [useStore, store] = createStore(themeStore);
+ setAccent = accent => {
+ this.set(state => (state.accent = accent));
+ };
+}
+/**
+ * @type {[import("zustand").UseStore<ThemeStore>, ThemeStore]}
+ */
+const [useStore, store] = createStore(ThemeStore);
export { useStore, store };
diff --git a/apps/web/src/stores/trash-store.js b/apps/web/src/stores/trash-store.js
index 4858b496e..97740c30e 100644
--- a/apps/web/src/stores/trash-store.js
+++ b/apps/web/src/stores/trash-store.js
@@ -1,38 +1,37 @@
import { db } from "../common/index";
import createStore from "../common/store";
+import BaseStore from "./index";
-function trashStore(set) {
- return {
- refresh: function() {
- set(state => {
- state.trash = db.trash.all;
- });
- },
- trash: [],
- delete: function(id, index) {
- return db.trash.delete(id).then(() => {
- set(state => {
- state.trash.splice(index, 1);
- });
- });
- },
- restore: function(id, index) {
- return db.trash.restore(id).then(() => {
- set(state => {
- state.trash.splice(index, 1);
- });
- });
- },
- clear: function() {
- return db.trash.clear().then(() => {
- set(state => {
- state.trash = [];
- });
+class TrashStore extends BaseStore {
+ trash = [];
+
+ refresh = () => {
+ this.set(state => (state.trash = db.trash.all));
+ };
+
+ delete = (id, index) => {
+ return db.trash.delete(id).then(() => {
+ this.set(state => {
+ state.trash.splice(index, 1);
});
- }
+ });
+ };
+
+ restore = (id, index) => {
+ return db.trash.restore(id).then(() => {
+ this.set(state => state.trash.splice(index, 1));
+ });
};
-}
-const [useStore, store] = createStore(trashStore);
+ clear = () => {
+ return db.trash.clear().then(() => {
+ this.set(state => (state.trash = []));
+ });
+ };
+}
+/**
+ * @type {[import("zustand").UseStore<TrashStore>, TrashStore]}
+ */
+const [useStore, store] = createStore(TrashStore);
export { useStore, store };
diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js
index 6ee1ab103..ace68d959 100644
--- a/apps/web/src/stores/user-store.js
+++ b/apps/web/src/stores/user-store.js
@@ -1,67 +1,66 @@
import createStore from "../common/store";
import { db } from "../common";
import { store as appStore } from "./app-store";
+import BaseStore from "./index";
-function userStore(set, get) {
- return {
- isLoggedIn: false,
- isLoggingIn: false,
- isSyncing: false,
- user: undefined,
- init: () => {
- return db.user.get().then(user => {
- if (!user) return false;
- set(state => {
- state.user = user;
- state.isLoggedIn = true;
- });
- get().sync();
- return true;
+class UserStore extends BaseStore {
+ isLoggedIn = false;
+ isLoggingIn = false;
+ isSyncing = false;
+ user = undefined;
+
+ init = () => {
+ return db.user.get().then(user => {
+ if (!user) return false;
+ this.set(state => {
+ state.user = user;
+ state.isLoggedIn = true;
});
- },
- login: (username, password) => {
- set(state => {
- state.isLoggingIn = true;
+ this.sync();
+ return true;
+ });
+ };
+
+ login = form => {
+ this.set(state => (state.isLoggingIn = true));
+ return db.user
+ .login(form.username, form.password)
+ .then(() => {
+ return this.init();
+ })
+ .finally(() => {
+ this.set(state => (state.isLoggingIn = false));
});
- return db.user
- .login(username, password)
- .then(() => {
- return get().init();
- })
- .finally(() => {
- set(state => {
- state.isLoggingIn = false;
- });
- });
- },
- sync: () => {
- set(state => {
- state.isSyncing = true;
+ };
+
+ sync = () => {
+ this.set(state => (state.isSyncing = true));
+ db.sync()
+ .then(() => {
+ appStore.refresh();
+ })
+ .catch(err => {
+ if (err.code === "MERGE_CONFLICT") appStore.refresh();
+ else console.error(err);
+ })
+ .finally(() => {
+ this.set(state => (state.isSyncing = false));
});
- db.sync()
- .then(() => {
- appStore.getState().refreshApp();
- })
- .catch(err => {
- console.error(err);
- })
- .finally(() => {
- set(state => {
- state.isSyncing = false;
- });
- });
- },
- logout: () => {
- db.user.logout().then(() => {
- set(state => {
- state.user = undefined;
- state.isLoggedIn = false;
- });
+ };
+
+ logout = () => {
+ db.user.logout().then(() => {
+ this.set(state => {
+ state.user = undefined;
+ state.isLoggedIn = false;
});
- }
+ appStore.refresh();
+ });
};
}
-const [useStore, store] = createStore(userStore);
-
+/**
+ * @type {[import("zustand").UseStore<UserStore>, UserStore]}
+ */
+const [useStore, store] = createStore(UserStore);
export { useStore, store };