1- import { useContext , useEffect , useState } from "react" ;
1+ import { useContext , useEffect , useRef , useState } from "react" ;
22import { createPortal } from "react-dom" ;
33import Talk from "talkjs" ;
44import { BoxContext } from "./MountedBox" ;
@@ -24,71 +24,73 @@ type HtmlPanelProps = {
2424 children : React . ReactNode ;
2525} ;
2626
27- type State =
28- | { type : "none" }
29- | { type : "loading" }
30- | { type : "loaded" ; panel : Talk . HtmlPanel } ;
31-
3227export function HtmlPanel ( {
3328 url,
3429 height = 100 ,
3530 show = true ,
3631 conversationId,
3732 children,
3833} : HtmlPanelProps ) {
39- const [ state , setState ] = useState < State > ( { type : "none" } ) ;
34+ const panelPromise = useRef < undefined | Promise < Talk . HtmlPanel > > ( undefined ) ;
35+ const [ panel , setPanel ] = useState < undefined | Talk . HtmlPanel > ( undefined ) ;
4036 const box = useContext ( BoxContext ) ;
4137
4238 useEffect ( ( ) => {
43- async function run ( ) {
44- if ( state . type !== "none" || ! box ) return ;
39+ function run ( ) {
40+ console . log ( "@@trying" ) ;
41+ if ( ! box || panelPromise . current ) return ;
42+ console . log ( "@@initializing" ) ;
43+
44+ // const old = await panelPromise;
45+ // old?.destroy();
4546
46- setState ( { type : "loading" } ) ;
47- const panel = await box . createHtmlPanel ( {
48- url,
49- conversation : conversationId ,
50- height,
51- show,
52- } ) ;
53- await panel . windowLoadedPromise ;
54- setState ( { type : "loaded" , panel } ) ;
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+ } ) ;
55+
56+ panelPromise . current = panel ;
5557 }
5658
5759 run ( ) ;
5860
59- return ( ) => {
60- if ( state . type === "loaded" ) {
61- state . panel . destroy ( ) ;
62- setState ( { type : "none" } ) ;
63- }
64- } ;
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+ // };
6575 // We intentionally exclude `height` and `show` from the dependency array so
6676 // that we update them later via methods instead of by re-creating the
6777 // entire panel from scratch each time.
6878 //
6979 // eslint-disable-next-line react-hooks/exhaustive-deps
70- } , [ state , url , box , conversationId ] ) ;
80+ } , [ url , box , conversationId ] ) ;
7181
7282 useEffect ( ( ) => {
73- if ( state . type === "loaded" ) {
74- state . panel . setHeight ( height ) ;
75- }
76- } , [ state , height ] ) ;
83+ panel ?. setHeight ( height ) ;
84+ } , [ panel , height ] ) ;
7785
7886 useEffect ( ( ) => {
79- if ( state . type === "loaded" ) {
80- if ( show ) {
81- state . panel . show ( ) ;
82- } else {
83- state . panel . hide ( ) ;
84- }
87+ if ( show ) {
88+ panel ?. show ( ) ;
89+ } else {
90+ panel ?. hide ( ) ;
8591 }
86- } , [ state , show ] ) ;
92+ } , [ panel , show ] ) ;
8793
88- return (
89- < >
90- { state . type === "loaded" &&
91- createPortal ( children , state . panel . window . document . body ) }
92- </ >
93- ) ;
94+ // return <>{panel && createPortal(children, panel.window.document.body)}</>;
95+ return null ;
9496}
0 commit comments