-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪟 🔧 Refactor webapp configuration provider (#21456)
- Loading branch information
Showing
31 changed files
with
159 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
import React, { useContext, useMemo } from "react"; | ||
import { useAsync } from "react-use"; | ||
import React, { useContext } from "react"; | ||
|
||
import { LoadingPage } from "components"; | ||
import { AirbyteWebappConfig } from "./types"; | ||
|
||
import { applyProviders } from "./configProviders"; | ||
import { Config, ValueProvider } from "./types"; | ||
|
||
export interface ConfigContextData<T extends Config = Config> { | ||
config: T; | ||
export interface ConfigContextData { | ||
config: AirbyteWebappConfig; | ||
} | ||
|
||
export const ConfigContext = React.createContext<ConfigContextData | null>(null); | ||
|
||
export function useConfig<T extends Config>(): T { | ||
export function useConfig(): AirbyteWebappConfig { | ||
const configService = useContext(ConfigContext); | ||
|
||
if (configService === null) { | ||
throw new Error("useConfig must be used within a ConfigProvider"); | ||
} | ||
|
||
return useMemo(() => configService.config as unknown as T, [configService.config]); | ||
return configService.config; | ||
} | ||
|
||
const ConfigServiceInner: React.FC< | ||
export const ConfigServiceProvider: React.FC< | ||
React.PropsWithChildren<{ | ||
defaultConfig: Config; | ||
providers?: ValueProvider<Config>; | ||
config: AirbyteWebappConfig; | ||
}> | ||
> = ({ children, defaultConfig, providers }) => { | ||
const { loading, value } = useAsync( | ||
async () => (providers ? applyProviders(defaultConfig, providers) : defaultConfig), | ||
[providers] | ||
); | ||
const config: ConfigContextData | null = useMemo(() => (value ? { config: value } : null), [value]); | ||
|
||
if (loading) { | ||
return <LoadingPage />; | ||
} | ||
|
||
return <ConfigContext.Provider value={config}>{children}</ConfigContext.Provider>; | ||
> = ({ children, config }) => { | ||
return <ConfigContext.Provider value={{ config }}>{children}</ConfigContext.Provider>; | ||
}; | ||
|
||
export const ConfigServiceProvider = React.memo(ConfigServiceInner); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { AirbyteWebappConfig } from "./types"; | ||
|
||
export const config: AirbyteWebappConfig = { | ||
segment: { | ||
token: window.SEGMENT_TOKEN ?? process.env.REACT_APP_SEGMENT_TOKEN, | ||
enabled: window.TRACKING_STRATEGY === "segment", | ||
}, | ||
apiUrl: window.API_URL ?? process.env.REACT_APP_API_URL ?? `http://${window.location.hostname}:8001/api`, | ||
connectorBuilderApiUrl: process.env.REACT_APP_CONNECTOR_BUILDER_API_URL ?? `http://${window.location.hostname}:8003`, | ||
version: window.AIRBYTE_VERSION ?? "dev", | ||
integrationUrl: process.env.REACT_APP_INTEGRATION_DOCS_URLS ?? "/docs", | ||
oauthRedirectUrl: `${window.location.protocol}//${window.location.host}`, | ||
cloudApiUrl: window.CLOUD_API_URL ?? process.env.REACT_APP_CLOUD_API_URL, | ||
cloudPublicApiUrl: process.env.REACT_APP_CLOUD_PUBLIC_API_URL, | ||
firebase: { | ||
apiKey: window.FIREBASE_API_KEY ?? process.env.REACT_APP_FIREBASE_API_KEY, | ||
authDomain: window.FIREBASE_AUTH_DOMAIN ?? process.env.REACT_APP_FIREBASE_AUTH_DOMAIN, | ||
authEmulatorHost: window.FIREBASE_AUTH_EMULATOR_HOST ?? process.env.REACT_APP_FIREBASE_AUTH_EMULATOR_HOST, | ||
}, | ||
intercom: { | ||
appId: process.env.REACT_APP_INTERCOM_APP_ID, | ||
}, | ||
launchDarkly: window.LAUNCHDARKLY_KEY ?? process.env.REACT_APP_LAUNCHDARKLY_KEY, | ||
datadog: { | ||
applicationId: window.REACT_APP_DATADOG_APPLICATION_ID ?? process.env.REACT_APP_DATADOG_APPLICATION_ID, | ||
clientToken: window.REACT_APP_DATADOG_CLIENT_TOKEN ?? process.env.REACT_APP_DATADOG_CLIENT_TOKEN, | ||
site: window.REACT_APP_DATADOG_SITE ?? process.env.REACT_APP_DATADOG_SITE, | ||
service: window.REACT_APP_DATADOG_SERVICE ?? process.env.REACT_APP_DATADOG_SERVICE, | ||
}, | ||
sentryDsn: window.REACT_APP_SENTRY_DSN ?? process.env.REACT_APP_SENTRY_DSN, | ||
webappTag: window.REACT_APP_WEBAPP_TAG ?? process.env.REACT_APP_WEBAPP_TAG ?? "dev", | ||
}; | ||
|
||
export class MissingConfigError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "MissingConfigError"; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from "./defaultConfig"; | ||
export * from "./configProviders"; | ||
export * from "./config"; | ||
export * from "./ConfigServiceProvider"; | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.