Skip to content

Commit 34b0857

Browse files
committed
feat: 增加检查更新机制,更新后重启软件
1 parent 86ef184 commit 34b0857

5 files changed

Lines changed: 58 additions & 53 deletions

File tree

messages/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
"language": "Language",
3131
"checkUpdate": "Check for Updates",
3232
"checkError": "Failed to check for updates",
33-
"updateAvailable": "New version available",
33+
"updateAvailable": "Update to new version",
3434
"updateDownloading": "Updating {downloaded} / {contentLength}",
3535
"updateInstalled": "Restart app",
36+
"noUpdate": "Current version is the latest",
3637
"items": {
3738
"github": {
3839
"title": "GitHub",

messages/ja.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"updateAvailable": "新しいバージョンが利用可能です",
6666
"updateDownloading": "Updating {downloaded} / {contentLength}",
6767
"updateInstalled": "Restart app",
68+
"noUpdate": "現在已是最新版本",
6869
"items": {
6970
"github": {
7071
"title": "GitHub",

messages/zh.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@
6262
"language": "语言",
6363
"checkUpdate": "检查更新",
6464
"checkError": "检查更新失败",
65-
"updateAvailable": "检测到新版本",
65+
"updateAvailable": "更新至最新版本",
6666
"updateDownloading": "更新中 {downloaded} / {contentLength}",
6767
"updateInstalled": "重启应用",
68+
"noUpdate": "当前已是最新版本",
6869
"items": {
6970
"github": {
7071
"title": "GitHub",

src/app/core/setting/about/updater.tsx

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,90 @@
11
import { check, Update } from '@tauri-apps/plugin-updater';
22
import { relaunch } from '@tauri-apps/plugin-process';
3-
import { useState } from 'react';
3+
import { useEffect, useState } from 'react';
44
import { Button } from '@/components/ui/button';
55
import { useTranslations } from 'next-intl';
66
import useSettingStore from '@/stores/setting';
77
import Image from 'next/image';
88
import { toast } from '@/hooks/use-toast';
99
import { Badge } from '@/components/ui/badge';
10-
import { Loader2 } from 'lucide-react';
10+
import { ArrowBigRightDash, Loader2 } from 'lucide-react';
1111

1212
export default function Updater() {
1313
const t = useTranslations('settings.about');
14+
const [checking, setChecking] = useState(false);
1415
const [loading, setLoading] = useState(false);
15-
const [updateStatus, setUpdateStatus] = useState(t('checkUpdate'));
16-
const [downloaded, setDownloaded] = useState(0);
17-
const [contentLength, setContentLength] = useState(0);
1816
const { version } = useSettingStore();
17+
const [update, setUpdate] = useState<Update | null>(null);
18+
19+
async function checkUpdate() {
20+
setChecking(true);
21+
try {
22+
setUpdate(await check());
23+
} catch (error) {
24+
toast({
25+
title: t('checkError'),
26+
description: error as string,
27+
variant: 'destructive'
28+
});
29+
} finally {
30+
setChecking(false);
31+
}
32+
}
1933

2034
async function checkVersion() {
21-
if (updateStatus === t('checkUpdate')) {
22-
setLoading(true);
23-
let update: Update | null = null;
35+
setLoading(true);
36+
if (update) {
2437
try {
25-
update = await check();
38+
await update.downloadAndInstall();
2639
} catch (error) {
2740
toast({
2841
title: t('checkError'),
2942
description: error as string,
3043
variant: 'destructive'
3144
});
32-
} finally {
33-
setLoading(false);
3445
}
35-
if (update) {
36-
try {
37-
setUpdateStatus(t('updateAvailable', { version: update.version }));
38-
await update.downloadAndInstall((event) => {
39-
switch (event.event) {
40-
case 'Started':
41-
setContentLength(event.data.contentLength || 0);
42-
break;
43-
case 'Progress':
44-
setDownloaded(event.data.chunkLength || 0);
45-
setUpdateStatus(t('updateDownloading', { downloaded, contentLength }));
46-
break;
47-
case 'Finished':
48-
setUpdateStatus(t('updateInstalled'));
49-
break;
50-
}
51-
});
52-
} catch (error) {
53-
toast({
54-
title: t('checkError'),
55-
description: error as string,
56-
variant: 'destructive'
57-
});
58-
} finally {
59-
setLoading(false);
60-
}
61-
}
62-
} else if (updateStatus === t('updateInstalled')) {
6346
await relaunch();
47+
} else {
48+
toast({
49+
title: t('checkError'),
50+
description: t('noUpdate'),
51+
variant: 'default'
52+
});
53+
setLoading(false);
6454
}
6555
}
6656

57+
useEffect(() => {
58+
checkUpdate();
59+
}, []);
60+
6761
return (
6862
<div className="flex justify-between w-full items-center">
69-
<div className='flex items-center gap-4'>
63+
<div className="flex items-center gap-4">
7064
<div>
71-
<Image src="/app-icon.png" alt="logo" className='size-24' width={0} height={0} />
65+
<Image src="/app-icon.png" alt="logo" className="size-24 dark:invert" width={0} height={0} />
7266
</div>
73-
<div className='h-24 flex flex-col justify-between'>
74-
<span className='text-2xl font-bold flex items-center gap-2'>NoteGen</span>
67+
<div className="h-24 flex flex-col justify-between">
68+
<span className="text-2xl font-bold flex items-center gap-2">NoteGen</span>
7569
<span>
7670
{t('desc')}
7771
</span>
78-
<div>
72+
<div className="flex items-center gap-2">
7973
<Badge variant="outline">v{version}</Badge>
74+
{
75+
update ? (
76+
<>
77+
<ArrowBigRightDash className="size-4" />
78+
<Badge className="bg-green-500 text-white" variant="outline">v{update.version}</Badge>
79+
</>
80+
) : null
81+
}
8082
</div>
8183
</div>
8284
</div>
83-
<Button disabled={loading} onClick={checkVersion}>
85+
<Button disabled={!update || loading || checking} onClick={checkVersion}>
8486
{loading ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : null}
85-
{updateStatus}
87+
{checking ? t('checkUpdate') : update ? t('updateAvailable') : t('noUpdate')}
8688
</Button>
8789
</div>
8890
)

src/app/core/setting/config.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { BotMessageSquare, LayoutTemplate, Command, FileUp, Palette, ScanText, Store, UserRoundCog, Drama, FolderOpen, Package } from "lucide-react"
22

33
const baseConfig = [
4+
{
5+
icon: <Store />,
6+
anchor: 'about',
7+
},
48
{
59
icon: <BotMessageSquare />,
610
anchor: 'ai',
@@ -40,11 +44,7 @@ const baseConfig = [
4044
{
4145
icon: <UserRoundCog />,
4246
anchor: 'dev',
43-
},
44-
{
45-
icon: <Store />,
46-
anchor: 'about',
47-
},
47+
}
4848
]
4949

5050
export default baseConfig

0 commit comments

Comments
 (0)