2020-09-07 10:22:16 +02:00
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
2020-09-30 01:25:44 +02:00
|
|
|
import { get, getItem } from 'lib/web';
|
2020-09-20 10:33:39 +02:00
|
|
|
import { LOCALE_CONFIG, THEME_CONFIG } from 'lib/constants';
|
2020-09-07 10:22:16 +02:00
|
|
|
|
|
|
|
const app = createSlice({
|
|
|
|
name: 'app',
|
2020-09-20 10:33:39 +02:00
|
|
|
initialState: {
|
|
|
|
locale: getItem(LOCALE_CONFIG) || 'en-US',
|
|
|
|
theme: getItem(THEME_CONFIG) || 'light',
|
2020-09-30 01:25:44 +02:00
|
|
|
versions: {
|
|
|
|
current: process.env.VERSION,
|
|
|
|
latest: null,
|
|
|
|
},
|
2020-09-20 10:33:39 +02:00
|
|
|
},
|
2020-09-07 10:22:16 +02:00
|
|
|
reducers: {
|
2020-09-20 10:33:39 +02:00
|
|
|
setLocale(state, action) {
|
|
|
|
state.locale = action.payload;
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
setTheme(state, action) {
|
|
|
|
state.theme = action.payload;
|
2020-09-07 10:22:16 +02:00
|
|
|
return state;
|
|
|
|
},
|
2020-09-30 01:25:44 +02:00
|
|
|
setVersions(state, action) {
|
|
|
|
state.versions = action.payload;
|
|
|
|
return state;
|
|
|
|
},
|
2020-09-07 10:22:16 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-09-30 01:25:44 +02:00
|
|
|
export const { setLocale, setTheme, setVersions } = app.actions;
|
2020-09-07 10:22:16 +02:00
|
|
|
|
|
|
|
export default app.reducer;
|
2020-09-30 01:25:44 +02:00
|
|
|
|
|
|
|
export function checkVersion() {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const {
|
|
|
|
app: {
|
|
|
|
versions: { current },
|
|
|
|
},
|
|
|
|
} = getState();
|
|
|
|
|
|
|
|
const data = await get('https://api.github.com/repos/mikecao/umami/releases/latest');
|
|
|
|
|
|
|
|
if (!data || !data['tag_name']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const latest = data['tag_name'].startsWith('v') ? data['tag_name'].slice(1) : data['tag_name'];
|
|
|
|
|
|
|
|
if (latest === current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const latestArray = latest.split('.');
|
|
|
|
const currentArray = current.split('.');
|
|
|
|
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
if (Number(latestArray[i]) > Number(currentArray[i])) {
|
|
|
|
return dispatch(
|
|
|
|
setVersions({
|
|
|
|
current,
|
|
|
|
latest: latest,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|