Skip to content

Commit 2f99954

Browse files
committed
refactor: About page
1 parent 4358d35 commit 2f99954

File tree

3 files changed

+154
-161
lines changed

3 files changed

+154
-161
lines changed

frontend/src/hooks/useCoreBranch.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export const useCoreBranch = (isAlpha = false) => {
6565

6666
const grantable = computed(() => localVersion.value && envStore.env.os !== 'windows')
6767

68+
const CoreFilePath = `${CoreWorkingDirectory}/${getKernelFileName(isAlpha)}`
69+
const CoreBakFilePath = `${CoreFilePath}.bak`
70+
6871
const downloadCore = async () => {
6972
downloading.value = true
7073
try {
@@ -102,24 +105,19 @@ export const useCoreBranch = (isAlpha = false) => {
102105
update(t('common.downloading') + ((progress / total) * 100).toFixed(2) + '%')
103106
},
104107
{ CancelId: downloadCancelId },
105-
).catch((err) => {
106-
destroy()
107-
throw err
108-
})
108+
).finally(destroy)
109109

110110
const stableFileName = getKernelFileName()
111-
const alphaFileName = getKernelFileName(true)
112-
const kernelFilePath = CoreWorkingDirectory + '/' + (isAlpha ? alphaFileName : stableFileName)
113111

114-
await ignoredError(Movefile, kernelFilePath, kernelFilePath + '.bak')
112+
await ignoredError(Movefile, CoreFilePath, CoreBakFilePath)
115113

116114
if (assetName.endsWith('.zip')) {
117115
await UnzipZIPFile(downloadCacheFile, 'data/.cache')
118-
const tmpPath = `data/.cache/` + assetName.replace('.zip', '')
119-
await Movefile(tmpPath + '/' + stableFileName, kernelFilePath)
116+
const tmpPath = `data/.cache/${assetName.replace('.zip', '')}`
117+
await Movefile(`${tmpPath}/${stableFileName}`, CoreFilePath)
120118
await Removefile(tmpPath)
121119
} else {
122-
const extractDir = 'data/.cache/' + (isAlpha ? 'latest' : 'stable')
120+
const extractDir = `data/.cache/${isAlpha ? 'latest' : 'stable'}`
123121
await Makedir(extractDir)
124122
await Exec('tar', [
125123
'xzvf',
@@ -129,17 +127,16 @@ export const useCoreBranch = (isAlpha = false) => {
129127
'--strip-components',
130128
'1',
131129
])
132-
await Movefile(extractDir + '/' + stableFileName, kernelFilePath)
130+
await Movefile(`${extractDir}/${stableFileName}`, CoreFilePath)
133131
await Removefile(extractDir)
134132
}
135133

136134
await Removefile(downloadCacheFile)
137135

138-
if (!kernelFilePath.endsWith('.exe')) {
139-
await ignoredError(Exec, 'chmod', ['+x', await AbsolutePath(kernelFilePath)])
136+
if (!CoreFilePath.endsWith('.exe')) {
137+
await ignoredError(Exec, 'chmod', ['+x', await AbsolutePath(CoreFilePath)])
140138
}
141139

142-
destroy()
143140
refreshLocalVersion()
144141
downloadCompleted.value = true
145142
message.success('common.success')
@@ -154,9 +151,7 @@ export const useCoreBranch = (isAlpha = false) => {
154151
const getLocalVersion = async (showTips = false) => {
155152
localVersionLoading.value = true
156153
try {
157-
const fileName = getKernelFileName(isAlpha)
158-
const kernelFilePath = CoreWorkingDirectory + '/' + fileName
159-
const res = await Exec(kernelFilePath, ['version'])
154+
const res = await Exec(CoreFilePath, ['version'])
160155
versionDetail.value = res.trim()
161156
return res.match(/version (\S+)/)?.[1] || ''
162157
} catch (error: any) {
@@ -206,20 +201,14 @@ export const useCoreBranch = (isAlpha = false) => {
206201
}
207202

208203
const grantCorePermission = async () => {
209-
const fileName = getKernelFileName(isAlpha)
210-
const kernelFilePath = CoreWorkingDirectory + '/' + fileName
211-
await GrantTUNPermission(kernelFilePath)
204+
await GrantTUNPermission(CoreFilePath)
212205
message.success('common.success')
213206
}
214207

215208
const rollbackCore = async () => {
216209
await confirm('common.warning', 'settings.kernel.rollback')
217210

218-
const doRollback = async () => {
219-
const file = getKernelFileName(isAlpha)
220-
await Movefile(`${CoreWorkingDirectory}/${file}.bak`, `${CoreWorkingDirectory}/${file}`)
221-
refreshLocalVersion()
222-
}
211+
const doRollback = () => Movefile(CoreBakFilePath, CoreFilePath)
223212

224213
const { running, branch } = appSettings.app.kernel
225214
const isCurrentRunning = running && (branch === 'alpha') === isAlpha
@@ -228,6 +217,7 @@ export const useCoreBranch = (isAlpha = false) => {
228217
} else {
229218
await doRollback()
230219
}
220+
refreshLocalVersion()
231221
message.success('common.success')
232222
}
233223

@@ -243,8 +233,7 @@ export const useCoreBranch = (isAlpha = false) => {
243233
watch(
244234
[localVersion, downloadCompleted],
245235
debounce(async () => {
246-
const bak = CoreWorkingDirectory + '/' + getKernelFileName(isAlpha) + '.bak'
247-
rollbackable.value = await FileExists(bak)
236+
rollbackable.value = await FileExists(CoreBakFilePath)
248237
}, 500),
249238
)
250239

frontend/src/stores/app.ts

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
import { ref } from 'vue'
1+
import { useI18n } from 'vue-i18n'
2+
import { computed, ref } from 'vue'
23
import { defineStore } from 'pinia'
34

5+
import { useEnvStore } from './env'
6+
import {
7+
APP_TITLE,
8+
APP_VERSION,
9+
APP_VERSION_API,
10+
getGitHubApiAuthorization,
11+
ignoredError,
12+
message,
13+
alert,
14+
} from '@/utils'
15+
import {
16+
Download,
17+
HttpGet,
18+
BrowserOpenURL,
19+
Movefile,
20+
UnzipZIPFile,
21+
Makedir,
22+
Removefile,
23+
AbsolutePath,
24+
HttpCancel,
25+
} from '@/bridge'
26+
427
export const useAppStore = defineStore('app', () => {
528
/* Global Menu */
629
const menuShow = ref(false)
@@ -18,7 +41,95 @@ export const useAppStore = defineStore('app', () => {
1841
y: 0,
1942
})
2043

44+
const { t } = useI18n()
45+
const envStore = useEnvStore()
46+
47+
/* About Page */
2148
const showAbout = ref(false)
49+
const checkForUpdatesLoading = ref(false)
50+
const restartable = ref(false)
51+
const downloading = ref(false)
52+
const downloadUrl = ref('')
53+
const remoteVersion = ref(APP_VERSION)
54+
const updatable = computed(() => downloadUrl.value && APP_VERSION !== remoteVersion.value)
55+
56+
const downloadApp = async () => {
57+
downloading.value = true
58+
try {
59+
const downloadCacheFile = 'data/.cache/gui.zip'
60+
const downloadCancelId = downloadCacheFile
61+
62+
const { update, destroy } = message.info('common.downloading', 10 * 60 * 1_000, () => {
63+
HttpCancel(downloadCancelId)
64+
setTimeout(() => Removefile(downloadCacheFile), 1000)
65+
})
66+
67+
await Makedir('data/.cache')
68+
await Download(
69+
downloadUrl.value,
70+
downloadCacheFile,
71+
undefined,
72+
(progress, total) => {
73+
update(t('common.downloading') + ((progress / total) * 100).toFixed(2) + '%')
74+
},
75+
{
76+
CancelId: downloadCancelId,
77+
},
78+
).finally(destroy)
79+
80+
const { appName, os } = envStore.env
81+
if (os !== 'darwin') {
82+
await Movefile(appName, appName + '.bak')
83+
await UnzipZIPFile(downloadCacheFile, '.')
84+
const suffix = { windows: '.exe', linux: '' }[os]
85+
await Movefile(APP_TITLE + suffix, appName)
86+
message.success('about.updateSuccessfulRestart')
87+
restartable.value = true
88+
} else {
89+
await UnzipZIPFile(downloadCacheFile, 'data')
90+
alert('common.success', 'about.updateSuccessfulReplace')
91+
BrowserOpenURL(await AbsolutePath('data'))
92+
}
93+
94+
await Removefile(downloadCacheFile)
95+
await ignoredError(Removefile, 'data/rolling-release')
96+
} catch (error: any) {
97+
console.log(error)
98+
message.error(error.message || error, 5_000)
99+
}
100+
downloading.value = false
101+
}
102+
103+
const checkForUpdates = async (showTips = false) => {
104+
if (checkForUpdatesLoading.value || downloading.value) return
105+
checkForUpdatesLoading.value = true
106+
remoteVersion.value = APP_VERSION
107+
try {
108+
const { body } = await HttpGet<Record<string, any>>(APP_VERSION_API, {
109+
Authorization: getGitHubApiAuthorization(),
110+
})
111+
if (body.message) throw body.message
112+
113+
const { tag_name, assets } = body
114+
115+
const { os, arch } = envStore.env
116+
const assetName = `${APP_TITLE}-${os}-${arch}.zip`
117+
118+
const asset = assets.find((v: any) => v.name === assetName)
119+
if (!asset) throw 'Asset Not Found:' + assetName
120+
121+
remoteVersion.value = tag_name
122+
downloadUrl.value = asset.browser_download_url
123+
124+
if (showTips) {
125+
message.info(updatable.value ? 'about.newVersion' : 'about.latestVersion')
126+
}
127+
} catch (error: any) {
128+
console.error(error)
129+
message.error(error.message || error)
130+
}
131+
checkForUpdatesLoading.value = false
132+
}
22133

23134
return {
24135
menuShow,
@@ -28,5 +139,12 @@ export const useAppStore = defineStore('app', () => {
28139
tipsMessage,
29140
tipsPosition,
30141
showAbout,
142+
checkForUpdatesLoading,
143+
restartable,
144+
downloading,
145+
remoteVersion,
146+
updatable,
147+
checkForUpdates,
148+
downloadApp,
31149
}
32150
})

0 commit comments

Comments
 (0)