forked from idiomaspuentes/obs-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalState.js
144 lines (125 loc) · 3.63 KB
/
GlobalState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { createContext, useReducer, useContext } from "react";
import { pad } from "./src/core/utils";
import { getStories } from "./src/core/getStories";
export const OBSContext = createContext();
const initialState = {
reference: {
story: 1,
frame: 1,
},
OBS: null,
};
function doesNextFrameExist(obs, reference) {
return !!obs?.stories?.[pad(reference.story)]?.frames[reference.frame + 1];
}
function doesNextStoryExist(obs, reference) {
return !!obs?.stories?.[pad(reference.story + 1)];
}
function doesPrevFrameExist(obs, reference) {
return !!obs?.stories?.[pad(reference.story)]?.frames[reference.frame - 1];
}
function doesPrevStoryExist(obs, reference) {
return !!obs?.stories?.[pad(reference.story - 1)];
}
function obtainLastFrame(obs, reference) {
return (
Object.keys(obs?.stories?.[pad(reference.story - 1)]?.frames).length - 1
);
}
const OBSReducer = (state, action) => {
switch (action.type) {
case "GO_NEXT":
if (doesNextFrameExist(state.OBS, state.reference)) {
return {
...state,
reference: {
story: state.reference.story,
frame: state.reference.frame + 1,
},
};
} else if (doesNextStoryExist(state.OBS, state.reference)) {
return {
...state,
reference: { story: state.reference.story + 1, frame: 1 },
};
} else {
return state;
}
case "NAV_TO":
const {story, frame} = action.payload;
if (!story) {console.error('No se envió un story'); return state;}
if (!state.OBS?.stories?.[pad(story)]) {console.error('No se encontró el story especificado'); return state;}
if (frame && (!state.OBS?.stories?.[pad(story)]?.frames[frame])){console.error('No se encontró el frame especificado'); return state;}
if (!frame){
return {
...state,
reference: { story, frame: 1 },
}} else
return {
...state,
reference: { story, frame },
};
case "GO_PREV":
if (
doesPrevFrameExist(state.OBS, state.reference) &&
state.reference.frame !== 1
) {
return {
...state,
reference: {
story: state.reference.story,
frame: state.reference.frame - 1,
},
};
} else if (doesPrevStoryExist(state.OBS, state.reference)) {
return {
...state,
reference: {
story: state.reference.story - 1,
frame: obtainLastFrame(state.OBS, state.reference),
},
};
} else {
return state;
}
case "SET_OBS":
return {
...state,
OBS: action.payload,
};
default:
return state;
}
};
export const OBSContextProvider = (props) => {
const [OBSState, setOBState] = useReducer(OBSReducer, initialState);
return (
<OBSContext.Provider value={{ OBSState, setOBState }}>
{props.children}
</OBSContext.Provider>
);
};
export function useObsNav() {
const { OBSState, setOBState } = useContext(OBSContext);
const { reference } = OBSState;
const goTo = (story, frame) => {
setOBState({ type: "NAV_TO", payload: {story, frame} });
};
const goNext = () => {
setOBState({ type: "GO_NEXT" });
};
const goPrev = () => {
setOBState({ type: "GO_PREV" });
};
return { reference, goTo, goNext, goPrev };
}
export function useObs() {
const { OBSState, setOBState } = useContext(OBSContext);
const { OBS: source } = OBSState;
const setSrc = () => {
getStories("es-419_gl", "xsu").then((obs) => {
setOBState({ type: "SET_OBS", payload: obs });
});
};
return { source, setSrc };
}