2022-02-23 07:47:59 +01:00
|
|
|
import create from 'zustand';
|
|
|
|
import produce from 'immer';
|
|
|
|
import semver from 'semver';
|
|
|
|
import { VERSION_CHECK } from 'lib/constants';
|
|
|
|
import { getItem } from 'lib/web';
|
|
|
|
|
2022-06-03 16:06:44 +02:00
|
|
|
const REPO_URL = 'https://api.umami.is/v1/updates';
|
2022-02-23 07:47:59 +01:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
current: process.env.VERSION,
|
|
|
|
latest: null,
|
|
|
|
hasUpdate: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = create(() => ({ ...initialState }));
|
|
|
|
|
|
|
|
export async function checkVersion() {
|
|
|
|
const { current } = store.getState();
|
|
|
|
|
|
|
|
const data = await fetch(REPO_URL, {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
2022-06-03 16:06:44 +02:00
|
|
|
Accept: 'application/json',
|
2022-02-23 07:47:59 +01:00
|
|
|
},
|
|
|
|
}).then(res => {
|
|
|
|
if (res.ok) {
|
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
store.setState(
|
|
|
|
produce(state => {
|
2022-06-03 16:06:44 +02:00
|
|
|
const { latest } = data;
|
2022-02-23 07:47:59 +01:00
|
|
|
const lastCheck = getItem(VERSION_CHECK);
|
|
|
|
const hasUpdate = latest && semver.gt(latest, current) && lastCheck?.version !== latest;
|
|
|
|
|
|
|
|
state.current = current;
|
|
|
|
state.latest = latest;
|
|
|
|
state.hasUpdate = hasUpdate;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default store;
|