1- import { useContext , useEffect , useRef , useState } from "react" ;
1+ import { useContext , useEffect , useState } from "react" ;
22import { createPortal } from "react-dom" ;
33import Talk from "talkjs" ;
44import { BoxContext } from "./MountedBox" ;
@@ -31,47 +31,38 @@ export function HtmlPanel({
3131 conversationId,
3232 children,
3333} : HtmlPanelProps ) {
34- const panelPromise = useRef < undefined | Promise < Talk . HtmlPanel > > ( undefined ) ;
3534 const [ panel , setPanel ] = useState < undefined | Talk . HtmlPanel > ( undefined ) ;
3635 const box = useContext ( BoxContext ) ;
3736
3837 useEffect ( ( ) => {
39- function run ( ) {
40- console . log ( "@@trying" ) ;
41- if ( ! box || panelPromise . current ) return ;
42- console . log ( "@@initializing" ) ;
38+ async function run ( ) {
39+ if ( ! box || panel ) return Promise . resolve ( panel ) ;
4340
44- // const old = await panelPromise;
45- // old?.destroy();
41+ const newPanel = await box . createHtmlPanel ( {
42+ url,
43+ conversation : conversationId ,
44+ height,
45+ show : false ,
46+ } ) ;
4647
47- const panel = box
48- . createHtmlPanel ( { url, conversation : conversationId , height, show } )
49- . then ( async ( panel ) => {
50- await panel . windowLoadedPromise ;
51- console . log ( "@@window loaded" ) ;
52- // setPanel(panel);
53- return panel ;
54- } ) ;
48+ await newPanel . DOMContentLoadedPromise ;
49+ if ( show ) {
50+ newPanel . show ( ) ;
51+ }
5552
56- panelPromise . current = panel ;
53+ setPanel ( newPanel ) ;
54+ return newPanel ;
5755 }
5856
59- run ( ) ;
57+ const panelPromise = run ( ) ;
6058
61- // return () => {
62- // console.log("@@cleanup", panelPromise);
63- // if (panelPromise) {
64- // panelPromise.current?.then((panel) => {
65- // panelPromise.current = undefined;
66- // panel.destroy().then(() => {
67- // console.log("@@deleted");
68- // setPanel(undefined);
69- // });
70- // });
71- // } else {
72- // setPanel(undefined);
73- // }
74- // };
59+ return ( ) => {
60+ panelPromise . then ( ( panel ) => {
61+ panel ?. destroy ( ) . then ( ( ) => {
62+ setPanel ( undefined ) ;
63+ } ) ;
64+ } ) ;
65+ } ;
7566 // We intentionally exclude `height` and `show` from the dependency array so
7667 // that we update them later via methods instead of by re-creating the
7768 // entire panel from scratch each time.
@@ -91,6 +82,5 @@ export function HtmlPanel({
9182 }
9283 } , [ panel , show ] ) ;
9384
94- // return <>{panel && createPortal(children, panel.window.document.body)}</>;
95- return null ;
85+ return < > { panel && createPortal ( children , panel . window . document . body ) } </ > ;
9686}
0 commit comments