A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
refactor: move sync logic to appstore
thecodrrc060e99f6d50
diff --git a/apps/web/src/components/diff-viewer/index.js b/apps/web/src/components/diff-viewer/index.jsindex bf774b9e3..e529fea93 100644--- a/apps/web/src/components/diff-viewer/index.js+++ b/apps/web/src/components/diff-viewer/index.js@@ -42,7 +42,7 @@ function DiffViewer(props) {const setIsEditorOpen = useAppStore((store) => store.setIsEditorOpen);const theme = useThemeStore((store) => store.theme);- const sync = useUserStore((store) => store.sync);+ const sync = useAppStore((store) => store.sync);const clearSession = useEditorStore((store) => store.clearSession);const [conflictedNote, setConflictedNote] = useState();const [remoteContent, setRemoteContent] = useState();diff --git a/apps/web/src/components/navigation-menu/index.js b/apps/web/src/components/navigation-menu/index.jsindex 37a5786cc..195708d5a 100644--- a/apps/web/src/components/navigation-menu/index.js+++ b/apps/web/src/components/navigation-menu/index.js@@ -64,10 +64,10 @@ function NavigationMenu(props) {const isSideMenuOpen = useAppStore((store) => store.isSideMenuOpen);const refreshMenuPins = useAppStore((store) => store.refreshMenuPins);const toggleSideMenu = useAppStore((store) => store.toggleSideMenu);- const isSyncing = useUserStore((store) => store.isSyncing);+ const isSyncing = useAppStore((store) => store.isSyncing);const isLoggedIn = useUserStore((store) => store.isLoggedIn);//const logout = useUserStore((store) => store.logout);- const sync = useUserStore((store) => store.sync);+ const sync = useAppStore((store) => store.sync);const theme = useThemeStore((store) => store.theme);const toggleNightMode = useThemeStore((store) => store.toggleNightMode);const isMobile = useMobile();diff --git a/apps/web/src/components/status-bar/index.js b/apps/web/src/components/status-bar/index.jsindex 2eed2787c..d6f7b67ce 100644--- a/apps/web/src/components/status-bar/index.js+++ b/apps/web/src/components/status-bar/index.js@@ -3,6 +3,7 @@ import { Button, Flex, Text } from "rebass";import EditorFooter from "../editor/footer";import * as Icon from "../icons";import { useStore as useUserStore } from "../../stores/user-store";+import { useStore as useAppStore } from "../../stores/app-store";import { showLogInDialog } from "../../common/dialog-controller";import TimeAgo from "timeago-react";import { hashNavigate, navigate } from "../../navigation";@@ -10,9 +11,9 @@ import { hashNavigate, navigate } from "../../navigation";function StatusBar() {const user = useUserStore((state) => state.user);const sync = useUserStore((state) => state.sync);- const lastSynced = useUserStore((state) => state.lastSynced);+ const lastSynced = useAppStore((state) => state.lastSynced);const isLoggedIn = useUserStore((state) => state.isLoggedIn);- const isSyncing = useUserStore((state) => state.isSyncing);+ const isSyncing = useAppStore((state) => state.isSyncing);return (<Flexdiff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.jsindex 29213438a..74c8ca9a4 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/db";import { store as noteStore } from "./note-store";import { store as notebookStore } from "./notebook-store";import { store as trashStore } from "./trash-store";+import { store as editorStore } from "./editor-store";import { store as tagStore } from "./tag-store";import BaseStore from "./index";import { isMobile } from "../utils/dimensions";@@ -12,6 +13,7 @@ import { resetReminders } from "../common/reminders";class AppStore extends BaseStore {// default stateisSideMenuOpen = !isMobile();+ isSyncing = false;isFocusMode = false;isEditorOpen = false;isVaultCreated = false;@@ -19,8 +21,10 @@ class AppStore extends BaseStore {globalMenu = { items: [], data: {} };reminders = [];menuPins = [];+ lastSynced = 0;refresh = async () => {+ await this.updateLastSynced();await resetReminders();noteStore.refresh();notebookStore.refresh();@@ -110,6 +114,33 @@ class AppStore extends BaseStore {return;}};++ updateLastSynced = async () => {+ const lastSynced = await db.lastSynced();+ this.set((state) => (state.lastSynced = lastSynced));+ };++ sync = async (full = true) => {+ this.updateLastSynced();+ this.set((state) => (state.isSyncing = true));+ return db+ .sync(full)+ .then(async () => {+ await this.updateLastSynced();+ return await this.refresh();+ })+ .catch(async (err) => {+ console.error(err);+ if (err.code === "MERGE_CONFLICT") await this.refresh();+ else {+ showToast("error", err.message);+ console.error(err);+ }+ })+ .finally(() => {+ this.set((state) => (state.isSyncing = false));+ });+ };}/**diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.jsindex 938c1cc33..f72c18e1c 100644--- a/apps/web/src/stores/user-store.js+++ b/apps/web/src/stores/user-store.js@@ -19,9 +19,7 @@ class UserStore extends BaseStore {isLoggedIn = false;isLoggingIn = false;isSigningIn = false;- isSyncing = false;user = undefined;- lastSynced = 0;init = () => {EV.subscribe(EVENTS.appRefreshRequested, () => appStore.refresh());@@ -65,7 +63,7 @@ class UserStore extends BaseStore {});EV.subscribe(EVENTS.databaseSyncRequested, async () => {- await this.sync(false);+ await appStore.sync(false);});EV.subscribe(EVENTS.userLoggedOut, async (reason) => {@@ -86,7 +84,7 @@ class UserStore extends BaseStore {await db.connectSSE();}});- await this.sync();+ await appStore.sync();return true;});};@@ -127,28 +125,6 @@ class UserStore extends BaseStore {this.set((state) => (state.isSigningIn = false));});};-- sync = (full = true) => {- this.set((state) => (state.isSyncing = true));- return db- .sync(full)- .then(async () => {- const lastSynced = await db.lastSynced();- this.set((state) => (state.lastSynced = lastSynced));- return await appStore.refresh();- })- .catch(async (err) => {- console.error(err);- if (err.code === "MERGE_CONFLICT") await appStore.refresh();- else {- showToast("error", err.message);- console.error(err);- }- })- .finally(() => {- this.set((state) => (state.isSyncing = false));- });- };}/**