You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.5 KiB

import { useReducer, useRef } from 'react';
const initialState = () => ({
appSettings: {
apiURL: 'https://pool.wowne.ro/api',
appLoaded: false,
coinDecimals: 11,
coinSymbol: 9077, // hex 2375 (⍵) https://git.wownero.com/wownero/meta/issues/2
minerStatsUpdateInterval: 15,
networkStatsUpdateInterval: 15,
poolStatsUpdateInterval: 30,
},
intervals: [],
messages: {},
pool: {
config: null,
hasAids: false,
networkStats: null,
stats: null,
},
user: {
loggedIn: false,
wallets: null,
},
});
let updatedState;
const reducer = (state, action) => {
let result = {};
const {
appLoaded,
config,
hasAids,
networkStats,
stats,
loggedIn,
} = action;
switch (action.type) {
case 'APP_LOADED':
result = {
...state,
appSettings: {
...state.appSettings,
appLoaded,
},
};
break;
case 'UPDATE_WALLETS':
result = {
...state,
user: {
...state.user,
wallets: JSON.parse(localStorage.getItem('pool.wowne.ro')),
},
};
break;
case 'SET_INTERVALS':
const intervals = action.intervals.map(i => setInterval(i.fn, i.time * 1000, i.params));
result = {
...state,
intervals,
};
break;
case 'USER_LOGGED_IN':
result = { ...state, user: { loggedIn } };
break;
case 'UPDATE_POOL_CONFIG':
result = {
...state,
pool: {
...state.pool,
config,
}
};
break;
case 'UPDATE_POOL_STATS':
result = {
...state,
pool: {
...state.pool,
stats,
}
};
break;
case 'UPDATE_NETWORK_STATS':
result = {
...state,
pool: {
...state.pool,
networkStats,
}
};
break;
case 'CLEAR_APP':
state.intervals.forEach(interval => clearInterval(interval));
result = {
...state,
intervals: [],
};
break;
case 'CLEAR_MESSAGES':
result = { ...state, messages: {} };
break;
case 'HAS_AIDS':
result = {
...state,
pool: {
...state.pool,
hasAids,
}
}
break;
default:
throw new Error();
}
updatedState.current = result;
return result;
};
const useAppState = () => {
const state = initialState();
updatedState = useRef(state);
return [...useReducer(reducer, state), updatedState];
};
export default useAppState;