mod-arch-shared
Version:
Shared library for modular architecture micro-frontend projects
39 lines • 1.74 kB
JavaScript
import React, { useEffect, useState } from 'react';
import { Bullseye, Spinner } from '@patternfly/react-core';
import { useModularArchContext } from '../hooks';
import { DeploymentMode, PlatformMode } from '../utilities';
const loadScript = (src, onLoad, onError) => {
const script = document.createElement('script');
script.src = src;
script.async = true;
script.onload = onLoad;
script.onerror = onError;
document.head.appendChild(script);
};
/* eslint-disable no-console */
export const DashboardScriptLoader = ({ children }) => {
const { deploymentMode, platformMode } = useModularArchContext();
const [scriptLoaded, setScriptLoaded] = useState(false);
useEffect(() => {
const scriptUrl = '/dashboard_lib.bundle.js';
if (!(deploymentMode === DeploymentMode.Integrated) ||
!(platformMode === PlatformMode.Kubeflow)) {
console.warn('DashboardScriptLoader: Script not loaded, only needed for kubeflow integration');
setScriptLoaded(true);
return;
}
fetch(scriptUrl, { method: 'HEAD' })
.then((response) => {
if (response.ok) {
loadScript(scriptUrl, () => setScriptLoaded(true), () => console.error('Failed to load the script'));
}
else {
console.warn('Script not found');
}
})
.catch((error) => console.error('Error checking script existence', error));
}, [deploymentMode, platformMode]);
return !scriptLoaded ? (React.createElement(Bullseye, null,
React.createElement(Spinner, null))) : (React.createElement(React.Fragment, null, children));
};
//# sourceMappingURL=DashboardScriptLoader.js.map