From 135d0918b329d2c0ffe51a7c7cedfb327d2821da Mon Sep 17 00:00:00 2001 From: reya <123083837+reyamir@users.noreply.github.com> Date: Sun, 12 May 2024 15:07:53 +0700 Subject: [PATCH] feat: add check for updates --- apps/desktop2/src/routes/index.tsx | 4 +++ packages/utils/index.ts | 1 + packages/utils/src/update.ts | 38 +++++++++++++++++++++++++ src-tauri/capabilities/main.json | 13 ++++----- src-tauri/gen/schemas/capabilities.json | 2 +- 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 packages/utils/src/update.ts diff --git a/apps/desktop2/src/routes/index.tsx b/apps/desktop2/src/routes/index.tsx index b04b50676..df317f454 100644 --- a/apps/desktop2/src/routes/index.tsx +++ b/apps/desktop2/src/routes/index.tsx @@ -1,5 +1,6 @@ import { PlusIcon } from "@lume/icons"; import { Spinner, User } from "@lume/ui"; +import { checkForAppUpdates } from "@lume/utils"; import { Link } from "@tanstack/react-router"; import { createFileRoute, redirect } from "@tanstack/react-router"; import { invoke } from "@tauri-apps/api/core"; @@ -8,6 +9,9 @@ import { toast } from "sonner"; export const Route = createFileRoute("/")({ beforeLoad: async ({ context }) => { + // check for app updates + await checkForAppUpdates(true); + const ark = context.ark; const accounts = await ark.get_all_accounts(); diff --git a/packages/utils/index.ts b/packages/utils/index.ts index 1db886cb3..5a057150f 100644 --- a/packages/utils/index.ts +++ b/packages/utils/index.ts @@ -10,6 +10,7 @@ export * from "./src/image"; export * from "./src/parser"; export * from "./src/groupBy"; export * from "./src/invoice"; +export * from "./src/update"; // Hooks export * from "./src/hooks/useNetworkStatus"; diff --git a/packages/utils/src/update.ts b/packages/utils/src/update.ts new file mode 100644 index 000000000..2ac52ccc1 --- /dev/null +++ b/packages/utils/src/update.ts @@ -0,0 +1,38 @@ +import { check } from "@tauri-apps/plugin-updater"; +import { ask, message } from "@tauri-apps/plugin-dialog"; +import { relaunch } from "@tauri-apps/plugin-process"; + +export async function checkForAppUpdates(silent: boolean) { + const update = await check(); + + if (!update) { + if (silent) return; + + await message("You are on the latest version. Stay awesome!", { + title: "No Update Available", + kind: "info", + okLabel: "OK", + }); + + return; + } + + if (update?.available) { + const yes = await ask( + `Update to ${update.version} is available!\n\nRelease notes: ${update.body}`, + { + title: "Update Available", + kind: "info", + okLabel: "Update", + cancelLabel: "Cancel", + }, + ); + + if (yes) { + await update.downloadAndInstall(); + await relaunch(); + } + + return; + } +} diff --git a/src-tauri/capabilities/main.json b/src-tauri/capabilities/main.json index b11fa0b23..72d4fcc6c 100644 --- a/src-tauri/capabilities/main.json +++ b/src-tauri/capabilities/main.json @@ -2,11 +2,7 @@ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "desktop-capability", "description": "Capability for the desktop", - "platforms": [ - "linux", - "macOS", - "windows" - ], + "platforms": ["linux", "macOS", "windows"], "windows": [ "main", "splash", @@ -33,8 +29,9 @@ "notification:default", "os:allow-locale", "os:allow-platform", - "updater:allow-check", "updater:default", + "updater:allow-check", + "updater:allow-download-and-install", "window:allow-start-dragging", "window:allow-create", "window:allow-close", @@ -46,7 +43,9 @@ "webview:allow-set-webview-size", "webview:allow-set-webview-position", "webview:allow-webview-close", - "dialog:allow-open", + "dialog:default", + "dialog:allow-ask", + "dialog:allow-message", "fs:allow-read-file", "shell:allow-open", { diff --git a/src-tauri/gen/schemas/capabilities.json b/src-tauri/gen/schemas/capabilities.json index b68d1eaeb..fb7cfe462 100644 --- a/src-tauri/gen/schemas/capabilities.json +++ b/src-tauri/gen/schemas/capabilities.json @@ -1 +1 @@ -{"desktop-capability":{"identifier":"desktop-capability","description":"Capability for the desktop","local":true,"windows":["main","splash","settings","search","nwc","activity","zap-*","event-*","user-*","editor-*","column-*"],"permissions":["path:default","event:default","window:default","app:default","resources:default","menu:default","tray:default","notification:allow-is-permission-granted","notification:allow-request-permission","notification:default","os:allow-locale","os:allow-platform","updater:allow-check","updater:default","window:allow-start-dragging","window:allow-create","window:allow-close","window:allow-set-focus","clipboard-manager:allow-write","clipboard-manager:allow-read","webview:allow-create-webview-window","webview:allow-create-webview","webview:allow-set-webview-size","webview:allow-set-webview-position","webview:allow-webview-close","dialog:allow-open","fs:allow-read-file","shell:allow-open",{"identifier":"http:default","allow":[{"url":"http://**/"},{"url":"https://**/"}]},{"identifier":"fs:allow-read-text-file","allow":[{"path":"$RESOURCE/locales/*"},{"path":"$RESOURCE/resources/*"}]}],"platforms":["linux","macOS","windows"]}} \ No newline at end of file +{"desktop-capability":{"identifier":"desktop-capability","description":"Capability for the desktop","local":true,"windows":["main","splash","settings","search","nwc","activity","zap-*","event-*","user-*","editor-*","column-*"],"permissions":["path:default","event:default","window:default","app:default","resources:default","menu:default","tray:default","notification:allow-is-permission-granted","notification:allow-request-permission","notification:default","os:allow-locale","os:allow-platform","updater:default","updater:allow-check","updater:allow-download-and-install","window:allow-start-dragging","window:allow-create","window:allow-close","window:allow-set-focus","clipboard-manager:allow-write","clipboard-manager:allow-read","webview:allow-create-webview-window","webview:allow-create-webview","webview:allow-set-webview-size","webview:allow-set-webview-position","webview:allow-webview-close","dialog:default","dialog:allow-ask","dialog:allow-message","fs:allow-read-file","shell:allow-open",{"identifier":"http:default","allow":[{"url":"http://**/"},{"url":"https://**/"}]},{"identifier":"fs:allow-read-text-file","allow":[{"path":"$RESOURCE/locales/*"},{"path":"$RESOURCE/resources/*"}]}],"platforms":["linux","macOS","windows"]}} \ No newline at end of file