|
| 1 | +import { observer } from "mobx-react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import React, { Suspense } from "react"; |
1 | 4 | import { createRoot } from "react-dom/client"; |
2 | | -import { flushSync } from "react-dom"; |
3 | 5 | import Variables from "../Styles/variables.scss"; |
4 | | -import UI from "./UserInterface"; |
| 6 | +import "./global.scss"; |
| 7 | +import { Loader } from "./Loader"; |
| 8 | +import { terriaStore } from "./terriaStore"; |
5 | 9 |
|
6 | | -export default function renderUi(terria, allBaseMaps, viewState) { |
7 | | - const container = document.getElementById("ui"); |
8 | | - const root = createRoot(container); |
| 10 | +// Lazy load the entire TerriaUserInterface component |
| 11 | +const LazyTerriaUserInterface = React.lazy(() => |
| 12 | + import("./UserInterface").then((module) => ({ |
| 13 | + default: module.TerriaUserInterface |
| 14 | + })) |
| 15 | +); |
| 16 | + |
| 17 | +const Root = observer(({ themeOverrides }) => { |
| 18 | + const { terria, viewState, status } = terriaStore; |
9 | 19 |
|
10 | | - // Ensure that the initial render is synchronous so there is no a white flash visible between loading and rendering |
11 | | - flushSync(() => |
12 | | - root.render( |
13 | | - <UI |
| 20 | + if (status === "loading") { |
| 21 | + return <Loader />; |
| 22 | + } |
| 23 | + |
| 24 | + return ( |
| 25 | + <Suspense fallback={<Loader />}> |
| 26 | + <LazyTerriaUserInterface |
14 | 27 | terria={terria} |
15 | | - allBaseMaps={allBaseMaps} |
16 | 28 | viewState={viewState} |
17 | | - themeOverrides={Variables} |
| 29 | + themeOverrides={themeOverrides} |
18 | 30 | /> |
19 | | - ) |
| 31 | + </Suspense> |
20 | 32 | ); |
21 | | -} |
| 33 | +}); |
| 34 | + |
| 35 | +Root.propTypes = { |
| 36 | + themeOverrides: PropTypes.object |
| 37 | +}; |
| 38 | + |
| 39 | +export const renderUi = () => { |
| 40 | + const container = document.getElementById("ui"); |
| 41 | + if (!container) { |
| 42 | + console.error("Container element with id 'ui' not found."); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const root = createRoot(container); |
| 47 | + root.render(<Root themeOverrides={Variables} />); |
| 48 | +}; |
0 commit comments