forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.js
55 lines (44 loc) · 1.13 KB
/
version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import create from 'zustand';
import produce from 'immer';
import semver from 'semver';
import { VERSION_CHECK, UPDATES_URL } from 'lib/constants';
import { getItem } from 'lib/web';
const initialState = {
current: process.env.currentVersion,
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 default store;