-
Notifications
You must be signed in to change notification settings - Fork 4
/
appContext.ts
34 lines (29 loc) · 1.15 KB
/
appContext.ts
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
import { createContext, useContext } from "react";
import { DashboardCollectionsBarChart, FeatureFlags } from "../interfaces";
import Admin from "../models/Admin";
export type AppContextType = {
csrfToken: string;
settingUp: boolean;
admin: Admin;
featureFlags: FeatureFlags;
quicksightPagePath: string;
dashboardCollectionsBarChart?: DashboardCollectionsBarChart;
};
// Don't export this, since we always want the error handling behavior of our hook.
const AppContext = createContext<AppContextType | undefined>(undefined);
export const useAppContext = (): AppContextType => {
const context = useContext(AppContext);
if (context === undefined) {
throw new Error(
"useAppContext must be used within an AppContext provider."
);
}
return context;
};
export const useCsrfToken = () => useAppContext().csrfToken;
export const useAppAdmin = () => useAppContext().admin;
export const useAppEmail = () => useAppAdmin().email;
export const useAppFeatureFlags = () => useAppContext().featureFlags;
export const useDashboardCollectionsBarChartSettings = () =>
useAppContext().dashboardCollectionsBarChart;
export default AppContext.Provider;