import { Suspense, forwardRef, useCallback, useEffect, lazy } from 'react' import { useLocation } from 'react-router-dom' import { RemoteModule, useRemoteModule, } from '@sentre/react-dynamic-remote-component' import { Layout, Row, Col, Typography, Button } from 'antd' import IonIcon from '@sentre/antd-ionicon' import ErrorBoundary from 'components/errorBoundary' import PageLoading from './loading' import { RootDispatch, useRootDispatch } from 'store' import { setBackground } from 'store/ui.reducer' /** * Local Component */ const LazyLocalComponent = lazy(() => import(process.env.REACT_APP_HMR as string).then((module) => ({ default: module.Page, })), ) const LocalComponent = forwardRef( ({ manifest, ...props }, ref) => { return }, ) LocalComponent.displayName = 'LocalComponent' /** * Remote Component */ const RemoteComponent = forwardRef( ({ manifest, ...props }, ref) => { const { Page: Component } = useRemoteModule(manifest) return }, ) RemoteComponent.displayName = 'RemoteComponent' /** * Error Component */ const PageError = ({ url = 'Unknown' }: { url?: string }) => { const retry = () => window.location.reload() const support = useCallback(() => { return window.open( `mailto:hi@sentre.io?subject=${url} has failed`, '_blank', ) }, [url]) return ( {url}

Oops! The application can't load properly.

) } /** * Page Loader */ const PageLoader = forwardRef( ({ url, appId, ...props }, ref) => { const manifest = { url, scope: appId, module: './bootstrap' } const dispatch = useRootDispatch() const { pathname } = useLocation() const currentAppId = pathname.split('/')[2] // We have to watch on the current appId to check mount/unmount events. useEffect(() => { // Mount executions document.title = `${props.name} | Sentre Hub` // Unmount executions inside the return return () => { if (!currentAppId) return dispatch(setBackground({ light: '', dark: '' })) document.title = 'Sentre Hub' } }, [dispatch, currentAppId, props.name]) // To activate the Hot Module Replacement (HMR) // We must directly mount the local component instead of the remote one. const hmr = process.env.REACT_APP_HMR && appId === process.env.REACT_APP_ID const Component = hmr ? LocalComponent : RemoteComponent return ( }> }> ) }, ) PageLoader.displayName = 'PageLoader' export default PageLoader