-
Notifications
You must be signed in to change notification settings - Fork 4.6k
🪟 🔧 Refactor webapp configuration provider #21456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 21 commits
34f23b6
28af138
5fcc0f5
20bb6ff
7bc28f3
7bbf7b7
0f4d870
39c435a
d680628
fb72243
54196d0
074fd82
7eb471a
54d1add
bb7c231
87a6971
d0444a7
399a930
da5e1c7
cf9111b
b513266
8e751cf
2d7e6d5
cfbd267
7d15c84
dfc5a6d
70a8f09
54b672d
1550b3a
4ab4102
de0cad3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 ?? | ||
`${window.location.protocol}//${window.location.hostname}:8001/api`, | ||
connectorBuilderApiUrl: | ||
process.env.REACT_APP_CONNECTOR_BUILDER_API_URL ?? `${window.location.protocol}//${window.location.hostname}:8003`, | ||
healthCheckInterval: 20000, | ||
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, | ||
}; | ||
|
||
export class MissingConfigError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "MissingConfigError"; | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
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"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,33 @@ declare global { | |
SEGMENT_TOKEN?: string; | ||
LAUNCHDARKLY_KEY?: string; | ||
analytics: SegmentAnalytics.AnalyticsJS; | ||
// Cloud specific properties | ||
FIREBASE_API_KEY?: string; | ||
FIREBASE_AUTH_DOMAIN?: string; | ||
FIREBASE_AUTH_EMULATOR_HOST?: string; | ||
CLOUD_API_URL?: string; | ||
CLOUD_PUBLIC_API_URL?: string; | ||
} | ||
} | ||
|
||
export interface Config { | ||
segment: { token: string; enabled: boolean }; | ||
export interface AirbyteWebappConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to make it more clear what this is a config for |
||
segment: { token?: string; enabled: boolean }; | ||
apiUrl: string; | ||
connectorBuilderApiUrl: string; | ||
oauthRedirectUrl: string; | ||
healthCheckInterval: number; | ||
version?: string; | ||
version: string; | ||
integrationUrl: string; | ||
oauthRedirectUrl: string; | ||
cloudApiUrl?: string; | ||
cloudPublicApiUrl?: string; | ||
firebase: { | ||
apiKey?: string; | ||
authDomain?: string; | ||
authEmulatorHost?: string; | ||
}; | ||
intercom: { | ||
appId?: string; | ||
}; | ||
launchDarkly?: string; | ||
} | ||
|
||
|
@@ -39,4 +55,4 @@ export type Provider<T> = () => T; | |
|
||
export type ValueProvider<T> = Array<ProviderAsync<DeepPartial<T>>>; | ||
|
||
export type ConfigProvider<T extends Config = Config> = ProviderAsync<DeepPartial<T>>; | ||
export type ConfigProvider<T extends AirbyteWebappConfig = AirbyteWebappConfig> = ProviderAsync<DeepPartial<T>>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think most of those types could still be removed or the Provider which seems to be used in the auth service, just inlined there instead (and most likely doesn't need a named type). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, forgot that I wanted to clean up (remove) these unnecessary types! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed here 1550b3a |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,14 @@ import React, { useMemo } from "react"; | |
|
||
import { LoadingPage } from "components"; | ||
|
||
import { MissingConfigError, useConfig } from "config"; | ||
import { ApiServices } from "core/ApiServices"; | ||
import { RequestMiddleware } from "core/request/RequestMiddleware"; | ||
import { ServicesProvider, useGetService, useInjectServices } from "core/servicesProvider"; | ||
import { RequestAuthMiddleware } from "packages/cloud/lib/auth/RequestAuthMiddleware"; | ||
import { UserService } from "packages/cloud/lib/domain/users"; | ||
import { useAuth } from "packages/firebaseReact"; | ||
|
||
import { useConfig } from "./config"; | ||
import { FirebaseSdkProvider } from "./FirebaseSdkProvider"; | ||
|
||
/** | ||
|
@@ -43,6 +43,10 @@ const ServiceOverrides: React.FC<React.PropsWithChildren<unknown>> = React.memo( | |
|
||
const { cloudApiUrl } = useConfig(); | ||
|
||
if (!cloudApiUrl) { | ||
throw new MissingConfigError("Missing required configuration cloudApiUrl"); | ||
} | ||
Comment on lines
+46
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option would be to log the error with our |
||
|
||
const inject = useMemo( | ||
() => ({ | ||
UserService: new UserService(cloudApiUrl, middlewares), | ||
|
Uh oh!
There was an error while loading. Please reload this page.