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({});
24 const [pins, setPins] = React.useState([]);
26 const saveConfig = React.useCallback((values) => {
28 const newConfig = { ...s, ...values };
29 localStorage.setItem('tracker.config', JSON.stringify(newConfig));
34 const addPin = React.useCallback((pin) => {
36 const id = ps.length ? ps[ps.length - 1].id + 1 : 1;
37 return [...ps, { ...pin, id }];
41 const removePin = React.useCallback((pin) => {
42 setPins(ps => ps.filter(p => p.id !== pin.id));
45 React.useEffect(() => {
46 const savedConfig = localStorage.getItem('tracker.config');
48 setConfig(c => ({ ...c, ...JSON.parse(savedConfig) }));
52 React.useEffect(() => {
53 const newDungeons = configureDungeons(config);
54 setDungeons(newDungeons);
57 React.useEffect(() => {
58 setState(mergeStates(autoState, manualState));
59 }, [autoState, manualState]);
61 React.useEffect(() => {
62 setLogic(applyLogic(config, dungeons, state));
63 }, [config, dungeons, state]);
65 const value = React.useMemo(() => {
78 }, [config, dungeons, logic, pins, state]);
80 return <context.Provider value={value}>
85 TrackerProvider.propTypes = {
86 children: PropTypes.node,