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 = DUNGEONS.map(dungeon => {
42 const newDungeon = JSON.parse(JSON.stringify(dungeon));
43 if (config.wildMap && dungeon.map) {
46 if (config.wildCompass && dungeon.compass) {
49 if (config.wildSmall) {
50 newDungeon.items += dungeon.sk;
52 if (config.wildBig && dungeon.bk && !dungeon.dropBk) {
56 newDungeon.bosses = config.bossShuffle ? BOSSES : [dungeon.boss];
60 setDungeons(newDungeons);
63 React.useEffect(() => {
64 setState(mergeStates(autoState, manualState));
65 }, [autoState, manualState]);
67 React.useEffect(() => {
68 setLogic(applyLogic(config, dungeons, state));
69 }, [config, dungeons, state]);
71 const value = React.useMemo(() => {
72 return { config, dungeons, logic, saveConfig, setAutoState, setManualState, state };
73 }, [config, dungeons, logic, state]);
75 return <context.Provider value={value}>
80 TrackerProvider.propTypes = {
81 children: PropTypes.node,