UNPKG

@prexo/ai-chat-sdk

Version:

AI Chat Component with Persistent History

47 lines (46 loc) 1.48 kB
import { PrexoAiChatBot } from "./components/custom/chat.widget.js"; if (typeof window !== "undefined") { let chatInstance = null; let mountElement = null; window.PrexoAiChatBot = { init: (config) => { import("react").then((React) => { import("react-dom/client").then((ReactDOM) => { import("./components/custom/chat.widget").then(({ PrexoAiChatBot: PrexoAiChatBot2 }) => { const { mountId = "prexo-ai-chat-sdk-root", ...chatProps } = config; mountElement = document.getElementById(mountId); if (!mountElement) { mountElement = document.createElement("div"); mountElement.id = mountId; document.body.appendChild(mountElement); } const root = ReactDOM.createRoot(mountElement); chatInstance = root; root.render( React.createElement(PrexoAiChatBot2, { ...chatProps, onClose: () => { config.onClose?.(); window.PrexoAiChatBot.destroy(); } }) ); }); }); }); }, destroy: () => { if (chatInstance && mountElement) { chatInstance.unmount(); if (mountElement.parentNode) { mountElement.parentNode.removeChild(mountElement); } chatInstance = null; mountElement = null; } } }; } export { PrexoAiChatBot };