]> git.localhorst.tv Git - alttp.git/blob - resources/js/hooks/tracker.js
249a34bfe283bb8dfb000812c43d8d86cbaacfa8
[alttp.git] / resources / js / hooks / tracker.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3
4 import { CONFIG, DUNGEONS, makeEmptyState, mergeStates } from '../helpers/tracker';
5
6 const context = React.createContext({});
7
8 export const useTracker = () => React.useContext(context);
9
10 export const TrackerProvider = ({ children }) => {
11         const [config, setConfig] = React.useState(CONFIG);
12         const [state, setState] = React.useState(makeEmptyState());
13         const [autoState, setAutoState] = React.useState(makeEmptyState());
14         const [manualState, setManualState] = React.useState(makeEmptyState());
15         const [dungeons, setDungeons] = React.useState(DUNGEONS);
16
17         React.useEffect(() => {
18                 const newDungeons = DUNGEONS.map(dungeon => {
19                         const newDungeon = JSON.parse(JSON.stringify(dungeon));
20                         if (config.wildMap && dungeon.map) {
21                                 ++newDungeon.items;
22                         }
23                         if (config.wildCompass && dungeon.compass) {
24                                 ++newDungeon.items;
25                         }
26                         if (config.wildSmall) {
27                                 newDungeon.items += dungeon.sk;
28                         }
29                         if (config.wildBig && dungeon.bk && !dungeon.dropBk) {
30                                 ++newDungeon.items;
31                         }
32                         if (!config.bossShuffle && dungeon.boss) {
33                                 newDungeon.bosses = [dungeon.boss];
34                         }
35                         return newDungeon;
36                 });
37                 setDungeons(newDungeons);
38         }, [config]);
39
40         React.useEffect(() => {
41                 setState(mergeStates(autoState, manualState));
42         }, [autoState, manualState]);
43
44         const value = React.useMemo(() => {
45                 return { config, setConfig, dungeons, setAutoState, setManualState, state };
46         }, [config, dungeons, state]);
47
48         return <context.Provider value={value}>
49                 {children}
50         </context.Provider>;
51 };
52
53 TrackerProvider.propTypes = {
54         children: PropTypes.node,
55 };