This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ActiveMainProvider.jsx
99 lines (82 loc) · 2.86 KB
/
ActiveMainProvider.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import uuidv4 from 'uuid/v4';
import NavigationItemContext from '../../navigation-item/NavigationItemContext';
import ActiveMainContext from '../ActiveMainContext';
import ActiveMainRegistrationContext from './ActiveMainRegistrationContext';
const propTypes = {
children: PropTypes.node,
};
const ActiveMainProvider = ({ children }) => {
const navigationItem = React.useContext(NavigationItemContext);
const activeMainRegistration = React.useContext(ActiveMainRegistrationContext);
const unregisterActiveMainRef = React.useRef();
const [state, dispatch] = React.useReducer((currentState, action) => {
if (action.type === 'register') {
return {
registrationId: action.registrationId,
activeMain: {
label: action.label,
metaData: action.metaData,
},
};
}
if (action.type === 'unregister') {
if (currentState.registrationId === action.registrationId) {
return {
registrationId: undefined,
activeMainPage: undefined,
};
}
}
return currentState;
}, { registrationId: undefined, activeMain: undefined });
React.useEffect(() => {
if (!activeMainRegistration) {
return;
}
// If an ancestor provider exists, we need to forward the active main info
// if the provider exists in the active navigation branch. It is
// otherwise unregistered, if necessary, as we do not want potentially stale
// information living above this provider level.
if (navigationItem.isActive) {
unregisterActiveMainRef.current = activeMainRegistration.register(state.activeMain);
} else if (unregisterActiveMainRef.current) {
unregisterActiveMainRef.current();
unregisterActiveMainRef.current = undefined;
}
}, [state.activeMain, navigationItem.isActive, activeMainRegistration]);
React.useEffect(() => () => {
if (unregisterActiveMainRef.current) {
unregisterActiveMainRef.current();
unregisterActiveMainRef.current = undefined;
}
}, []);
const activeMainRegistrationContextValue = React.useMemo(() => ({
register: (registrationData) => {
if (!registrationData) {
return undefined;
}
const { label, metaData } = registrationData;
const registrationId = uuidv4();
dispatch({
type: 'register',
registrationId,
label,
metaData,
});
return () => {
dispatch({ type: 'unregister', registrationId });
};
},
}), []);
return (
<ActiveMainRegistrationContext.Provider value={activeMainRegistrationContextValue}>
<ActiveMainContext.Provider value={state.activeMain}>
{children}
</ActiveMainContext.Provider>
</ActiveMainRegistrationContext.Provider>
);
};
ActiveMainProvider.propTypes = propTypes;
export default ActiveMainProvider;