Commit aaf93b2f by Hugo Häggmark Committed by Torkel Ödegaard

Feature: Encapsulated dynamic imports with error boundary and suspense (#19128)

* Feature: Encapsulated dynamic imports with error boundary and suspense

* Refactor: Changes after PR review

* Align PageLoader and LoadingPlaceholder

* Updated loading failed UI abit

* Change Failed to Unable
parent 80592e33
import React, { FC } from 'react'; import React, { FC } from 'react';
import { LoadingPlaceholder } from '@grafana/ui';
interface Props { interface Props {
pageName?: string; pageName?: string;
...@@ -8,8 +9,7 @@ const PageLoader: FC<Props> = ({ pageName = '' }) => { ...@@ -8,8 +9,7 @@ const PageLoader: FC<Props> = ({ pageName = '' }) => {
const loadingText = `Loading ${pageName}...`; const loadingText = `Loading ${pageName}...`;
return ( return (
<div className="page-loader-wrapper"> <div className="page-loader-wrapper">
<i className="page-loader-wrapper__spinner fa fa-spinner fa-spin" /> <LoadingPlaceholder text={loadingText} />
<div className="page-loader-wrapper__text">{loadingText}</div>
</div> </div>
); );
}; };
......
import React, { lazy, Suspense, FunctionComponent } from 'react';
import { cx, css } from 'emotion';
import { LoadingPlaceholder, ErrorBoundary, Button } from '@grafana/ui';
export const LoadingChunkPlaceHolder: FunctionComponent = () => (
<div className={cx('preloader')}>
<LoadingPlaceholder text={'Loading...'} />
</div>
);
function getAlertPageStyle() {
return css`
width: 508px;
margin: 128px auto;
`;
}
export const SafeDynamicImport = (importStatement: Promise<any>) => ({ ...props }) => {
const LazyComponent = lazy(() => importStatement);
return (
<ErrorBoundary>
{({ error, errorInfo }) => {
if (!errorInfo) {
return (
<Suspense fallback={<LoadingChunkPlaceHolder />}>
<LazyComponent {...props} />
</Suspense>
);
}
return (
<div className={getAlertPageStyle()}>
<h2>Unable to find application file</h2>
<br />
<h2 className="page-heading">Grafana has likely been updated. Please try reloading the page.</h2>
<br />
<div className="gf-form-group">
<Button size={'md'} variant={'secondary'} icon="fa fa-repeat" onClick={() => window.location.reload()}>
Reload
</Button>
</div>
<details style={{ whiteSpace: 'pre-wrap' }}>
{error && error.toString()}
<br />
{errorInfo.componentStack}
</details>
</div>
);
}}
</ErrorBoundary>
);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment