UNPKG

mod-arch-shared

Version:

Shared library for modular architecture micro-frontend projects

68 lines 3.18 kB
import React, { createContext, useEffect, useState } from 'react'; import { Bullseye, Spinner } from '@patternfly/react-core'; import { kubeflowScriptLoader, kubeflowNamespaceLoader } from '../utilities'; import { useNamespacesWithConfig } from '../hooks/useNamespaces'; export const ModularArchContext = createContext(undefined); export const ModularArchContextProvider = ({ children, config, }) => { const { deploymentMode, platformMode, mandatoryNamespace } = config; const [scriptLoaded, setScriptLoaded] = useState(false); const [preferredNamespace, setPreferredNamespace] = useState(undefined); const [initializationError, setInitializationError] = useState(); // Namespace-related state const [unsortedNamespaces, isLoaded, error] = useNamespacesWithConfig(config); const namespaces = React.useMemo(() => unsortedNamespaces.toSorted((a, b) => a.name.localeCompare(b.name)), [unsortedNamespaces]); const firstNamespace = namespaces.length > 0 ? namespaces[0] : null; // Set preferred namespace based on mandatory namespace or first available namespace const defaultPreferredNamespace = React.useMemo(() => { if (mandatoryNamespace) { return { name: mandatoryNamespace }; } return firstNamespace; }, [mandatoryNamespace, firstNamespace]); // Script loader for kubeflow integration useEffect(() => { kubeflowScriptLoader(deploymentMode, platformMode, () => setScriptLoaded(true), (scriptError) => { // eslint-disable-next-line no-console console.error('Error loading kubeflow script:', scriptError); setScriptLoaded(true); // Still set to true to not block the UI }); }, [deploymentMode, platformMode]); // Namespace selector for kubeflow integration useEffect(() => { // If mandatory namespace is set, don't use kubeflow namespace loader if (mandatoryNamespace) { return; } kubeflowNamespaceLoader(deploymentMode, platformMode, scriptLoaded, (newNamespace) => { setPreferredNamespace({ name: newNamespace }); }, (err) => { setInitializationError(err); }, mandatoryNamespace); }, [deploymentMode, platformMode, scriptLoaded, mandatoryNamespace]); const contextValue = React.useMemo(() => ({ config, namespacesLoaded: isLoaded, namespacesLoadError: error, namespaces, preferredNamespace: preferredNamespace ?? defaultPreferredNamespace ?? undefined, updatePreferredNamespace: setPreferredNamespace, initializationError, scriptLoaded, }), [ config, isLoaded, error, namespaces, preferredNamespace, defaultPreferredNamespace, initializationError, scriptLoaded, ]); // Show loading spinner if script is not loaded yet if (!scriptLoaded) { return (React.createElement(Bullseye, null, React.createElement(Spinner, null))); } return React.createElement(ModularArchContext.Provider, { value: contextValue }, children); }; //# sourceMappingURL=ModularArchContext.js.map