Skip to content

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

web: use cronosjs for accurate backup scheduling and avoid manual string concatenation

kashaf-ansari-dev32dcde43e6d3
diff --git a/apps/web/src/dialogs/settings/backup-export-settings.ts b/apps/web/src/dialogs/settings/backup-export-settings.ts
index b6dad6943..cd7559f3b 100644
--- a/apps/web/src/dialogs/settings/backup-export-settings.ts
+++ b/apps/web/src/dialogs/settings/backup-export-settings.ts
@@ -27,6 +27,8 @@ import { useStore as useAppStore } from "../../stores/app-store";
import { useStore as useUserStore } from "../../stores/user-store";
import { desktop } from "../../common/desktop-bridge";
import { formatDate } from "@notesnook/core";
+import { BACKUP_CRON_EXPRESSIONS, FULL_BACKUP_CRON_EXPRESSIONS } from "../../common/notices";
+import { CronosExpression } from "cronosjs";
import dayjs from "dayjs";
function getNextBackupTime(
@@ -35,13 +37,19 @@ function getNextBackupTime(
isFull: boolean = false
): number | undefined {
if (offset === 0 || !lastBackupDate) return undefined;
- let days = 0;
- if (isFull) {
- days = offset === 1 ? 7 : 30;
- } else {
- days = offset === 1 ? 1 : offset === 2 ? 7 : 30;
+ try {
+ const cronString = isFull
+ ? FULL_BACKUP_CRON_EXPRESSIONS[offset as keyof typeof FULL_BACKUP_CRON_EXPRESSIONS]
+ : BACKUP_CRON_EXPRESSIONS[offset as keyof typeof BACKUP_CRON_EXPRESSIONS];
+
+ if (!cronString) return undefined;
+
+ const expr = CronosExpression.parse(cronString);
+ const nextDate = expr.nextDate(new Date(lastBackupDate));
+ return nextDate ? nextDate.getTime() : undefined;
+ } catch (e) {
+ return undefined;
}
- return dayjs(lastBackupDate).add(days, "d").valueOf();
}
const getDesktopBackupsDirectoryPath = () =>
@@ -64,14 +72,9 @@ export const BackupExportSettings: SettingsGroup[] = [
if (!last) return strings.backupNowDesc();
- const typeStr = last === full ? " (Full)" : " (Partial)";
- return `${strings.backupNowDesc()}\n${strings.lastBackupOn(
- formatDate(last, {
- type: "date-time",
- dateFormat: s.dateFormat,
- timeFormat: s.timeFormat
- })
- )}${typeStr}`;
+ const formattedDate = formatDate(last, { type: "date-time", dateFormat: s.dateFormat, timeFormat: s.timeFormat });
+ const backupStr = last === full ? strings.lastFullBackupOn(formattedDate) : strings.lastPartialBackupOn(formattedDate);
+ return `${strings.backupNowDesc()}\n${backupStr}`;
},
onStateChange: (listener) =>
useSettingStore.subscribe(
diff --git a/packages/intl/locale/en.po b/packages/intl/locale/en.po
index 1a02d2bf0..93cf2bb5e 100644
--- a/packages/intl/locale/en.po
+++ b/packages/intl/locale/en.po
@@ -4513,6 +4513,14 @@ msgstr "Last"
msgid "Last backup: {date}"
msgstr "Last backup: {date}"
+#: src/strings.ts:3281
+msgid "Last backup: {date} (Full)"
+msgstr "Last backup: {date} (Full)"
+
+#: src/strings.ts:3280
+msgid "Last backup: {date} (Partial)"
+msgstr "Last backup: {date} (Partial)"
+
#: src/strings.ts:2029
msgid "Last edited at"
msgstr "Last edited at"
diff --git a/packages/intl/locale/pseudo-LOCALE.po b/packages/intl/locale/pseudo-LOCALE.po
index 754886215..827b52ad3 100644
--- a/packages/intl/locale/pseudo-LOCALE.po
+++ b/packages/intl/locale/pseudo-LOCALE.po
@@ -4513,6 +4513,14 @@ msgstr "Last"
msgid "Last backup: {date}"
msgstr ""
+#: src/strings.ts:3281
+msgid "Last backup: {date} (Full)"
+msgstr ""
+
+#: src/strings.ts:3280
+msgid "Last backup: {date} (Partial)"
+msgstr ""
+
#: src/strings.ts:2029
msgid "Last edited at"
msgstr "Last edited at"
diff --git a/packages/intl/src/strings.ts b/packages/intl/src/strings.ts
index 6d7eda308..d1d29881b 100644
--- a/packages/intl/src/strings.ts
+++ b/packages/intl/src/strings.ts
@@ -3276,5 +3276,7 @@ Continue without attachments?`,
checkingAttachmentsProgress: (current: number, total: number) =>
t`Checking attachments (${current}/${total})`,
lastBackupOn: (date: string) => t`Last backup: ${date}`,
- nextBackupOn: (date: string) => t`Next backup: ${date}`
+ nextBackupOn: (date: string) => t`Next backup: ${date}`,
+ lastPartialBackupOn: (date: string) => t`Last backup: ${date} (Partial)`,
+ lastFullBackupOn: (date: string) => t`Last backup: ${date} (Full)`
};