2022-02-23 07:47:59 +01:00
|
|
|
import create from 'zustand';
|
|
|
|
import produce from 'immer';
|
|
|
|
import semver from 'semver';
|
2022-08-08 20:31:36 +02:00
|
|
|
import { CURRENT_VERSION, VERSION_CHECK, UPDATES_URL } from 'lib/constants';
|
2022-02-23 07:47:59 +01:00
|
|
|
import { getItem } from 'lib/web';
|
|
|
|
|
|
|
|
const initialState = {
|
2022-08-08 20:31:36 +02:00
|
|
|
current: CURRENT_VERSION,
|
2022-02-23 07:47:59 +01:00
|
|
|
latest: null,
|
|
|
|
hasUpdate: false,
|
2022-06-24 10:54:55 +02:00
|
|
|
checked: false,
|
2022-07-16 08:53:31 +02:00
|
|
|
releaseUrl: null,
|
2022-02-23 07:47:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const store = create(() => ({ ...initialState }));
|
|
|
|
|
|
|
|
export async function checkVersion() {
|
|
|
|
const { current } = store.getState();
|
|
|
|
|
2022-06-24 10:54:55 +02:00
|
|
|
const data = await fetch(`${UPDATES_URL}?v=${current}`, {
|
|
|
|
method: 'GET',
|
2022-02-23 07:47:59 +01:00
|
|
|
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-07-16 08:53:31 +02:00
|
|
|
const { latest, url } = data;
|
2022-02-23 07:47:59 +01:00
|
|
|
const lastCheck = getItem(VERSION_CHECK);
|
2022-06-24 10:54:55 +02:00
|
|
|
|
|
|
|
const hasUpdate = !!(latest && lastCheck?.version !== latest && semver.gt(latest, current));
|
2022-02-23 07:47:59 +01:00
|
|
|
|
|
|
|
state.current = current;
|
|
|
|
state.latest = latest;
|
|
|
|
state.hasUpdate = hasUpdate;
|
2022-06-24 10:54:55 +02:00
|
|
|
state.checked = true;
|
2022-07-16 08:53:31 +02:00
|
|
|
state.releaseUrl = url;
|
2022-02-23 07:47:59 +01:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default store;
|