From 2eebfcf6fe873595a69ac0de805811fa7ed94f12 Mon Sep 17 00:00:00 2001
From: lloydzhou <lloydzhou@qq.com>
Date: Fri, 11 Oct 2024 11:29:22 +0800
Subject: [PATCH 1/5] client app tauri updater #2966

---
 app/components/settings.tsx | 16 ++++++++++++----
 app/global.d.ts             |  7 +++++++
 app/locales/cn.ts           |  2 ++
 app/locales/en.ts           |  2 ++
 app/store/update.ts         |  2 ++
 app/utils.ts                | 23 +++++++++++++++++++++++
 6 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/app/components/settings.tsx b/app/components/settings.tsx
index 9f338718e28..9ee919b8d7f 100644
--- a/app/components/settings.tsx
+++ b/app/components/settings.tsx
@@ -49,7 +49,7 @@ import Locale, {
   changeLang,
   getLang,
 } from "../locales";
-import { copyToClipboard } from "../utils";
+import { copyToClipboard, clientUpdate } from "../utils";
 import Link from "next/link";
 import {
   Anthropic,
@@ -1357,9 +1357,17 @@ export function Settings() {
             {checkingUpdate ? (
               <LoadingIcon />
             ) : hasNewVersion ? (
-              <Link href={updateUrl} target="_blank" className="link">
-                {Locale.Settings.Update.GoToUpdate}
-              </Link>
+              clientConfig?.isApp ? (
+                <IconButton
+                  icon={<ResetIcon></ResetIcon>}
+                  text={Locale.Settings.Update.GoToUpdate}
+                  onClick={() => clientUpdate()}
+                />
+              ) : (
+                <Link href={updateUrl} target="_blank" className="link">
+                  {Locale.Settings.Update.GoToUpdate}
+                </Link>
+              )
             ) : (
               <IconButton
                 icon={<ResetIcon></ResetIcon>}
diff --git a/app/global.d.ts b/app/global.d.ts
index 8ee636bcd3c..897871fec37 100644
--- a/app/global.d.ts
+++ b/app/global.d.ts
@@ -26,6 +26,13 @@ declare interface Window {
       isPermissionGranted(): Promise<boolean>;
       sendNotification(options: string | Options): void;
     };
+    updater: {
+      checkUpdate(): Promise<UpdateResult>;
+      installUpdate(): Promise<void>;
+      onUpdaterEvent(
+        handler: (status: UpdateStatusResult) => void,
+      ): Promise<UnlistenFn>;
+    };
     http: {
       fetch<T>(
         url: string,
diff --git a/app/locales/cn.ts b/app/locales/cn.ts
index e5bcca0edbb..3086cc63e3e 100644
--- a/app/locales/cn.ts
+++ b/app/locales/cn.ts
@@ -205,6 +205,8 @@ const cn = {
       IsChecking: "正在检查更新...",
       FoundUpdate: (x: string) => `发现新版本:${x}`,
       GoToUpdate: "前往更新",
+      Success: "Update Succesfull.",
+      Failed: "Update Failed.",
     },
     SendKey: "发送键",
     Theme: "主题",
diff --git a/app/locales/en.ts b/app/locales/en.ts
index 1204575224a..a025a522ff5 100644
--- a/app/locales/en.ts
+++ b/app/locales/en.ts
@@ -207,6 +207,8 @@ const en: LocaleType = {
       IsChecking: "Checking update...",
       FoundUpdate: (x: string) => `Found new version: ${x}`,
       GoToUpdate: "Update",
+      Success: "Update Succesfull.",
+      Failed: "Update Failed.",
     },
     SendKey: "Send Key",
     Theme: "Theme",
diff --git a/app/store/update.ts b/app/store/update.ts
index e68fde369d5..327dc5e88f9 100644
--- a/app/store/update.ts
+++ b/app/store/update.ts
@@ -6,6 +6,7 @@ import {
 } from "../constant";
 import { getClientConfig } from "../config/client";
 import { createPersistStore } from "../utils/store";
+import { clientUpdate } from "../utils";
 import ChatGptIcon from "../icons/chatgpt.png";
 import Locale from "../locales";
 import { ClientApi } from "../client/api";
@@ -119,6 +120,7 @@ export const useUpdateStore = createPersistStore(
                           icon: `${ChatGptIcon.src}`,
                           sound: "Default",
                         });
+                        clientUpdate();
                       }
                     }
                   });
diff --git a/app/utils.ts b/app/utils.ts
index 5d45017107c..9e75ffc7fc7 100644
--- a/app/utils.ts
+++ b/app/utils.ts
@@ -386,3 +386,26 @@ export function getOperationId(operation: {
     `${operation.method.toUpperCase()}${operation.path.replaceAll("/", "_")}`
   );
 }
+
+export function clientUpdate() {
+  // this a wild for updating client app
+  return window.__TAURI__?.updater
+    .checkUpdate()
+    .then((updateResult) => {
+      if (updateResult.shouldUpdate) {
+        window.__TAURI__?.updater
+          .installUpdate()
+          .then((result) => {
+            showToast(Locale.Settings.Update.Success);
+          })
+          .catch((e) => {
+            console.error("[Install Update Error]", e);
+            showToast(Locale.Settings.Update.Failed);
+          });
+      }
+    })
+    .catch((e) => {
+      console.error("[Check Update Error]", e);
+      showToast(Locale.Settings.Update.Failed);
+    });
+}

From bd9de4dc4db95a6d9aca4567d6147cb5b55b38ab Mon Sep 17 00:00:00 2001
From: lloydzhou <lloydzhou@qq.com>
Date: Fri, 11 Oct 2024 11:42:36 +0800
Subject: [PATCH 2/5] fix version compare

---
 app/components/settings.tsx |  4 ++--
 app/utils.ts                | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/app/components/settings.tsx b/app/components/settings.tsx
index 9ee919b8d7f..c5653543cae 100644
--- a/app/components/settings.tsx
+++ b/app/components/settings.tsx
@@ -49,7 +49,7 @@ import Locale, {
   changeLang,
   getLang,
 } from "../locales";
-import { copyToClipboard, clientUpdate } from "../utils";
+import { copyToClipboard, clientUpdate, semverCompare } from "../utils";
 import Link from "next/link";
 import {
   Anthropic,
@@ -585,7 +585,7 @@ export function Settings() {
   const [checkingUpdate, setCheckingUpdate] = useState(false);
   const currentVersion = updateStore.formatVersion(updateStore.version);
   const remoteId = updateStore.formatVersion(updateStore.remoteVersion);
-  const hasNewVersion = currentVersion !== remoteId;
+  const hasNewVersion = semverCompare(currentVersion, remoteId) === -1;
   const updateUrl = getClientConfig()?.isApp ? RELEASE_URL : UPDATE_URL;
 
   function checkUpdate(force = false) {
diff --git a/app/utils.ts b/app/utils.ts
index 9e75ffc7fc7..9e9e90f66af 100644
--- a/app/utils.ts
+++ b/app/utils.ts
@@ -409,3 +409,14 @@ export function clientUpdate() {
       showToast(Locale.Settings.Update.Failed);
     });
 }
+
+// https://gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
+export function semverCompare(a, b) {
+  if (a.startsWith(b + "-")) return -1;
+  if (b.startsWith(a + "-")) return 1;
+  return a.localeCompare(b, undefined, {
+    numeric: true,
+    sensitivity: "case",
+    caseFirst: "upper",
+  });
+}

From a0d4a04192e2221f0ca969cab3236d4090a85955 Mon Sep 17 00:00:00 2001
From: lloydzhou <lloydzhou@qq.com>
Date: Fri, 11 Oct 2024 11:52:24 +0800
Subject: [PATCH 3/5] update

---
 app/locales/en.ts | 2 +-
 app/utils.ts      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/locales/en.ts b/app/locales/en.ts
index a025a522ff5..40471536ff9 100644
--- a/app/locales/en.ts
+++ b/app/locales/en.ts
@@ -207,7 +207,7 @@ const en: LocaleType = {
       IsChecking: "Checking update...",
       FoundUpdate: (x: string) => `Found new version: ${x}`,
       GoToUpdate: "Update",
-      Success: "Update Succesfull.",
+      Success: "Update Successful.",
       Failed: "Update Failed.",
     },
     SendKey: "Send Key",
diff --git a/app/utils.ts b/app/utils.ts
index 9e9e90f66af..d8fc46330d1 100644
--- a/app/utils.ts
+++ b/app/utils.ts
@@ -411,7 +411,7 @@ export function clientUpdate() {
 }
 
 // https://gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
-export function semverCompare(a, b) {
+export function semverCompare(a: string, b: string) {
   if (a.startsWith(b + "-")) return -1;
   if (b.startsWith(a + "-")) return 1;
   return a.localeCompare(b, undefined, {

From 819238acaf5114329168f2c95da74d747795daa1 Mon Sep 17 00:00:00 2001
From: Dogtiti <499960698@qq.com>
Date: Fri, 11 Oct 2024 20:49:43 +0800
Subject: [PATCH 4/5] fix: i18n

---
 app/locales/cn.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/locales/cn.ts b/app/locales/cn.ts
index 3086cc63e3e..1e0116ec915 100644
--- a/app/locales/cn.ts
+++ b/app/locales/cn.ts
@@ -205,8 +205,8 @@ const cn = {
       IsChecking: "正在检查更新...",
       FoundUpdate: (x: string) => `发现新版本:${x}`,
       GoToUpdate: "前往更新",
-      Success: "Update Succesfull.",
-      Failed: "Update Failed.",
+      Success: "更新成功!",
+      Failed: "更新失败",
     },
     SendKey: "发送键",
     Theme: "主题",

From 87d85c10c3b3dcd7a62e174dabb6338f005ce9b7 Mon Sep 17 00:00:00 2001
From: lloydzhou <lloydzhou@qq.com>
Date: Mon, 14 Oct 2024 21:48:36 +0800
Subject: [PATCH 5/5] update

---
 src-tauri/tauri.conf.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json
index cc137ee8afd..56a5b46a9e6 100644
--- a/src-tauri/tauri.conf.json
+++ b/src-tauri/tauri.conf.json
@@ -99,7 +99,7 @@
       "endpoints": [
         "https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/releases/latest/download/latest.json"
       ],
-      "dialog": false,
+      "dialog": true,
       "windows": {
         "installMode": "passive"
       },