Skip to content

Commit cda4494

Browse files
authored
Merge pull request #5632 from ConnectAI-E/feature/H0llyW00dzZ-updater
Feature/h0lly w00dz z updater
2 parents 22f83c9 + 87d85c1 commit cda4494

File tree

7 files changed

+61
-6
lines changed

7 files changed

+61
-6
lines changed

app/components/settings.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import Locale, {
4949
changeLang,
5050
getLang,
5151
} from "../locales";
52-
import { copyToClipboard } from "../utils";
52+
import { copyToClipboard, clientUpdate, semverCompare } from "../utils";
5353
import Link from "next/link";
5454
import {
5555
Anthropic,
@@ -585,7 +585,7 @@ export function Settings() {
585585
const [checkingUpdate, setCheckingUpdate] = useState(false);
586586
const currentVersion = updateStore.formatVersion(updateStore.version);
587587
const remoteId = updateStore.formatVersion(updateStore.remoteVersion);
588-
const hasNewVersion = currentVersion !== remoteId;
588+
const hasNewVersion = semverCompare(currentVersion, remoteId) === -1;
589589
const updateUrl = getClientConfig()?.isApp ? RELEASE_URL : UPDATE_URL;
590590

591591
function checkUpdate(force = false) {
@@ -1357,9 +1357,17 @@ export function Settings() {
13571357
{checkingUpdate ? (
13581358
<LoadingIcon />
13591359
) : hasNewVersion ? (
1360-
<Link href={updateUrl} target="_blank" className="link">
1361-
{Locale.Settings.Update.GoToUpdate}
1362-
</Link>
1360+
clientConfig?.isApp ? (
1361+
<IconButton
1362+
icon={<ResetIcon></ResetIcon>}
1363+
text={Locale.Settings.Update.GoToUpdate}
1364+
onClick={() => clientUpdate()}
1365+
/>
1366+
) : (
1367+
<Link href={updateUrl} target="_blank" className="link">
1368+
{Locale.Settings.Update.GoToUpdate}
1369+
</Link>
1370+
)
13631371
) : (
13641372
<IconButton
13651373
icon={<ResetIcon></ResetIcon>}

app/global.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ declare interface Window {
2626
isPermissionGranted(): Promise<boolean>;
2727
sendNotification(options: string | Options): void;
2828
};
29+
updater: {
30+
checkUpdate(): Promise<UpdateResult>;
31+
installUpdate(): Promise<void>;
32+
onUpdaterEvent(
33+
handler: (status: UpdateStatusResult) => void,
34+
): Promise<UnlistenFn>;
35+
};
2936
http: {
3037
fetch<T>(
3138
url: string,

app/locales/cn.ts

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ const cn = {
205205
IsChecking: "正在检查更新...",
206206
FoundUpdate: (x: string) => `发现新版本:${x}`,
207207
GoToUpdate: "前往更新",
208+
Success: "更新成功!",
209+
Failed: "更新失败",
208210
},
209211
SendKey: "发送键",
210212
Theme: "主题",

app/locales/en.ts

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ const en: LocaleType = {
207207
IsChecking: "Checking update...",
208208
FoundUpdate: (x: string) => `Found new version: ${x}`,
209209
GoToUpdate: "Update",
210+
Success: "Update Successful.",
211+
Failed: "Update Failed.",
210212
},
211213
SendKey: "Send Key",
212214
Theme: "Theme",

app/store/update.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "../constant";
77
import { getClientConfig } from "../config/client";
88
import { createPersistStore } from "../utils/store";
9+
import { clientUpdate } from "../utils";
910
import ChatGptIcon from "../icons/chatgpt.png";
1011
import Locale from "../locales";
1112
import { ClientApi } from "../client/api";
@@ -119,6 +120,7 @@ export const useUpdateStore = createPersistStore(
119120
icon: `${ChatGptIcon.src}`,
120121
sound: "Default",
121122
});
123+
clientUpdate();
122124
}
123125
}
124126
});

app/utils.ts

+34
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,37 @@ export function getOperationId(operation: {
386386
`${operation.method.toUpperCase()}${operation.path.replaceAll("/", "_")}`
387387
);
388388
}
389+
390+
export function clientUpdate() {
391+
// this a wild for updating client app
392+
return window.__TAURI__?.updater
393+
.checkUpdate()
394+
.then((updateResult) => {
395+
if (updateResult.shouldUpdate) {
396+
window.__TAURI__?.updater
397+
.installUpdate()
398+
.then((result) => {
399+
showToast(Locale.Settings.Update.Success);
400+
})
401+
.catch((e) => {
402+
console.error("[Install Update Error]", e);
403+
showToast(Locale.Settings.Update.Failed);
404+
});
405+
}
406+
})
407+
.catch((e) => {
408+
console.error("[Check Update Error]", e);
409+
showToast(Locale.Settings.Update.Failed);
410+
});
411+
}
412+
413+
// https://gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
414+
export function semverCompare(a: string, b: string) {
415+
if (a.startsWith(b + "-")) return -1;
416+
if (b.startsWith(a + "-")) return 1;
417+
return a.localeCompare(b, undefined, {
418+
numeric: true,
419+
sensitivity: "case",
420+
caseFirst: "upper",
421+
});
422+
}

src-tauri/tauri.conf.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"endpoints": [
100100
"https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/releases/latest/download/latest.json"
101101
],
102-
"dialog": false,
102+
"dialog": true,
103103
"windows": {
104104
"installMode": "passive"
105105
},

0 commit comments

Comments
 (0)