2020-10-09 19:35:46 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function useViewIsReady(viewRef: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
// Just checking if the iframe is ready is not sufficient because its content
|
|
|
|
// might not be ready (for example, IPC listeners might not be initialised).
|
|
|
|
// So we also listen to a custom "ready" message coming from the webview content
|
|
|
|
// (in UserWebviewIndex.js)
|
|
|
|
const [iframeReady, setIFrameReady] = useState(false);
|
|
|
|
const [iframeContentReady, setIFrameContentReady] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2020-11-16 18:14:26 +02:00
|
|
|
console.debug('useViewIsReady ============== Setup Listeners');
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
function onIFrameReady() {
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2020-11-16 18:14:26 +02:00
|
|
|
console.debug('useViewIsReady: onIFrameReady');
|
2020-10-09 19:35:46 +02:00
|
|
|
setIFrameReady(true);
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function onMessage(event: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
const data = event.data;
|
|
|
|
|
|
|
|
if (!data || data.target !== 'UserWebview') return;
|
|
|
|
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2020-11-16 18:14:26 +02:00
|
|
|
console.debug('useViewIsReady: message', data);
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
if (data.message === 'ready') {
|
|
|
|
setIFrameContentReady(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const iframeDocument = viewRef.current.contentWindow.document;
|
|
|
|
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2020-11-16 18:14:26 +02:00
|
|
|
console.debug('useViewIsReady readyState', iframeDocument.readyState);
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
if (iframeDocument.readyState === 'complete') {
|
|
|
|
onIFrameReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
viewRef.current.addEventListener('dom-ready', onIFrameReady);
|
|
|
|
viewRef.current.addEventListener('load', onIFrameReady);
|
|
|
|
viewRef.current.contentWindow.addEventListener('message', onMessage);
|
|
|
|
|
|
|
|
return () => {
|
2022-11-15 10:51:40 +02:00
|
|
|
if (viewRef.current) {
|
|
|
|
viewRef.current.removeEventListener('dom-ready', onIFrameReady);
|
|
|
|
viewRef.current.removeEventListener('load', onIFrameReady);
|
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
|
|
|
viewRef.current.contentWindow.removeEventListener('message', onMessage);
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return iframeReady && iframeContentReady;
|
|
|
|
}
|