mod-arch-shared
Version:
Shared library for modular architecture micro-frontend projects
52 lines • 2.73 kB
JavaScript
import * as React from 'react';
import useNamespaces from '../hooks/useNamespaces';
import { useModularArchContext } from '../hooks';
import { DeploymentMode, PlatformMode } from '../utilities';
export const NamespaceSelectorContext = React.createContext({
namespacesLoaded: false,
namespacesLoadError: undefined,
namespaces: [],
preferredNamespace: undefined,
updatePreferredNamespace: () => undefined,
initializationError: undefined,
});
export const NamespaceSelectorContextProvider = ({ children, ...props }) => (React.createElement(EnabledNamespaceSelectorContextProvider, { ...props }, children));
const EnabledNamespaceSelectorContextProvider = ({ children, }) => {
const { deploymentMode, platformMode } = useModularArchContext();
const [unsortedNamespaces, isLoaded, error] = useNamespaces();
const namespaces = React.useMemo(() => unsortedNamespaces.toSorted((a, b) => a.name.localeCompare(b.name)), [unsortedNamespaces]);
const [preferredNamespace, setPreferredNamespace] = React.useState(undefined);
const [initializationError, setInitializationError] = React.useState();
const firstNamespace = namespaces.length > 0 ? namespaces[0] : null;
React.useEffect(() => {
if (deploymentMode === DeploymentMode.Integrated && platformMode === PlatformMode.Kubeflow) {
// Initialize the central dashboard client
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.centraldashboard.CentralDashboardEventHandler.init((cdeh) => {
// eslint-disable-next-line no-param-reassign
cdeh.onNamespaceSelected = (newNamespace) => {
setPreferredNamespace({ name: newNamespace });
};
});
}
catch (err) {
/* eslint-disable no-console */
console.error('Failed to initialize central dashboard client', err);
if (err instanceof Error) {
setInitializationError(err);
}
}
}
}, [deploymentMode, platformMode]);
const contextValue = React.useMemo(() => ({
namespacesLoaded: isLoaded,
namespacesLoadError: error,
namespaces,
preferredNamespace: preferredNamespace ?? firstNamespace ?? undefined,
updatePreferredNamespace: setPreferredNamespace,
initializationError,
}), [isLoaded, error, namespaces, preferredNamespace, firstNamespace, initializationError]);
return (React.createElement(NamespaceSelectorContext.Provider, { value: contextValue }, children));
};
//# sourceMappingURL=NamespaceSelectorContext.js.map