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
web: add support for disabling sync & auto sync
Abdullah Atta088235a46d85
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.jsindex da62ec47d..63112057f 100644--- a/apps/web/src/app-effects.js+++ b/apps/web/src/app-effects.js@@ -50,7 +50,6 @@ const relay = new WebExtensionRelay();export default function AppEffects({ setShow }) {const refreshNavItems = useStore((store) => store.refreshNavItems);- const sync = useStore((store) => store.sync);const updateLastSynced = useStore((store) => store.updateLastSynced);const isFocusMode = useStore((store) => store.isFocusMode);const addReminder = useStore((store) => store.addReminder);@@ -83,13 +82,6 @@ export default function AppEffects({ setShow }) {});- db.eventManager.subscribe(- EVENTS.databaseSyncRequested,- async (full, force) => {- await sync(full, force);- }- );-initStore();initAttachments();refreshNavItems();@@ -118,7 +110,6 @@ export default function AppEffects({ setShow }) {initEditorStore,initStore,initAttachments,- sync,updateLastSynced,refreshNavItems,initUser,diff --git a/apps/web/src/components/status-bar/index.js b/apps/web/src/components/status-bar/index.jsindex 2876ead4d..f75f63faf 100644--- a/apps/web/src/components/status-bar/index.js+++ b/apps/web/src/components/status-bar/index.js@@ -27,7 +27,8 @@ import {SyncError,Checkmark,Alert,- Issue+ Issue,+ SyncOff} from "../icons";import { useStore as useUserStore } from "../../stores/user-store";import { useStore as useAppStore } from "../../stores/app-store";@@ -203,6 +204,7 @@ function statusToInfoText(status) {function SyncStatus() {const syncStatus = useAppStore((state) => state.syncStatus);const lastSynced = useAppStore((state) => state.lastSynced);+ const isSyncEnabled = useAppStore((state) => state.isSyncEnabled);const sync = useAppStore((state) => state.sync);const user = useUserStore((state) => state.user);@@ -210,6 +212,21 @@ function SyncStatus() {f.check(syncStatus.key, user, lastSynced));+ if (!isSyncEnabled)+ return (+ <Button+ variant="statusitem"+ sx={{ alignItems: "center", justifyContent: "center", display: "flex" }}+ title={"Sync is disabled"}+ data-test-id={`sync-status-disabled`}+ >+ <SyncOff size={12} color={"error"} />+ <Text variant="subBody" ml={status.text ? "3px" : 0}>+ Sync disabled+ </Text>+ </Button>+ );+return (<Buttonvariant="statusitem"diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.jsindex 61502ddfd..27f515a61 100644--- a/apps/web/src/stores/app-store.js+++ b/apps/web/src/stores/app-store.js@@ -29,8 +29,9 @@ import { store as monographStore } from "./monograph-store";import BaseStore from "./index";import { showToast } from "../utils/toast";import { resetReminders } from "../common/reminders";-import { EV, EVENTS } from "@notesnook/core/common";+import { EV, EVENTS, SYNC_CHECK_IDS } from "@notesnook/core/common";import { logger } from "../utils/logger";+import Config from "../utils/config";var syncStatusTimeout = 0;const BATCH_SIZE = 50;@@ -40,6 +41,8 @@ class AppStore extends BaseStore {isFocusMode = false;isEditorOpen = false;isVaultCreated = false;+ isAutoSyncEnabled = Config.get("autoSyncEnabled", true);+ isSyncEnabled = Config.get("syncEnabled", true);syncStatus = {key: "synced",progress: null,@@ -74,6 +77,27 @@ class AppStore extends BaseStore {});+ EV.subscribe(EVENTS.syncCheckStatus, async (type) => {+ const { isAutoSyncEnabled, isSyncEnabled } = this.get();+ switch (type) {+ case SYNC_CHECK_IDS.sync:+ return { type, result: isSyncEnabled };+ case SYNC_CHECK_IDS.autoSync:+ return { type, result: isAutoSyncEnabled };+ default:+ return { type, result: true };+ }+ });++ db.eventManager.subscribe(+ EVENTS.databaseSyncRequested,+ async (full, force) => {+ if (!this.get().isAutoSyncEnabled) return;++ await this.get().sync(full, force);+ }+ );+db.eventManager.subscribe(EVENTS.syncCompleted, () => {this.set((state) => {state.syncStatus = { key: "synced" };@@ -110,6 +134,21 @@ class AppStore extends BaseStore {this.set((state) => (state.isFocusMode = !state.isFocusMode));};+ toggleAutoSync = () => {+ Config.set("autoSyncEnabled", !this.get().isAutoSyncEnabled);+ this.set((state) => (state.isAutoSyncEnabled = !state.isAutoSyncEnabled));+ };++ toggleSync = () => {+ const { isSyncEnabled } = this.get();+ Config.set("syncEnabled", !isSyncEnabled);+ this.set((state) => (state.isSyncEnabled = !state.isSyncEnabled));++ if (isSyncEnabled) {+ db.syncer.stop();+ }+ };+toggleSideMenu = (toggleState) => {this.set((state) =>@@ -200,6 +239,7 @@ class AppStore extends BaseStore {};sync = async (full = true, force = false) => {+ if (!this.get().isSyncEnabled) return;if (this.isSyncing()) return;clearTimeout(syncStatusTimeout);diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.jsindex 327aae239..4541d4eda 100644--- a/apps/web/src/views/settings.js+++ b/apps/web/src/views/settings.js@@ -127,6 +127,7 @@ const otherItems = [function Settings() {const [groups, setGroups] = useState({+ sync: false,appearance: false,editor: false,mfa: false,@@ -137,6 +138,10 @@ function Settings() {other: true});const isVaultCreated = useAppStore((store) => store.isVaultCreated);+ const isSyncEnabled = useAppStore((store) => store.isSyncEnabled);+ const isAutoSyncEnabled = useAppStore((store) => store.isAutoSyncEnabled);+ const toggleAutoSync = useAppStore((store) => store.toggleAutoSync);+ const toggleSync = useAppStore((store) => store.toggleSync);const setIsVaultCreated = useAppStore((store) => store.setIsVaultCreated);const refreshApp = useAppStore((store) => store.refresh);const refreshNotes = useNoteStore((store) => store.refresh);@@ -253,7 +258,7 @@ function Settings() {});if (result) {- await showToast("success", "Account password changed!");+ showToast("success", "Account password changed!");}}}>@@ -268,12 +273,6 @@ function Settings() {tip="Re-upload, delete & manage your attachments."/></Button>- <Button variant="list" onClick={() => sync(true, true)}>- <Tip- text="Having problems with syncing?"- tip="Try force sync to resolve issues with syncing"- />- </Button><Buttondata-test-id="settings-logout"variant="list"@@ -386,6 +385,41 @@ function Settings() {))}</>)}+ {isLoggedIn && (+ <>+ <Header+ title="Sync settings"+ isOpen={groups.sync}+ onClick={() => {+ setGroups((g) => ({ ...g, sync: !g.sync }));+ }}+ />+ {groups.sync && (+ <>+ <Toggle+ title="Disable sync"+ onTip="Changes on this device won't sync to your other devices."+ offTip="Your changes will be synced to other devices."+ onToggled={toggleSync}+ isToggled={!isSyncEnabled}+ />+ <Toggle+ title="Disable auto sync"+ onTip="You will have to manually run the sync to transfer your changes to other devices."+ offTip="All changes will automatically sync to your other device."+ onToggled={toggleAutoSync}+ isToggled={!isAutoSyncEnabled}+ />+ <Button variant="list" onClick={() => sync(true, true)}>+ <Tip+ text="Having problems with syncing?"+ tip="Try force sync to resolve issues with syncing"+ />+ </Button>+ </>+ )}+ </>+ )}<Headertitle="Appearance"isOpen={groups.appearance}diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.jsindex 31b383dff..aeb5740f4 100644--- a/packages/core/api/sync/index.js+++ b/packages/core/api/sync/index.js@@ -18,10 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.*/import {+ checkSyncStatus,EV,EVENTS,sendAttachmentsProgressEvent,- sendSyncProgressEvent+ sendSyncProgressEvent,+ SYNC_CHECK_IDS} from "../../common";import Constants from "../../utils/constants";import TokenManager from "../token-manager";@@ -167,6 +169,11 @@ class Sync {* @param {number} serverLastSynced*/async start(full, force, serverLastSynced) {+ if (!(await checkSyncStatus(SYNC_CHECK_IDS.sync))) {+ await this.connection.stop();+ return;+ }+this.logger.info("Starting sync", { full, force, serverLastSynced });this.connection.onclose((error) => {@@ -197,6 +204,10 @@ class Sync {this.logger.info("Nothing to do.");await this.stop(serverLastSynced || oldLastSynced);}++ if (!(await checkSyncStatus(SYNC_CHECK_IDS.autoSync))) {+ await this.connection.stop();+ }}async init(isForceSync) {diff --git a/packages/core/common.js b/packages/core/common.jsindex 55ca2eee6..4870ad249 100644--- a/packages/core/common.js+++ b/packages/core/common.js@@ -29,6 +29,13 @@ export async function checkIsUserPremium(type) {return results.some((r) => r.type === type && r.result === true);}+export async function checkSyncStatus(type) {+ const results = await EV.publishWithResult(EVENTS.syncCheckStatus, type);+ if (typeof results === "boolean") return results;+ else if (typeof results === "undefined") return true;+ return results.some((r) => r.type === type && r.result === true);+}+export function sendAttachmentsProgressEvent(type, groupId, total, current) {EV.publish(EVENTS.attachmentsLoading, {type,@@ -65,6 +72,11 @@ export const CHECK_IDS = {backupEncrypt: "backup:encrypt"};+export const SYNC_CHECK_IDS = {+ autoSync: "autoSync",+ sync: "sync"+};+export const EVENTS = {userCheckStatus: "user:checkStatus",userSubscriptionUpdated: "user:subscriptionUpdated",@@ -78,6 +90,7 @@ export const EVENTS = {syncProgress: "sync:progress",syncCompleted: "sync:completed",syncItemMerged: "sync:itemMerged",+ syncCheckStatus: "sync:checkStatus",databaseUpdated: "db:updated",databaseCollectionInitiated: "db:collectionInitiated",appRefreshRequested: "app:refreshRequested",