-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathosrdContext.tsx
113 lines (91 loc) · 2.83 KB
/
osrdContext.tsx
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
import { createContext, useContext, useMemo } from 'react';
import { useSelector } from 'react-redux';
import { ModalProvider } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider';
import { type MODES } from 'main/consts';
import type { OsrdSlice } from 'reducers';
import type { EditorSelectors } from 'reducers/editor/selectors';
import type { MapViewerSelectors } from 'reducers/mapViewer/selectors';
import type { ConfSelectors, ConfSliceActions } from 'reducers/osrdconf/osrdConfCommon';
import type { ValueOf } from 'utils/types';
export type OsrdSelectors = ConfSelectors | MapViewerSelectors | EditorSelectors;
export type OsrdContext = {
slice: OsrdSlice;
selectors: OsrdSelectors;
mode: ValueOf<typeof MODES>;
} | null;
const osrdContext = createContext<OsrdContext>(null);
export const useOsrdContext = () => {
const context = useContext(osrdContext);
if (!context) {
throw new Error('useOsrdContext must be used within a OsrdContext.Provider');
}
return context;
};
export const useOsrdActions = () => {
const { slice } = useOsrdContext();
if (!slice) {
throw new Error('OsrdContext slice is not available');
}
if (!slice.actions) {
throw new Error('OsrdContext slice does not have any actions');
}
return slice.actions;
};
export const useOsrdConfActions = () => {
const { slice } = useOsrdContext();
if (!slice) {
throw new Error('OsrdContext slice is not available');
}
if (!slice.actions) {
throw new Error('OsrdContext slice does not have any actions');
}
return slice.actions as ConfSliceActions;
};
export const useOsrdConfSelectors = () => {
const { selectors } = useOsrdContext();
if (!selectors) {
throw new Error('OsrdContext selectors are not available');
}
return selectors as ConfSelectors;
};
export const useInfraID = () => {
const { selectors } = useOsrdContext();
if (!selectors) {
throw new Error('OsrdContext selectors are not available');
}
const infraId = useSelector(selectors.getInfraID);
return infraId;
};
export const useInfraActions = () => {
const { slice } = useOsrdContext();
if (!slice) {
throw new Error('OsrdContext slice is not available');
}
if (!slice.actions) {
throw new Error('OsrdContext slice does not have any actions');
}
return {
updateInfraID: slice.actions.updateInfraID,
updateInfra: slice.actions.updateInfra,
};
};
type OsrdContextLayoutProps = {
slice: NonNullable<OsrdContext>['slice'];
selectors: NonNullable<OsrdContext>['selectors'];
mode: ValueOf<typeof MODES>;
};
export const OsrdContextLayout = ({ slice, selectors, mode }: OsrdContextLayoutProps) => {
const value = useMemo(
() => ({
slice,
selectors,
mode,
}),
[slice, selectors]
);
return (
<osrdContext.Provider value={value}>
<ModalProvider />
</osrdContext.Provider>
);
};