import { useReducer, useRef } from 'react'; const initialState = () => ({ appSettings: { apiURL: 'https://pool.wowne.ro/api', appLoaded: false, coinDecimals: 11, }, pool: { config: null, }, user: { loggedIn: false, }, messages: {}, }); let updatedState; const reducer = (state, action) => { let result = {}; const { appLoaded, config, loggedIn, } = action; switch (action.type) { case 'APP_LOADED': result = { ...state, appSettings: { ...state.appSettings, appLoaded, }, }; break; case 'CLEAR_MESSAGES': result = { ...state, messages: {} }; break; case 'USER_LOGGED_IN': result = { ...state, user: { loggedIn } }; break; case 'UPDATE_POOL_CONFIG': result = { ...state, pool: { ...state.pool, config, } }; 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;