@state-less/leap-frontend
Version:
A collection of open source fullstack services powered by React Server
121 lines (120 loc) • 4.07 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useReducer } from 'react';
const _localStorage = typeof localStorage === 'undefined'
? {
getItem: (name) => null,
}
: localStorage;
const params = new URLSearchParams(typeof window === 'undefined' ? '' : window?.location?.search);
const bg = Number(params.get('bg')) % 4;
const menuOpen = Boolean(params.get('menuOpen'));
const initialState = {
menuOpen,
animatedBackground: _localStorage?.getItem('animatedBackgroundUser')
? Number(_localStorage?.getItem('animatedBackgroundUser'))
: Number(_localStorage?.getItem('animatedBackground')) || bg,
messages: [],
alerts: {
info: [],
warning: [],
},
history: [],
fullscreen: _localStorage?.getItem('fullscreen') === 'true',
search: '',
searchDistance: Number(_localStorage?.getItem('searchDistance') || 2),
showBC: false,
};
export var Actions;
(function (Actions) {
Actions[Actions["TOGGLE_MENU"] = 0] = "TOGGLE_MENU";
Actions[Actions["SET_BG"] = 1] = "SET_BG";
Actions[Actions["CHOOSE_BG"] = 2] = "CHOOSE_BG";
Actions[Actions["TOGGLE_ANIMATED_BACKGROUND"] = 3] = "TOGGLE_ANIMATED_BACKGROUND";
Actions[Actions["SHOW_MESSAGE"] = 4] = "SHOW_MESSAGE";
Actions[Actions["HIDE_MESSAGE"] = 5] = "HIDE_MESSAGE";
Actions[Actions["RECORD_CHANGE"] = 6] = "RECORD_CHANGE";
Actions[Actions["REVERT_CHANGE"] = 7] = "REVERT_CHANGE";
Actions[Actions["TOGGLE_FULLSCREEN"] = 8] = "TOGGLE_FULLSCREEN";
Actions[Actions["SEARCH"] = 9] = "SEARCH";
Actions[Actions["SET_SEARCH_DISTANCE"] = 10] = "SET_SEARCH_DISTANCE";
Actions[Actions["SET_LAST_BC"] = 11] = "SET_LAST_BC";
})(Actions || (Actions = {}));
export const stateContext = createContext({
state: initialState,
dispatch: (() => { }),
});
const reducer = (state, action) => {
switch (action.type) {
case Actions.TOGGLE_MENU:
return {
...state,
menuOpen: !state.menuOpen,
};
case Actions.SET_BG:
return {
...state,
animatedBackground: 1,
};
case Actions.TOGGLE_ANIMATED_BACKGROUND:
return {
...state,
animatedBackground: (~~state.animatedBackground + 1) % 4,
};
case Actions.CHOOSE_BG:
return {
...state,
animatedBackground: action.value,
};
case Actions.SHOW_MESSAGE:
return {
...state,
messages: [
...state.messages,
{ message: action.value, action: action.action },
],
};
case Actions.HIDE_MESSAGE:
return {
...state,
messages: state.messages.slice(1),
};
case Actions.RECORD_CHANGE:
return {
...state,
history: [...state.history, action.value],
};
case Actions.REVERT_CHANGE: {
return {
...state,
history: state.history.slice(0, -1),
};
}
case Actions.TOGGLE_FULLSCREEN:
return {
...state,
fullscreen: !state.fullscreen,
};
case Actions.SEARCH:
return {
...state,
search: action.value,
};
case Actions.SET_SEARCH_DISTANCE:
return {
...state,
searchDistance: action.value,
};
case Actions.SET_LAST_BC:
return {
...state,
showBC: action.value,
};
default:
return state;
}
return state;
};
export const StateProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
return (_jsx(stateContext.Provider, { value: { state, dispatch }, children: children }));
};