From 657c180e056e345776ac1a21966c4c3eafdb1d09 Mon Sep 17 00:00:00 2001 From: kunish Date: Thu, 10 Oct 2024 23:42:40 +0800 Subject: [PATCH] feat(config): check for frontend version update --- src/apis/index.ts | 45 +++++++++++++++++-------------- src/pages/Config.tsx | 64 +++++++++++++++++++++++++++----------------- vite.config.ts | 5 ++-- 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/src/apis/index.ts b/src/apis/index.ts index 41ed000..4c5cfce 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -248,37 +248,42 @@ export const updateRuleProviderAPI = (providerName: string) => { } type ReleaseAPIResponse = { + tag_name: string assets: { name: string }[] } -export const isUpdateAvailableAPI = async (versionResponse: string) => { +export const isFrontendUpdateAvailableAPI = async (currentVersion: string) => { + const repositoryURL = 'https://api.github.com/repos/MetaCubeX/metacubexd' + + const { tag_name } = await ky + .get(`${repositoryURL}/releases/latest`) + .json() + + return tag_name !== currentVersion +} + +export const isBackendUpdateAvailableAPI = async (currentVersion: string) => { const repositoryURL = 'https://api.github.com/repos/MetaCubeX/mihomo' - const match = /(alpha|beta|meta)-?(\w+)/.exec(versionResponse) + const match = /(alpha|beta|meta)-?(\w+)/.exec(currentVersion) if (!match) return false + const check = async (url: string) => { + const { assets } = await ky + .get(`${repositoryURL}${url}`) + .json() + + const alreadyLatest = assets.some(({ name }) => name.includes(version)) + + return !alreadyLatest + } + const channel = match[1], version = match[2] - if (channel === 'meta') { - const { assets } = await ky - .get(`${repositoryURL}/releases/latest`) - .json() + if (channel === 'meta') return await check('/releases/latest') - const alreadyLatest = assets.some(({ name }) => name.includes(version)) - - return !alreadyLatest - } - - if (channel === 'alpha') { - const { assets } = await ky - .get(`${repositoryURL}/releases/tags/Prerelease-Alpha`) - .json() - - const alreadyLatest = assets.some(({ name }) => name.includes(version)) - - return !alreadyLatest - } + if (channel === 'alpha') return await check('/releases/tags/Prerelease-Alpha') return false } diff --git a/src/pages/Config.tsx b/src/pages/Config.tsx index 3010a92..caacc69 100644 --- a/src/pages/Config.tsx +++ b/src/pages/Config.tsx @@ -8,7 +8,8 @@ import { fetchBackendVersionAPI, flushFakeIPDataAPI, flushingFakeIPData, - isUpdateAvailableAPI, + isBackendUpdateAvailableAPI, + isFrontendUpdateAvailableAPI, reloadConfigFileAPI, reloadingConfigFile, restartBackendAPI, @@ -494,29 +495,37 @@ const ConfigForXd = () => { ) } -const Versions: Component<{ backendVersion: Accessor }> = ({ - backendVersion, -}) => { - const [isUpdateAvailable, setIsUpdateAvailable] = createSignal(false) +const Versions: Component<{ + frontendVersion: string + backendVersion: Accessor +}> = ({ frontendVersion, backendVersion }) => { + const [isFrontendUpdateAvailable] = createResource(() => + isFrontendUpdateAvailableAPI(frontendVersion), + ) + const [isBackendUpdateAvailable] = createResource(() => + isBackendUpdateAvailableAPI(backendVersion()), + ) - createEffect(async () => { - const version = backendVersion() - - if (!version) return - - setIsUpdateAvailable(await isUpdateAvailableAPI(version)) - }) + const UpdateAvailableIndicator = () => ( + + + + + ) return (
- {import.meta.env.APP_VERSION} +
+ + + + + {import.meta.env.APP_VERSION} +
- - - - - + + {backendVersion()} @@ -536,13 +545,15 @@ export default () => { const [t] = useI18n() - const [backendVersion, setBackendVersion] = createSignal('') - const isSingBox = createMemo(() => backendVersion().includes('sing-box')) - - onMount(() => { - fetchBackendVersionAPI().then(setBackendVersion) + const frontendVersion = `v${import.meta.env.APP_VERSION}` + const [backendVersion] = createResource(fetchBackendVersionAPI, { + initialValue: '', }) + const isSingBox = createMemo( + () => backendVersion()?.includes('sing-box') || false, + ) + return ( <> {t('config')} @@ -564,7 +575,12 @@ export default () => { {t('version')} - + + +
) diff --git a/vite.config.ts b/vite.config.ts index 0f96548..f6321b4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,6 +2,7 @@ import AutoImport from 'unplugin-auto-import/vite' import { defineConfig } from 'vite' import { VitePWA } from 'vite-plugin-pwa' import solidPlugin from 'vite-plugin-solid' +import { version } from './package.json' export default defineConfig({ base: './', @@ -9,9 +10,7 @@ export default defineConfig({ resolve: { alias: { '~': '/src' } }, define: { - 'import.meta.env.APP_VERSION': JSON.stringify( - process.env.npm_package_version, - ), + 'import.meta.env.APP_VERSION': JSON.stringify(version), }, plugins: [