-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConfigProvider.tsx
More file actions
46 lines (42 loc) · 1.39 KB
/
ConfigProvider.tsx
File metadata and controls
46 lines (42 loc) · 1.39 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 { ProtocolConfig } from "@bosonprotocol/react-kit";
import { MagicProvider } from "components/magicLink/MagicContext";
import {
CONFIG,
defaultEnvConfig,
envConfigsFilteredByEnv,
getDappConfig
} from "lib/config";
import { useChainId } from "lib/utils/hooks/connection/connection";
import { ReactNode, useEffect, useState } from "react";
import { Context, useConfigContext } from "./ConfigContext";
type ConfigProviderProps = {
children: ReactNode;
};
function SyncCurrentConfigId({
children
}: Pick<ConfigProviderProps, "children">) {
const chainId = useChainId();
const { setEnvConfig } = useConfigContext();
useEffect(() => {
const newEnvConfig = envConfigsFilteredByEnv.find(
(envConfig) => envConfig.chainId === chainId
);
if (newEnvConfig) {
setEnvConfig(newEnvConfig);
}
}, [chainId, setEnvConfig]);
return <>{children}</>;
}
export function ConfigProvider({ children }: ConfigProviderProps) {
const [envConfig, setEnvConfig] = useState<ProtocolConfig>(defaultEnvConfig);
const dappConfig = getDappConfig(envConfig || defaultEnvConfig);
// TODO: change at core-component level
dappConfig.lens.ipfsGateway = CONFIG.ipfsGateway;
return (
<Context.Provider value={{ config: dappConfig, setEnvConfig }}>
<MagicProvider>
<SyncCurrentConfigId>{children}</SyncCurrentConfigId>
</MagicProvider>
</Context.Provider>
);
}