aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/index.tsx
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-10-07 19:44:41 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-11-05 19:41:14 +0100
commitabe176d0888c0fdcc803ddafe71a5cee9f4b63a0 (patch)
treeea7d5722c8e981ba540e648fbacbfd0e4a55768a /subprojects/frontend/src/index.tsx
parentfix: test and lint failures (diff)
downloadrefinery-abe176d0888c0fdcc803ddafe71a5cee9f4b63a0.tar.gz
refinery-abe176d0888c0fdcc803ddafe71a5cee9f4b63a0.tar.zst
refinery-abe176d0888c0fdcc803ddafe71a5cee9f4b63a0.zip
refactor(frontend): improve HMR experience
Use a HMR acceptor as in https://github.com/vitejs/vite/issues/10227#issuecomment-1256969751 Also updates frontend tooling to the latest version (yarn now support typescript 4.8.4)
Diffstat (limited to 'subprojects/frontend/src/index.tsx')
-rw-r--r--subprojects/frontend/src/index.tsx76
1 files changed, 42 insertions, 34 deletions
diff --git a/subprojects/frontend/src/index.tsx b/subprojects/frontend/src/index.tsx
index f7fe61b9..2c0259bf 100644
--- a/subprojects/frontend/src/index.tsx
+++ b/subprojects/frontend/src/index.tsx
@@ -1,16 +1,9 @@
1import Box from '@mui/material/Box';
2import CssBaseline from '@mui/material/CssBaseline';
3import { configure } from 'mobx'; 1import { configure } from 'mobx';
4import React, { Suspense, lazy } from 'react'; 2import React from 'react';
5import { createRoot } from 'react-dom/client'; 3import { type Root, createRoot } from 'react-dom/client';
6 4
7import Loading from './Loading'; 5import App from './App';
8import RootStore, { RootStoreProvider } from './RootStore'; 6import RootStore from './RootStore';
9import WindowControlsOverlayColor from './WindowControlsOverlayColor';
10import ThemeProvider from './theme/ThemeProvider';
11import getLogger from './utils/getLogger';
12
13const log = getLogger('index');
14 7
15const initialValue = `class Family { 8const initialValue = `class Family {
16 contains Person[] members 9 contains Person[] members
@@ -57,30 +50,45 @@ configure({
57 enforceActions: 'always', 50 enforceActions: 'always',
58}); 51});
59 52
60const rootStore = new RootStore(initialValue); 53let HotRootStore = RootStore;
54let HotApp = App;
55
56function createStore(): RootStore {
57 return new HotRootStore(initialValue);
58}
61 59
62const App = lazy(() => import('./App.js')); 60let rootStore = createStore();
63 61
64const app = ( 62let root: Root | undefined;
65 <React.StrictMode>
66 <RootStoreProvider rootStore={rootStore}>
67 <ThemeProvider>
68 <CssBaseline enableColorScheme />
69 <WindowControlsOverlayColor />
70 <Box height="100vh" overflow="auto">
71 <Suspense fallback={<Loading />}>
72 <App />
73 </Suspense>
74 </Box>
75 </ThemeProvider>
76 </RootStoreProvider>
77 </React.StrictMode>
78);
79 63
80const rootElement = document.getElementById('app'); 64function render(): void {
81if (rootElement === null) { 65 root?.render(<HotApp rootStore={rootStore} />);
82 log.error('Root element not found');
83} else {
84 const root = createRoot(rootElement);
85 root.render(app);
86} 66}
67
68if (import.meta.hot) {
69 import.meta.hot.accept('./App', (module) => {
70 if (module === undefined) {
71 return;
72 }
73 ({ default: HotApp } = module as unknown as typeof import('./App'));
74 render();
75 });
76 import.meta.hot.accept('./RootStore', (module) => {
77 if (module === undefined) {
78 return;
79 }
80 ({ default: HotRootStore } =
81 module as unknown as typeof import('./RootStore'));
82 rootStore.dispose();
83 rootStore = createStore();
84 render();
85 });
86}
87
88document.addEventListener('DOMContentLoaded', () => {
89 const rootElement = document.getElementById('app');
90 if (rootElement !== null) {
91 root = createRoot(rootElement);
92 render();
93 }
94});