Skip to content

Commit

Permalink
web: replace force sync with force push/pull
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed May 9, 2024
1 parent 837c296 commit 9a866a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/components/announcements/body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function CalltoAction({ action, variant, sx, dismissAnnouncement }) {
break;
}
case "force-sync": {
await appStore.sync(true, true);
await appStore.sync({ type: "full", force: true });
break;
}
case "backup": {
Expand Down
49 changes: 46 additions & 3 deletions apps/web/src/dialogs/settings/sync-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

import { SettingsGroup } from "./types";
import { useStore as useAppStore } from "../../stores/app-store";
import { confirm } from "../../common/dialog-controller";

export const SyncSettings: SettingsGroup[] = [
{
Expand Down Expand Up @@ -78,14 +79,56 @@ export const SyncSettings: SettingsGroup[] = [
{
key: "force-sync",
title: "Having problems with sync?",
description: "Try force sync to resolve issues with syncing.",
description: `Force push:
Use this if some changes from this device are not appearing on other devices.This will push everything on this device and overwrite whatever is one the server.
Force pull:
Use this if some changes are not appearing on this device from other devices. This will pull everything from the server and overwrite with whatever is one this device.
**These must only be used for troubleshooting. Using them regularly for sync is not recommended and will lead to unexpected data loss and other issues. If you are having persistent issues with sync, please report them to us at support@streetwriters.co.**`,
keywords: ["force sync", "sync troubleshoot"],
components: [
{
type: "button",
title: "Force sync",
title: "Force push",
variant: "error",
action: () =>
confirm({
title: "Are you sure?",
message:
"This must only be used for troubleshooting. Using them regularly for sync is **not recommended** and will lead to **unexpected data loss** and other issues. If you are having persistent issues with sync, please report them to us at support@streetwriters.co.",
checks: {
accept: { text: "I understand.", default: false }
},
positiveButtonText: "Proceed",
negativeButtonText: "Cancel"
}).then((result) => {
if (!result || !result.accept) return;
return useAppStore
.getState()
.sync({ force: true, type: "send" });
})
},
{
type: "button",
title: "Force pull",
variant: "error",
action: () => useAppStore.getState().sync(true, true)
action: () =>
confirm({
title: "Are you sure?",
message:
"This must only be used for troubleshooting. Using them regularly for sync is **not recommended** and will lead to **unexpected data loss** and other issues. If you are having persistent issues with sync, please report them to us at support@streetwriters.co.",
checks: {
accept: { text: "I understand.", default: false }
},
positiveButtonText: "Proceed",
negativeButtonText: "Cancel"
}).then((result) => {
if (!result || !result.accept) return;
return useAppStore
.getState()
.sync({ force: true, type: "fetch" });
})
}
]
}
Expand Down
27 changes: 10 additions & 17 deletions apps/web/src/stores/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
} from "../utils/page-visibility";
import { NetworkCheck } from "../utils/network-check";
import { Color, Notebook, Tag } from "@notesnook/core";
import { SyncOptions } from "@notesnook/core/dist/api/sync";

type SyncState =
| "synced"
Expand All @@ -57,7 +58,7 @@ type SyncStatus = {
};
const networkCheck = new NetworkCheck();
let syncStatusTimeout = 0;
let pendingSync: { full: boolean } | undefined = undefined;
let pendingSync: SyncOptions | undefined = undefined;

class AppStore extends BaseStore<AppStore> {
// default state
Expand Down Expand Up @@ -115,9 +116,9 @@ class AppStore extends BaseStore<AppStore> {

db.eventManager.subscribe(
EVENTS.databaseSyncRequested,
async (full, force, lastSynced) => {
async (full, force) => {
if (!this.get().isAutoSyncEnabled) return;
await this.get().sync(full, force, lastSynced);
await this.get().sync({ type: full ? "full" : "send", force });
}
);

Expand Down Expand Up @@ -268,22 +269,20 @@ class AppStore extends BaseStore<AppStore> {
this.set((state) => (state.lastSynced = lastSynced));
};

sync = async (full = true, force = false, lastSynced?: number) => {
sync = async (options: SyncOptions = { type: "full", force: false }) => {
if (
this.isSyncing() ||
!this.get().isSyncEnabled ||
!navigator.onLine ||
!(await networkCheck.waitForInternet())
) {
logger.info("Ignoring duplicate sync", {
full,
force,
lastSynced,
options,
syncing: this.isSyncing(),
syncDisabled: !this.get().isSyncEnabled,
offline: !navigator.onLine
});
if (this.isSyncing()) pendingSync = { full };
if (this.isSyncing()) pendingSync = options;
return;
}

Expand All @@ -292,10 +291,7 @@ class AppStore extends BaseStore<AppStore> {

this.updateSyncStatus("syncing");
try {
const result = await db.sync({
type: full ? "full" : "send",
force
});
const result = await db.sync(options);

if (!result) return this.updateSyncStatus("failed");
this.updateSyncStatus("completed", true);
Expand All @@ -304,9 +300,9 @@ class AppStore extends BaseStore<AppStore> {

if (pendingSync) {
logger.info("Running pending sync", pendingSync);
const isFullSync = pendingSync.full;
const syncOptions = { ...pendingSync };
pendingSync = undefined;
await this.get().sync(isFullSync, false);
await this.get().sync(syncOptions);
}
} catch (err) {
if (!(err instanceof Error)) {
Expand All @@ -316,9 +312,6 @@ class AppStore extends BaseStore<AppStore> {

logger.error(err);
if (err.cause === "MERGE_CONFLICT") {
// TODO: reopen conflicted note
// const sessionId = editorstore.get().session.id;
// if (sessionId) await editorstore.openSession(sessionId, true);
await this.refresh();
this.updateSyncStatus("conflicts");
} else {
Expand Down

0 comments on commit 9a866a5

Please sign in to comment.