mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Replaced redux with zustand. Fixed login issue, closes #980.
This commit is contained in:
parent
7071f5fba5
commit
9937caa569
33 changed files with 234 additions and 286 deletions
30
store/app.js
Normal file
30
store/app.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import create from 'zustand';
|
||||
import { DEFAULT_LOCALE, DEFAULT_THEME, LOCALE_CONFIG, THEME_CONFIG } from 'lib/constants';
|
||||
import { getItem } from 'lib/web';
|
||||
|
||||
const initialState = {
|
||||
locale: getItem(LOCALE_CONFIG) || DEFAULT_LOCALE,
|
||||
theme: getItem(THEME_CONFIG) || DEFAULT_THEME,
|
||||
shareToken: null,
|
||||
user: null,
|
||||
};
|
||||
|
||||
const store = create(() => ({ ...initialState }));
|
||||
|
||||
export function setTheme(theme) {
|
||||
store.setState({ theme });
|
||||
}
|
||||
|
||||
export function setLocale(locale) {
|
||||
store.setState({ locale });
|
||||
}
|
||||
|
||||
export function setShareToken(shareToken) {
|
||||
store.setState({ shareToken });
|
||||
}
|
||||
|
||||
export function setUser(user) {
|
||||
store.setState({ user });
|
||||
}
|
||||
|
||||
export default store;
|
||||
9
store/queries.js
Normal file
9
store/queries.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import create from 'zustand';
|
||||
|
||||
const store = create(() => ({}));
|
||||
|
||||
export function saveQuery(url, data) {
|
||||
store.setState({ [url]: data });
|
||||
}
|
||||
|
||||
export default store;
|
||||
54
store/version.js
Normal file
54
store/version.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import create from 'zustand';
|
||||
import produce from 'immer';
|
||||
import semver from 'semver';
|
||||
import { VERSION_CHECK } from 'lib/constants';
|
||||
import { getItem } from 'lib/web';
|
||||
|
||||
const REPO_URL = 'https://api.github.com/repos/mikecao/umami/releases/latest';
|
||||
|
||||
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: {
|
||||
Accept: 'application/vnd.github.v3+json',
|
||||
},
|
||||
}).then(res => {
|
||||
if (res.ok) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
store.setState(
|
||||
produce(state => {
|
||||
const { tag_name } = data;
|
||||
|
||||
const latest = tag_name.startsWith('v') ? tag_name.slice(1) : tag_name;
|
||||
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;
|
||||
20
store/websites.js
Normal file
20
store/websites.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import create from 'zustand';
|
||||
import produce from 'immer';
|
||||
|
||||
const store = create(() => ({}));
|
||||
|
||||
export function setDateRange(websiteId, dateRange) {
|
||||
store.setState(
|
||||
produce(state => {
|
||||
if (!state[websiteId]) {
|
||||
state[websiteId] = {};
|
||||
}
|
||||
|
||||
state[websiteId].dateRange = { ...dateRange, modified: Date.now() };
|
||||
|
||||
return state;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export default store;
|
||||
Loading…
Add table
Add a link
Reference in a new issue