-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppEnvironment.tsx
More file actions
46 lines (39 loc) · 1.21 KB
/
AppEnvironment.tsx
File metadata and controls
46 lines (39 loc) · 1.21 KB
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
import React, { createContext, ReactNode, useContext, useState } from 'react';
export type AppEnvironment = {
name: string;
production: boolean;
baseUrl: string;
};
type AppEnvironmentList = {
[label: string]: AppEnvironment;
};
export const APP_ENVIRONMENTS: AppEnvironmentList = {
production: {
name: 'Production',
production: true,
baseUrl: 'https://my.robojackets.org',
},
test: {
name: 'Test',
production: false,
baseUrl: 'https://apiary-test.robojackets.org',
},
};
type EnvironmentContextType = {
environment: AppEnvironment;
setEnvironment: (env: AppEnvironment) => void;
};
const AppEnvironmentContext = createContext<EnvironmentContextType | undefined>(undefined);
export function AppEnvironmentProvider({ children }: { children: ReactNode }) {
const [environment, setEnvironment] = useState(APP_ENVIRONMENTS.production);
return (
<AppEnvironmentContext.Provider value={{ environment, setEnvironment }}>
{children}
</AppEnvironmentContext.Provider>
);
}
export function useAppEnvironment() {
const context = useContext(AppEnvironmentContext);
if (!context) throw new Error('useEnvironment must be used inside EnvironmentProvider');
return context;
}