From 1f04016a15df63f8c4679f528966aeebf4ceb3ef Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Thu, 27 Jun 2024 14:37:47 +0200 Subject: [PATCH] Use React 18 renderer and fix empty cell bug --- package.json | 1 + src/H5WebInCell.tsx | 22 ++++++++++++++++++++++ src/H5webApp.tsx | 28 +++++++--------------------- src/mimeplugin.tsx | 15 ++++----------- yarn.lock | 3 ++- 5 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 src/H5WebInCell.tsx diff --git a/package.json b/package.json index d74f283..52798ca 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "@jupyterlab/services": "^7.0.0", "@jupyterlab/ui-components": "^4.0.0", "@lumino/widgets": "^2.0.0", + "@react-hookz/web": "^24.0.4", "react": "^18.0.0", "react-dom": "^18.0.0" }, diff --git a/src/H5WebInCell.tsx b/src/H5WebInCell.tsx new file mode 100644 index 0000000..7a40dcf --- /dev/null +++ b/src/H5WebInCell.tsx @@ -0,0 +1,22 @@ +import React from 'react'; +import H5webApp from './H5webApp'; +import { useMeasure } from '@react-hookz/web'; + +interface Props { + path: string; +} + +function H5WebInCell(props: Props) { + const { path } = props; + + // Wait for cell to have non-empty size so H5Web can render safely + const [rect, cellRef] = useMeasure(); + + return ( +
+ {rect && rect.width > 0 && } +
+ ); +} + +export default H5WebInCell; diff --git a/src/H5webApp.tsx b/src/H5webApp.tsx index 97b352e..d61789f 100644 --- a/src/H5webApp.tsx +++ b/src/H5webApp.tsx @@ -1,28 +1,9 @@ -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useMemo } from 'react'; import { App, getFeedbackMailto, H5GroveProvider } from '@h5web/app'; import { ServerConnection } from '@jupyterlab/services'; const FEEDBACK_EMAIL = 'h5web@esrf.fr'; -function TwoRenderApp() { - const [isFirstRender, setIsFirstRender] = useState(true); - - useEffect(() => { - setIsFirstRender(false); - }, []); - - if (isFirstRender) { - return null; - } - - return ( - getFeedbackMailto(context, FEEDBACK_EMAIL)} - disableDarkMode - /> - ); -} - function H5webApp(props: { filePath: string }) { const { filePath } = props; const { baseUrl, token } = ServerConnection.makeSettings(); @@ -42,7 +23,12 @@ function H5webApp(props: { filePath: string }) { filepath={filePath} axiosConfig={axiosConfig} > - + + getFeedbackMailto(context, FEEDBACK_EMAIL) + } + disableDarkMode + /> ); diff --git a/src/mimeplugin.tsx b/src/mimeplugin.tsx index 151bdbb..9525db8 100644 --- a/src/mimeplugin.tsx +++ b/src/mimeplugin.tsx @@ -2,10 +2,10 @@ import type { IRenderMime } from '@jupyterlab/rendermime-interfaces'; import { Widget } from '@lumino/widgets'; import React from 'react'; -import ReactDOM from 'react-dom'; +import { createRoot } from 'react-dom/client'; import HDF5_FILE_TYPE from './fileType'; -import H5webApp from './H5webApp'; +import H5WebInCell from './H5WebInCell'; class HDF5FilePathRenderer extends Widget implements IRenderMime.IRenderer { /* Renders HDF5 files from their path with H5web */ @@ -22,15 +22,8 @@ class HDF5FilePathRenderer extends Widget implements IRenderMime.IRenderer { throw new TypeError('Expected string'); } - return new Promise((resolve) => { - ReactDOM.render( -
- -
, - this.node, - () => resolve() - ); - }); + createRoot(this.node).render(); + return Promise.resolve(); } } diff --git a/yarn.lock b/yarn.lock index b127081..2925557 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1329,7 +1329,7 @@ __metadata: languageName: node linkType: hard -"@react-hookz/web@npm:24.0.4": +"@react-hookz/web@npm:24.0.4, @react-hookz/web@npm:^24.0.4": version: 24.0.4 resolution: "@react-hookz/web@npm:24.0.4" dependencies: @@ -5200,6 +5200,7 @@ __metadata: "@jupyterlab/services": ^7.0.0 "@jupyterlab/ui-components": ^4.0.0 "@lumino/widgets": ^2.0.0 + "@react-hookz/web": ^24.0.4 "@types/node": ^18.15.11 "@types/react": ^18.0.0 "@types/react-dom": ^18.0.0