Skip to content

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

web: improve sync status updating & display

Abdullah Atta127c4cd53766
diff --git a/apps/web/src/components/status-bar/index.js b/apps/web/src/components/status-bar/index.js
index f75f63faf..59a1413c4 100644
--- a/apps/web/src/components/status-bar/index.js
+++ b/apps/web/src/components/status-bar/index.js
@@ -212,25 +212,10 @@ 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 (
<Button
variant="statusitem"
- onClick={() => sync()}
+ onClick={() => (isSyncEnabled ? sync() : null)}
sx={{ alignItems: "center", justifyContent: "center", display: "flex" }}
title={status.tooltip}
data-test-id={`sync-status-${status.key}`}
@@ -310,5 +295,24 @@ const syncStatusFilters = [
color: "error",
text: "Sync failed",
tooltip: "Sync failed to completed. Please try again."
+ },
+ {
+ key: "offline",
+ check: (syncStatus) => syncStatus === "offline",
+ icon: SyncOff,
+ text: ({ lastSynced }) => (
+ <>
+ <TimeAgo live={true} locale="en_short" datetime={lastSynced} />{" "}
+ (offline)
+ </>
+ ),
+ tooltip: "You are offline."
+ },
+ {
+ key: "disabled",
+ check: (syncStatus) => syncStatus === "disabled",
+ icon: SyncOff,
+ text: () => "Sync disabled",
+ tooltip: "Sync is disabled."
}
];
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index c1c7176ee..ad0ba8b74 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -33,6 +33,7 @@ import { resetReminders } from "../common/reminders";
import { EV, EVENTS, SYNC_CHECK_IDS } from "@notesnook/core/common";
import { logger } from "../utils/logger";
import Config from "../utils/config";
+import { onPageVisibilityChanged } from "../utils/page-visibility";
var syncStatusTimeout = 0;
const BATCH_SIZE = 50;
@@ -46,7 +47,7 @@ class AppStore extends BaseStore {
isSyncEnabled = Config.get("syncEnabled", true);
isRealtimeSyncEnabled = Config.get("isRealtimeSyncEnabled", true);
syncStatus = {
- key: "synced",
+ key: navigator.onLine ? "synced" : "offline",
progress: null,
type: null
};
@@ -104,7 +105,7 @@ class AppStore extends BaseStore {
);
db.eventManager.subscribe(EVENTS.syncAborted, (error) => {
- showToast("error", error);
+ if (error) showToast("error", error);
this.updateSyncStatus("failed");
});
@@ -115,6 +116,20 @@ class AppStore extends BaseStore {
count = 0;
this.refresh();
});
+
+ onPageVisibilityChanged(async (type, documentHidden) => {
+ if (!documentHidden && type !== "offline") {
+ logger.info("Page visibility changed. Reconnecting SSE...");
+ if (type === "online") {
+ // a slight delay to make sure sockets are open and can be connected
+ // to. Otherwise, this fails miserably.
+ await new Promise((resolve) => setTimeout(resolve, 2000));
+ }
+ await db.connectSSE({ force: type === "online" }).catch(logger.error);
+ } else if (type === "offline") {
+ await this.abortSync("offline");
+ }
+ });
};
refresh = async () => {
@@ -156,7 +171,7 @@ class AppStore extends BaseStore {
this.set((state) => (state.isSyncEnabled = !state.isSyncEnabled));
if (isSyncEnabled) {
- db.syncer.stop();
+ this.abortSync("disabled");
}
};
@@ -258,7 +273,7 @@ class AppStore extends BaseStore {
};
sync = async (full = true, force = false) => {
- if (!this.get().isSyncEnabled) return;
+ if (!this.get().isSyncEnabled || !navigator.onLine) return;
if (this.isSyncing()) return;
clearTimeout(syncStatusTimeout);
@@ -289,9 +304,16 @@ class AppStore extends BaseStore {
}
};
+ abortSync = async (status = "offline") => {
+ if (this.isSyncing()) this.updateSyncStatus("failed");
+ else this.updateSyncStatus(status);
+
+ await db.syncer.stop();
+ };
+
/**
*
- * @param {"synced" | "syncing" | "conflicts" | "failed" | "completed"} key
+ * @param {"synced" | "syncing" | "conflicts" | "failed" | "completed" | "offline" | "disabled"} key
*/
updateSyncStatus = (key, reset = false) => {
logger.info(`Sync status updated: ${key}`);
@@ -299,7 +321,8 @@ class AppStore extends BaseStore {
if (reset) {
syncStatusTimeout = setTimeout(() => {
- this.updateSyncStatus("synced", false);
+ if (this.get().syncStatus.key === key)
+ this.updateSyncStatus("synced", false);
}, 3000);
}
};
diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js
index 421d56127..1b077cdc9 100644
--- a/apps/web/src/stores/user-store.js
+++ b/apps/web/src/stores/user-store.js
@@ -27,12 +27,10 @@ import {
showOnboardingDialog
} from "../common/dialog-controller";
import Config from "../utils/config";
-import { onPageVisibilityChanged } from "../utils/page-visibility";
import { hashNavigate } from "../navigation";
import { isUserPremium } from "../hooks/use-is-user-premium";
import { SUBSCRIPTION_STATUS } from "../common/constants";
import { ANALYTICS_EVENTS, trackEvent } from "../utils/analytics";
-import { logger } from "../utils/logger";
class UserStore extends BaseStore {
isLoggedIn = undefined;
@@ -100,18 +98,6 @@ class UserStore extends BaseStore {
}
});
- onPageVisibilityChanged(async function (type, documentHidden) {
- if (!documentHidden) {
- logger.info("Page visibility changed. Reconnecting SSE...");
- if (type === "online") {
- // a slight delay to make sure sockets are open and can be connected
- // to. Otherwise, this fails miserably.
- await new Promise((resolve) => setTimeout(resolve, 2000));
- }
- await db.connectSSE({ force: type === "online" }).catch(logger.error);
- }
- });
-
return true;
});
};
diff --git a/apps/web/src/utils/page-visibility.js b/apps/web/src/utils/page-visibility.js
index 546994543..ee8e51f11 100644
--- a/apps/web/src/utils/page-visibility.js
+++ b/apps/web/src/utils/page-visibility.js
@@ -32,6 +32,7 @@ if (typeof document.hidden !== "undefined") {
export function onPageVisibilityChanged(handler) {
onDeviceOnline(() => handler("online", false));
+ onDeviceOffline(() => handler("offline", false));
// Handle page visibility change
document.addEventListener(visibilityChange, () =>
@@ -44,3 +45,9 @@ function onDeviceOnline(handler) {
handler && handler();
});
}
+
+function onDeviceOffline(handler) {
+ window.addEventListener("offline", function () {
+ handler && handler();
+ });
+}