Skip to content

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

feat: add update delivery for web app

thecodrre0fd39aaf8e0
diff --git a/apps/web/package.json b/apps/web/package.json
index 2824314f9..ffe39efe9 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -53,6 +53,7 @@
"react-virtualized-auto-sizer": "^1.0.4",
"react-virtuoso": "^1.9.3",
"rebass": "^4.0.7",
+ "showdown": "github:thecodrr/showdown",
"streamablefs": "file:packages/streamablefs",
"timeago.js": "^4.0.2",
"tinymce": "^5.10.2",
diff --git a/apps/web/src/commands/check-for-update.js b/apps/web/src/commands/check-for-update.js
index 160792af4..87262b4d1 100644
--- a/apps/web/src/commands/check-for-update.js
+++ b/apps/web/src/commands/check-for-update.js
@@ -1,5 +1,34 @@
+import { EVENTS } from "@notesnook/desktop/events";
+import { AppEventManager } from "../common/app-events";
+import { isDesktop } from "../utils/platform";
+import { appVersion, getServiceWorkerVersion } from "../utils/version";
import { invokeCommand } from "./index";
-export default function checkForUpdate() {
- invokeCommand("checkForUpdate");
+export default async function checkForUpdate() {
+ if (isDesktop()) invokeCommand("checkForUpdate");
+ else {
+ AppEventManager.publish(EVENTS.checkingForUpdate);
+
+ const registrations =
+ (await navigator.serviceWorker?.getRegistrations()) || [];
+ for (let registration of registrations) {
+ await registration.update();
+ if (registration.waiting) {
+ const workerVersion = await getServiceWorkerVersion(
+ registration.waiting
+ );
+ if (!workerVersion || workerVersion.numerical <= appVersion.numerical) {
+ registration.waiting.postMessage({ type: "SKIP_WAITING" });
+ continue;
+ }
+
+ AppEventManager.publish(EVENTS.updateDownloadCompleted, {
+ version: workerVersion.formatted,
+ });
+ return;
+ }
+ }
+
+ AppEventManager.publish(EVENTS.updateNotAvailable);
+ }
}
diff --git a/apps/web/src/commands/download-update.js b/apps/web/src/commands/download-update.js
index 701430eb9..d25778f4c 100644
--- a/apps/web/src/commands/download-update.js
+++ b/apps/web/src/commands/download-update.js
@@ -1,5 +1,12 @@
+import { isDesktop } from "../utils/platform";
import { invokeCommand } from "./index";
-export default function downloadUpdate() {
- invokeCommand("downloadUpdate");
+export default async function downloadUpdate() {
+ if (isDesktop()) invokeCommand("downloadUpdate");
+ else {
+ console.log("Force updating");
+ if (!("serviceWorker" in navigator)) return;
+ const registration = await navigator.serviceWorker.ready;
+ await registration.update();
+ }
}
diff --git a/apps/web/src/commands/install-update.js b/apps/web/src/commands/install-update.js
index 9fb9e9240..d072e2726 100644
--- a/apps/web/src/commands/install-update.js
+++ b/apps/web/src/commands/install-update.js
@@ -1,5 +1,18 @@
+import { isDesktop } from "../utils/platform";
import { invokeCommand } from "./index";
-export default function installUpdate() {
- invokeCommand("installUpdate");
+export default async function installUpdate() {
+ if (isDesktop()) invokeCommand("installUpdate");
+ else {
+ const registrations =
+ (await navigator.serviceWorker?.getRegistrations()) || [];
+ let reload = false;
+ for (let registration of registrations) {
+ if (registration.waiting) {
+ registration.waiting.postMessage({ type: "SKIP_WAITING" });
+ reload = true;
+ }
+ }
+ if (reload) window.location.reload();
+ }
}
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 15904f37a..1f2d045eb 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -16,6 +16,10 @@ import Config from "../utils/config";
import Dialogs from "../components/dialogs";
import { Mutex } from "async-mutex";
import { formatDate } from "notes-core/utils/date";
+import downloadUpdate from "../commands/download-update";
+import installUpdate from "../commands/install-update";
+import { getChangelog } from "../utils/version";
+import { isDesktop } from "../utils/platform";
const DialogMutex = new Mutex();
function showDialog(dialog) {
@@ -231,34 +235,6 @@ export function showAppUpdatedNotice(version) {
});
}
-export function showAppAvailableNotice(version) {
- return confirm({
- title: `New version available`,
- message: (
- <Flex
- flexDirection="column"
- bg="bgSecondary"
- p={1}
- sx={{ borderRadius: "default" }}
- >
- <Text variant="title">v{version.formatted} changelog:</Text>
- <Text
- overflow="auto"
- as="pre"
- fontFamily="monospace"
- variant="body"
- mt={1}
- sx={{ overflow: "auto" }}
- >
- {version.changelog || "No change log."}
- </Text>
- </Flex>
- ),
- yesText: `Update now`,
- yesAction: () => window.location.reload(),
- });
-}
-
export function showEmailVerificationDialog() {
return showDialog((Dialogs, perform) => (
<Dialogs.EmailVerificationDialog onCancel={() => perform(false)} />
@@ -622,3 +598,56 @@ export function showInvalidSystemTimeDialog({ serverTime, localTime }) {
/>
));
}
+
+export function showUpdateAvailableNotice({ changelog, version }) {
+ return showUpdateDialog({
+ title: `New version available`,
+ subtitle: `v${version} is available for download`,
+ changelog,
+ action: { text: `Update now`, onClick: () => downloadUpdate() },
+ });
+}
+
+export async function showUpdateReadyNotice({ version }) {
+ const changelog = isDesktop() ? null : await getChangelog(version);
+ return await showUpdateDialog({
+ title: `Update ready for installation`,
+ subtitle: `v${version} is ready to be installed.`,
+ changelog,
+ action: {
+ text: "Install now",
+ onClick: () => installUpdate(),
+ },
+ });
+}
+
+function showUpdateDialog({ title, subtitle, changelog, action }) {
+ return confirm({
+ title,
+ subtitle,
+ message: changelog && (
+ <Flex flexDirection="column" sx={{ borderRadius: "default" }}>
+ <Text
+ as="div"
+ overflow="auto"
+ variant="body"
+ sx={{ overflow: "auto", fontFamily: "body" }}
+ css={`
+ h2 {
+ font-size: 1.2em;
+ font-weight: 600;
+ }
+ h3 {
+ font-size: 1em;
+ font-weight: 600;
+ }
+ `}
+ dangerouslySetInnerHTML={{ __html: changelog }}
+ ></Text>
+ </Flex>
+ ),
+ width: "500px",
+ yesText: action.text,
+ yesAction: action.onClick,
+ });
+}
diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.js
index cb7f24fa9..2c4ab0a07 100644
--- a/apps/web/src/components/icons/index.js
+++ b/apps/web/src/components/icons/index.js
@@ -125,6 +125,8 @@ import {
mdiBugOutline,
mdiLinkVariant,
mdiLinkVariantOff,
+ mdiNetworkOffOutline,
+ mdiCheckNetworkOutline,
} from "@mdi/js";
import { useTheme } from "emotion-theming";
import { AnimatedFlex } from "../animated";
@@ -317,3 +319,5 @@ export const Timebomb = createIcon(mdiBomb);
export const Issue = createIcon(mdiBugOutline);
export const ShortcutLink = createIcon(mdiLinkVariant);
export const RemoveShortcutLink = createIcon(mdiLinkVariantOff);
+export const Offline = createIcon(mdiNetworkOffOutline);
+export const Online = createIcon(mdiCheckNetworkOutline);
diff --git a/apps/web/src/components/status-bar/index.js b/apps/web/src/components/status-bar/index.js
index 6128a6a79..46a96e1bc 100644
--- a/apps/web/src/components/status-bar/index.js
+++ b/apps/web/src/components/status-bar/index.js
@@ -15,9 +15,12 @@ import { useStore as useAppStore } from "../../stores/app-store";
import TimeAgo from "../time-ago";
import { hardNavigate, hashNavigate, navigate } from "../../navigation";
import useAutoUpdater from "../../hooks/use-auto-updater";
-import downloadUpdate from "../../commands/download-update";
import installUpdate from "../../commands/install-update";
import checkForUpdate from "../../commands/check-for-update";
+import {
+ showIssueDialog,
+ showUpdateAvailableNotice,
+} from "../../common/dialog-controller";
import useStatus from "../../hooks/use-status";
import { getIconFromAlias } from "../icons/resolver";
@@ -109,9 +112,9 @@ function StatusBar() {
<Button
variant="statusitem"
display="flex"
- onClick={() => {
+ onClick={async () => {
if (updateStatus.type === "available") {
- downloadUpdate();
+ await showUpdateAvailableNotice(updateStatus);
} else if (updateStatus.type === "completed") {
installUpdate();
} else {
diff --git a/apps/web/src/hooks/use-auto-updater.js b/apps/web/src/hooks/use-auto-updater.js
index 210b9b8c6..8e4d5e9b7 100644
--- a/apps/web/src/hooks/use-auto-updater.js
+++ b/apps/web/src/hooks/use-auto-updater.js
@@ -1,8 +1,12 @@
import { useEffect, useState } from "react";
import { AppEventManager } from "../common/app-events";
import { EVENTS } from "@notesnook/desktop/events";
-import { isDesktop } from "../utils/platform";
+import {
+ showUpdateAvailableNotice,
+ showUpdateReadyNotice,
+} from "../common/dialog-controller";
import checkForUpdate from "../commands/check-for-update";
+import { isDesktop } from "../utils/platform";
var checkingForUpdateTimeout = 0;
export default function useAutoUpdater() {
@@ -22,15 +26,25 @@ export default function useAutoUpdater() {
}
function updateAvailable(info) {
- changeStatus({ type: "available", version: info.version });
+ changeStatus({
+ type: "available",
+ version: info.version,
+ changelog: info.releaseNotes,
+ });
+ showUpdateAvailableNotice({
+ changelog: info.releaseNotes,
+ version: info.version,
+ });
}
function updateNotAvailable() {
- changeStatus({ type: "updated" });
+ if (isDesktop()) changeStatus({ type: "updated" });
+ else changeStatus();
}
function updateDownloadCompleted(info) {
changeStatus({ type: "completed", version: info.version });
+ showUpdateReadyNotice({ version: info.version });
}
function updateDownloadProgress(progressInfo) {
@@ -58,9 +72,8 @@ export default function useAutoUpdater() {
updateDownloadProgress
);
- if (isDesktop()) {
- checkingForUpdate();
- checkForUpdate();
+ checkingForUpdate();
+ checkForUpdate();
return () => {
checkingForUpdateEvent.unsubscribe();
diff --git a/apps/web/src/index.js b/apps/web/src/index.js
index 329d48863..b2216c89e 100644
--- a/apps/web/src/index.js
+++ b/apps/web/src/index.js
@@ -1,8 +1,12 @@
+import { EVENTS } from "@notesnook/desktop/events";
import { render } from "react-dom";
+import { AppEventManager } from "./common/app-events";
+import { updateStatus } from "./hooks/use-status";
import { getCurrentHash, getCurrentPath, makeURL } from "./navigation";
import * as serviceWorker from "./serviceWorkerRegistration";
import Config from "./utils/config";
import { isTesting } from "./utils/platform";
+import { getServiceWorkerVersion } from "./utils/version";
if (process.env.REACT_APP_PLATFORM === "desktop") require("./commands");
const ROUTES = {
@@ -68,8 +72,31 @@ route?.component()?.then(({ default: Component }) => {
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
-if (process.env.REACT_APP_PLATFORM !== "desktop") serviceWorker.register();
-else serviceWorker.unregister();
+if (process.env.REACT_APP_PLATFORM !== "desktop") {
+ serviceWorker.register({
+ onUpdate: async (registration) => {
+ if (!registration.waiting) return;
+ const { formatted } = await getServiceWorkerVersion(registration.waiting);
+ AppEventManager.publish(EVENTS.updateDownloadCompleted, {
+ version: formatted,
+ });
+ },
+ onSuccess: () => {
+ updateStatus({
+ key: "pwaStatus",
+ status: "Ready for offline use.",
+ icon: "online",
+ });
+ },
+ onError: () => {
+ updateStatus({
+ key: "pwaStatus",
+ status: "Offline",
+ icon: "offline",
+ });
+ },
+ });
+} else serviceWorker.unregister();
function shouldSkipInitiation() {
return localStorage.getItem("skipInitiation") || false;