|
| 1 | +import { useContext, useEffect, useState } from "react"; |
| 2 | +import { createPortal } from "react-dom"; |
| 3 | +import Talk from "talkjs"; |
| 4 | +import { BoxContext } from "./MountedBox"; |
| 5 | + |
| 6 | +type HtmlPanelProps = { |
| 7 | + /** |
| 8 | + * The URL you want to load inside the HTML panel. The URL can be absolute or |
| 9 | + * relative. We recommend using same origin pages to have better control of |
| 10 | + * the page. Learn more about HTML Panels and same origin pages {@link https://talkjs.com/docs/Features/Customizations/HTML_Panels/ | here}. |
| 11 | + */ |
| 12 | + url: string; |
| 13 | + |
| 14 | + /** The panel height in pixels. Defaults to `100px`. */ |
| 15 | + height?: number; |
| 16 | + |
| 17 | + /** Sets the visibility of the panel. Defaults to `true`. */ |
| 18 | + show?: boolean; |
| 19 | + |
| 20 | + /** If given, the panel will only show up for the conversation that has an `id` matching the one given. */ |
| 21 | + conversationId?: string; |
| 22 | + |
| 23 | + /** The content that gets rendered inside the `<body>` of the panel. */ |
| 24 | + children: React.ReactNode; |
| 25 | +}; |
| 26 | + |
| 27 | +type State = |
| 28 | + | { type: "none" } |
| 29 | + | { type: "loading" } |
| 30 | + | { type: "loaded"; panel: Talk.HtmlPanel }; |
| 31 | + |
| 32 | +export function HtmlPanel({ |
| 33 | + url, |
| 34 | + height = 100, |
| 35 | + show = true, |
| 36 | + conversationId, |
| 37 | + children, |
| 38 | +}: HtmlPanelProps) { |
| 39 | + const [state, setState] = useState<State>({ type: "none" }); |
| 40 | + const box = useContext(BoxContext); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + async function run() { |
| 44 | + if (state.type !== "none" || !box) return; |
| 45 | + |
| 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 }); |
| 55 | + } |
| 56 | + |
| 57 | + run(); |
| 58 | + |
| 59 | + return () => { |
| 60 | + if (state.type === "loaded") { |
| 61 | + state.panel.destroy(); |
| 62 | + setState({ type: "none" }); |
| 63 | + } |
| 64 | + }; |
| 65 | + // We intentionally exclude `height` and `show` from the dependency array so |
| 66 | + // that we update them later via methods instead of by re-creating the |
| 67 | + // entire panel from scratch each time. |
| 68 | + // |
| 69 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 70 | + }, [state, url, box, conversationId]); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + if (state.type === "loaded") { |
| 74 | + state.panel.setHeight(height); |
| 75 | + } |
| 76 | + }, [state, height]); |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + if (state.type === "loaded") { |
| 80 | + if (show) { |
| 81 | + state.panel.show(); |
| 82 | + } else { |
| 83 | + state.panel.hide(); |
| 84 | + } |
| 85 | + } |
| 86 | + }, [state, show]); |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + {state.type === "loaded" && |
| 91 | + createPortal(children, state.panel.window.document.body)} |
| 92 | + </> |
| 93 | + ); |
| 94 | +} |
0 commit comments