-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseDeploymentSettings.tsx
103 lines (86 loc) · 3.45 KB
/
useDeploymentSettings.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
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react';
import defaultLogo from 'assets/logo-color.svg';
import defaultOsrdLogo from 'assets/logo-osrd-color-white.svg';
import proudLogo from 'assets/proud-logo-color.svg';
import proudOsrdLogo from 'assets/proud-logo-osrd-color-white.svg';
import xmasLogo from 'assets/xmas-logo-color.svg';
import xmasOsrdLogo from 'assets/xmas-logo-osrd-color-white.svg';
const MONTH_VALUES = {
JUNE: 5,
DECEMBER: 11,
};
const defaultSettings = {
digitalTwinName: 'Osrd',
digitalTwinLogo: defaultLogo,
digitalTwinLogoWithName: defaultOsrdLogo,
stdcmName: 'Stdcm',
stdcmLogo: undefined,
stdcmSimulationSheetLogo: undefined,
isCustomizedDeployment: false,
};
export type DeploymentSettings = {
digitalTwinName: string;
digitalTwinLogo: string;
digitalTwinLogoWithName: string;
stdcmName: string;
stdcmLogo?: string;
stdcmSimulationSheetLogo?: string;
isCustomizedDeployment: boolean;
};
export type DeploymentSettingsContext = DeploymentSettings | undefined;
const deploymentSettingsContext = createContext<DeploymentSettingsContext>(undefined);
type DeploymentContextProviderProps = { children: ReactNode };
export const DeploymentContextProvider = ({ children }: DeploymentContextProviderProps) => {
const [customizedDeploymentSetting, setCustomizedDeploymentSetting] =
useState<DeploymentSettings>();
useEffect(() => {
const fetchInternalProd = async () => {
try {
const response = await fetch('/overrides/overrides.json');
if (!response.ok || response.headers.get('Content-Type') !== 'application/json') {
let digitalTwinLogo = defaultLogo;
let digitalTwinLogoWithName = defaultOsrdLogo;
const currentMonth = new Date().getMonth();
if (currentMonth === MONTH_VALUES.JUNE) {
digitalTwinLogo = proudLogo;
digitalTwinLogoWithName = proudOsrdLogo;
} else if (currentMonth === MONTH_VALUES.DECEMBER) {
digitalTwinLogo = xmasLogo;
digitalTwinLogoWithName = xmasOsrdLogo;
}
setCustomizedDeploymentSetting({
...defaultSettings,
digitalTwinLogo,
digitalTwinLogoWithName,
});
} else {
const overridesData = await response.json();
const { icons, names } = overridesData;
const lmrLogoPath = `/overrides/${icons.stdcm.light}.svg`;
const lmrPngLogoPath = `/overrides/${icons.stdcm.light}@2x.png`;
const horizonLogoWithNamePath = `/overrides/${icons.digital_twin.dark}_Grey10.svg`;
const horizonLogoPath = `/overrides/${icons.digital_twin.dark}_Logo_Grey40.svg`;
setCustomizedDeploymentSetting({
digitalTwinName: names.digital_twin,
digitalTwinLogo: horizonLogoPath,
digitalTwinLogoWithName: horizonLogoWithNamePath,
stdcmName: names.stdcm,
stdcmLogo: lmrLogoPath,
stdcmSimulationSheetLogo: lmrPngLogoPath,
isCustomizedDeployment: true,
});
}
} catch (error) {
console.error('Error fetching overrides.json', error);
}
};
fetchInternalProd();
}, []);
return (
<deploymentSettingsContext.Provider value={customizedDeploymentSetting}>
{children}
</deploymentSettingsContext.Provider>
);
};
const useDeploymentSettings = () => useContext(deploymentSettingsContext);
export default useDeploymentSettings;