c32dae6eb9
* feat: add node swicther * chore: remove hook form from node switcher * feat: generate apollo types and add tests * fix: format * fix: types * fix: remove redundant wrapper * fix: layout styles * fix: add controlled value to radio group * fix: flaky node hook test * feat: hook in node switcher to the explorer & token footer info * fix: cache key handling for env config * fix: env type in tests * fix: format again * fix: use netlify git env vars * fix: remove commented styles * fix: replace clsx with classnames * fix: dialog sizes * fix: fetch config by default * fix: format * fix: dialog close
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import type { ReactNode } from 'react';
|
|
import { useState, createContext, useContext } from 'react';
|
|
|
|
import { NodeSwitcherDialog } from '../components/node-switcher-dialog';
|
|
import { useConfig } from './use-config';
|
|
import { compileEnvironment } from '../utils/compile-environment';
|
|
import { validateEnvironment } from '../utils/validate-environment';
|
|
import type { Environment, RawEnvironment, ConfigStatus } from '../types';
|
|
|
|
type EnvironmentProviderProps = {
|
|
definitions?: Partial<RawEnvironment>;
|
|
children?: ReactNode;
|
|
};
|
|
|
|
export type EnvironmentState = Environment & {
|
|
configStatus: ConfigStatus;
|
|
setNodeSwitcherOpen: () => void;
|
|
};
|
|
|
|
const EnvironmentContext = createContext({} as EnvironmentState);
|
|
|
|
export const EnvironmentProvider = ({
|
|
definitions,
|
|
children,
|
|
}: EnvironmentProviderProps) => {
|
|
const [isNodeSwitcherOpen, setNodeSwitcherOpen] = useState(false);
|
|
const [environment, updateEnvironment] = useState<Environment>(
|
|
compileEnvironment(definitions)
|
|
);
|
|
const { status: configStatus, config } = useConfig(
|
|
environment,
|
|
updateEnvironment
|
|
);
|
|
|
|
const errorMessage = validateEnvironment(environment);
|
|
|
|
if (errorMessage) {
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
return (
|
|
<EnvironmentContext.Provider
|
|
value={{
|
|
...environment,
|
|
configStatus,
|
|
setNodeSwitcherOpen: () => setNodeSwitcherOpen(true),
|
|
}}
|
|
>
|
|
{config && (
|
|
<NodeSwitcherDialog
|
|
dialogOpen={isNodeSwitcherOpen}
|
|
toggleDialogOpen={setNodeSwitcherOpen}
|
|
config={config}
|
|
onConnect={(url) =>
|
|
updateEnvironment((env) => ({ ...env, VEGA_URL: url }))
|
|
}
|
|
/>
|
|
)}
|
|
{children}
|
|
</EnvironmentContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useEnvironment = () => {
|
|
const context = useContext(EnvironmentContext);
|
|
if (context === undefined) {
|
|
throw new Error(
|
|
'Error running "useEnvironment". No context found, make sure your component is wrapped in an <EnvironmentProvider />.'
|
|
);
|
|
}
|
|
return context;
|
|
};
|