1- import { FC , ReactNode , createContext , useContext } from 'react' ;
1+ import { ReactNode , createContext , use } from 'react' ;
22import { DocLinkCreator } from '../lib/shared/links' ;
3- import { useTranslation } from 'react-i18next' ;
43
54export enum Landscape {
65 Live = 'LIVE' ,
@@ -9,49 +8,47 @@ export enum Landscape {
98 Development = 'DEV' ,
109}
1110
12- export interface FrontendConfig {
11+ interface FrontendConfigContextProps {
1312 backendUrl : string ;
1413 landscape ?: Landscape ;
1514 documentationBaseUrl : string ;
16- }
17-
18- export interface FrontendConfigProviderProps extends FrontendConfig {
1915 links : DocLinkCreator ;
2016}
2117
22- const FrontendConfigContext = createContext < FrontendConfigProviderProps | null > (
18+ const FrontendConfigContext = createContext < FrontendConfigContextProps | null > (
2319 null ,
2420) ;
2521
26- export const useFrontendConfig = ( ) => {
27- const c = useContext ( FrontendConfigContext ) ;
28- const { t } = useTranslation ( ) ;
29-
30- if ( ! c ) {
31- throw new Error ( t ( 'FrontendConfigContext.errorMessage' ) ) ;
32- }
33- return c ;
34- } ;
22+ const fetchPromise = fetch ( '/frontend-config.json' ) . then ( ( res ) => res . json ( ) ) ;
3523
36- export const FrontendConfigProvider : FC < {
24+ interface FrontendConfigProviderProps {
3725 children : ReactNode ;
38- config : FrontendConfig ;
39- } > = ( { children, config } ) => {
26+ }
27+
28+ export function FrontendConfigProvider ( {
29+ children,
30+ } : FrontendConfigProviderProps ) {
31+ const config = use ( fetchPromise ) ;
4032 const docLinks = new DocLinkCreator ( config . documentationBaseUrl ) ;
33+ const value : FrontendConfigContextProps = {
34+ links : docLinks ,
35+ backendUrl : config . backendUrl ,
36+ landscape : config . landscape ,
37+ documentationBaseUrl : config . documentationBaseUrl ,
38+ } ;
39+
4140 return (
42- < FrontendConfigContext . Provider
43- value = { {
44- links : docLinks ,
45- backendUrl : config . backendUrl ,
46- landscape : config . landscape ,
47- documentationBaseUrl : config . documentationBaseUrl ,
48- } }
49- >
50- { children }
51- </ FrontendConfigContext . Provider >
41+ < FrontendConfigContext value = { value } > { children } </ FrontendConfigContext >
5242 ) ;
53- } ;
54-
55- export async function LoadFrontendConfig ( ) : Promise < FrontendConfig > {
56- return fetch ( '/frontend-config.json' ) . then ( ( res ) => res . json ( ) ) ;
5743}
44+
45+ export const useFrontendConfig = ( ) => {
46+ const context = use ( FrontendConfigContext ) ;
47+
48+ if ( ! context ) {
49+ throw new Error (
50+ 'useFrontendConfig must be used within a FrontendConfigProvider.' ,
51+ ) ;
52+ }
53+ return context ;
54+ } ;
0 commit comments