1 import PropTypes from 'prop-types';
2 import React from 'react';
11 } from '../helpers/tracker';
13 const context = React.createContext({});
15 export const useTracker = () => React.useContext(context);
17 export const TrackerProvider = ({ children }) => {
18 const [config, setConfig] = React.useState(CONFIG);
19 const [state, setState] = React.useState(makeEmptyState());
20 const [autoState, setAutoState] = React.useState(makeEmptyState());
21 const [manualState, setManualState] = React.useState(makeEmptyState());
22 const [dungeons, setDungeons] = React.useState(DUNGEONS);
23 const [logic, setLogic] = React.useState({});
25 const saveConfig = React.useCallback((values) => {
27 const newConfig = { ...s, ...values };
28 localStorage.setItem('tracker.config', JSON.stringify(newConfig));
33 React.useEffect(() => {
34 const savedConfig = localStorage.getItem('tracker.config');
36 setConfig(c => ({ ...c, ...JSON.parse(savedConfig) }));
40 React.useEffect(() => {
41 const newDungeons = configureDungeons(config);
42 setDungeons(newDungeons);
45 React.useEffect(() => {
46 setState(mergeStates(autoState, manualState));
47 }, [autoState, manualState]);
49 React.useEffect(() => {
50 setLogic(applyLogic(config, dungeons, state));
51 }, [config, dungeons, state]);
53 const value = React.useMemo(() => {
54 return { config, dungeons, logic, saveConfig, setAutoState, setManualState, state };
55 }, [config, dungeons, logic, state]);
57 return <context.Provider value={value}>
62 TrackerProvider.propTypes = {
63 children: PropTypes.node,