Skip to content

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

feat: add auto updater frontend ui

thecodrr8e8e361d1e71
diff --git a/apps/web/src/commands/download-update.js b/apps/web/src/commands/download-update.js
new file mode 100644
index 000000000..701430eb9
--- /dev/null
+++ b/apps/web/src/commands/download-update.js
@@ -0,0 +1,5 @@
+import { invokeCommand } from "./index";
+
+export default function downloadUpdate() {
+ invokeCommand("downloadUpdate");
+}
diff --git a/apps/web/src/commands/index.js b/apps/web/src/commands/index.js
new file mode 100644
index 000000000..d1b246c1f
--- /dev/null
+++ b/apps/web/src/commands/index.js
@@ -0,0 +1,18 @@
+import EventManager from "notes-core/utils/eventmanager";
+import { isDesktop } from "../utils/platform";
+
+export const ElectronEventManager = new EventManager();
+
+export function invokeCommand(type, payload = {}) {
+ window.api.send("fromRenderer", {
+ type,
+ ...payload,
+ });
+}
+
+if (isDesktop()) {
+ window.api.receive("fromMain", (args) => {
+ const { type } = args;
+ ElectronEventManager.publish(type, args);
+ });
+}
diff --git a/apps/web/src/commands/install-update.js b/apps/web/src/commands/install-update.js
new file mode 100644
index 000000000..9fb9e9240
--- /dev/null
+++ b/apps/web/src/commands/install-update.js
@@ -0,0 +1,5 @@
+import { invokeCommand } from "./index";
+
+export default function installUpdate() {
+ invokeCommand("installUpdate");
+}
diff --git a/apps/web/src/components/status-bar/index.js b/apps/web/src/components/status-bar/index.js
index ae336a2dd..47f1b3713 100644
--- a/apps/web/src/components/status-bar/index.js
+++ b/apps/web/src/components/status-bar/index.js
@@ -7,6 +7,9 @@ import { useStore as useAppStore } from "../../stores/app-store";
import { showLogInDialog } from "../../common/dialog-controller";
import TimeAgo from "timeago-react";
import { hashNavigate, navigate } from "../../navigation";
+import useAutoUpdater from "../../hooks/use-auto-updater";
+import downloadUpdate from "../../commands/download-update";
+import installUpdate from "../../commands/install-update";
function StatusBar() {
const user = useUserStore((state) => state.user);
@@ -15,6 +18,7 @@ function StatusBar() {
const isLoggedIn = useUserStore((state) => state.isLoggedIn);
const isSyncing = useAppStore((state) => state.isSyncing);
const processingStatus = useAppStore((state) => state.processingStatus);
+ const updateStatus = useAutoUpdater();
return (
<Flex
@@ -83,6 +87,36 @@ function StatusBar() {
</Text>
</Flex>
)}
+
+ {updateStatus && (
+ <Button
+ variant="statusitem"
+ display="flex"
+ onClick={() => {
+ if (updateStatus.type === "available") {
+ downloadUpdate();
+ } else if (updateStatus.type === "completed") {
+ installUpdate();
+ }
+ }}
+ sx={{ alignItems: "center", justifyContent: "center" }}
+ >
+ <Icon.Update
+ rotate={
+ updateStatus.type !== "updated" &&
+ updateStatus.type !== "completed" &&
+ updateStatus.type !== "available"
+ }
+ color={
+ updateStatus.type === "available" ? "primary" : "fontTertiary"
+ }
+ size={12}
+ />
+ <Text variant="subBody" ml={1}>
+ {statusToInfoText(updateStatus)}
+ </Text>
+ </Button>
+ )}
</Flex>
<EditorFooter />
</Flex>
@@ -90,3 +124,18 @@ function StatusBar() {
}
export default StatusBar;
+
+function statusToInfoText(status) {
+ const { type, version, progress } = status;
+ return type === "checking"
+ ? "Checking for updates..."
+ : type === "updated"
+ ? "Latest version"
+ : type === "downloading"
+ ? `${Math.round(progress)}% updating...`
+ : type === "completed"
+ ? `v${version} downloaded (restart required)`
+ : type === "available"
+ ? `v${version} available`
+ : "";
+}
diff --git a/apps/web/src/hooks/use-auto-updater.js b/apps/web/src/hooks/use-auto-updater.js
new file mode 100644
index 000000000..826422372
--- /dev/null
+++ b/apps/web/src/hooks/use-auto-updater.js
@@ -0,0 +1,68 @@
+import { useEffect, useState } from "react";
+import { ElectronEventManager } from "../commands";
+import { EVENTS } from "@notesnook/desktop/events";
+import { isDesktop } from "../utils/platform";
+
+export default function useAutoUpdater() {
+ const [status, setStatus] = useState();
+
+ useEffect(() => {
+ function checkingForUpdate() {
+ setStatus({ type: "checking" });
+ }
+
+ function updateAvailable(info) {
+ setStatus({ type: "available", version: info.version });
+ }
+
+ function updateNotAvailable(info) {
+ setStatus({ type: "updated" });
+ }
+
+ function updateDownloadCompleted(info) {
+ setStatus({ type: "completed", version: info.version });
+ }
+
+ function updateDownloadProgress(progressInfo) {
+ setStatus({ type: "downloading", progress: progressInfo.percent });
+ }
+
+ ElectronEventManager.subscribe(EVENTS.checkingForUpdate, checkingForUpdate);
+ ElectronEventManager.subscribe(EVENTS.updateAvailable, updateAvailable);
+ ElectronEventManager.subscribe(
+ EVENTS.updateNotAvailable,
+ updateNotAvailable
+ );
+ ElectronEventManager.subscribe(
+ EVENTS.updateDownloadCompleted,
+ updateDownloadCompleted
+ );
+ ElectronEventManager.subscribe(
+ EVENTS.updateDownloadProgress,
+ updateDownloadProgress
+ );
+
+ if (isDesktop()) checkingForUpdate();
+ return () => {
+ ElectronEventManager.unsubscribe(
+ EVENTS.checkingForUpdate,
+ checkingForUpdate
+ );
+ ElectronEventManager.unsubscribe(
+ EVENTS.updateNotAvailable,
+ updateNotAvailable
+ );
+ ElectronEventManager.unsubscribe(EVENTS.updateAvailable, updateAvailable);
+ ElectronEventManager.unsubscribe(
+ EVENTS.updateDownloadCompleted,
+ updateDownloadCompleted
+ );
+ ElectronEventManager.unsubscribe(
+ EVENTS.updateDownloadProgress,
+ updateDownloadProgress
+ );
+ };
+ }, []);
+
+ return status;
+}
diff --git a/apps/web/src/index.js b/apps/web/src/index.js
index 8131c6173..47a858459 100644
--- a/apps/web/src/index.js
+++ b/apps/web/src/index.js
@@ -1,3 +1,4 @@
+import "./commands";
import React from "react";
import { initializeDatabase } from "./common/db";
import "./index.css";