diff options
| author | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
| commit | 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch) | |
| tree | b9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/store/version.ts | |
| download | umami-396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b.tar.xz umami-396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b.zip | |
Created from https://vercel.com/new
Diffstat (limited to 'src/store/version.ts')
| -rw-r--r-- | src/store/version.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/store/version.ts b/src/store/version.ts new file mode 100644 index 0000000..95367af --- /dev/null +++ b/src/store/version.ts @@ -0,0 +1,55 @@ +import { produce } from 'immer'; +import semver from 'semver'; +import { create } from 'zustand'; +import { CURRENT_VERSION, UPDATES_URL, VERSION_CHECK } from '@/lib/constants'; +import { getItem } from '@/lib/storage'; + +const initialState = { + current: CURRENT_VERSION, + latest: null, + hasUpdate: false, + checked: false, + releaseUrl: null, +}; + +const store = create(() => ({ ...initialState })); + +export async function checkVersion() { + const { current } = store.getState(); + + const data = await fetch(`${UPDATES_URL}?v=${current}`, { + method: 'GET', + headers: { + Accept: 'application/json', + }, + }).then(res => { + if (res.ok) { + return res.json(); + } + + return null; + }); + + if (!data) { + return; + } + + store.setState( + produce(state => { + const { latest, url } = data; + const lastCheck = getItem(VERSION_CHECK); + + const hasUpdate = !!(latest && lastCheck?.version !== latest && semver.gt(latest, current)); + + state.current = current; + state.latest = latest; + state.hasUpdate = hasUpdate; + state.checked = true; + state.releaseUrl = url; + + return state; + }), + ); +} + +export const useVersion = store; |