tawk-react
Version:
React wrapper for tawk.to live chat
3 lines (2 loc) • 4.13 kB
JavaScript
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("react");async function h(t){if(typeof document>"u")throw new Error("Script loading is not supported in this environment.");if(document.querySelector(`script[src="${t}"]`))throw new Error(`An element with the source: ${t} already exists in the document.`);return new Promise((e,i)=>{const r=document.createElement("script");r.defer=!0,r.src=t,r.type="text/javascript",r.crossOrigin="*",r.onload=e,r.onerror=()=>i(new Error(`Failed to load script from ${t}.`)),document.body.appendChild(r)})}function g(t){typeof document>"u"||document.querySelectorAll(`script[src="${t}"]`).forEach(e=>e.remove())}var s=(t=>(t.onLoad="tawkLoad",t.onStatusChange="tawkStatusChange",t.onBeforeLoad="tawkBeforeLoad",t.onChatMaximized="tawkChatMaximized",t.onChatMinimized="tawkChatMinimized",t.onChatHidden="tawkChatHidden",t.onChatStarted="tawkChatStarted",t.onChatEnded="tawkChatEnded",t.onPrechatSubmit="tawkPrechatSubmit",t.onOfflineSubmit="tawkOfflineSubmit",t.onChatMessageVisitor="tawkChatMessageVisitor",t.onChatMessageAgent="tawkChatMessageAgent",t.onChatMessageSystem="tawkChatMessageSystem",t.onAgentJoinChat="tawkAgentJoinChat",t.onAgentLeaveChat="tawkAgentLeaveChat",t.onChatSatisfaction="tawkChatSatisfaction",t.onVisitorNameChanged="tawkVisitorNameChanged",t.onFileUpload="tawkFileUpload",t.onTagsUpdated="tawkTagsUpdated",t))(s||{});const w=new Map;function c(){if(typeof window>"u")return()=>{};const t=e=>{const i=e,r=i.type,a=i.detail,o=w.get(r)??[];for(const d of o)d(a)};for(const e of Object.values(s))window.addEventListener(e,t);return()=>{for(const e of Object.values(s))window.removeEventListener(e,t)}}function m(t,e){const i=w.get(t)??[];return i.push(e),w.set(t,i),()=>{const a=(w.get(t)??[]).filter(o=>o!==e);w.set(t,a)}}const f=({propertyId:t,widgetId:e,autoStart:i,visitor:r,customStyle:a})=>(u.useEffect(()=>{const o=`https://embed.tawk.to/${t}/${e}`;window.Tawk_API=window.Tawk_API||{},window.Tawk_API.autoStart=i,window.Tawk_API.visitor=r,window.Tawk_API.customStyle=a,window.Tawk_LoadStart=new Date;const d=c();return h(o).catch(console.error),()=>{g(o),d()}},[t,e]),null);f.displayName="TawkLiveChat";function A(t,e){u.useEffect(()=>m(t,e),[t,e])}function k(){return{start:t=>{n()&&window.Tawk_API.start(t)},shutdown:()=>{n()&&window.Tawk_API.shutdown()},switchWidget:(t,e)=>{if(!n())return e(new Error("Tawk is not ready."));window.Tawk_API.switchWidget(t,e)},login:(t,e)=>{if(!n())return e(new Error("Tawk is not ready."));window.Tawk_API.login(t,e)},logout:t=>{if(!n())return t(new Error("Tawk is not ready."));window.Tawk_API.logout(t)},maximize:()=>{n()&&window.Tawk_API.maximize()},minimize:()=>{n()&&window.Tawk_API.minimize()},toggle:()=>{n()&&window.Tawk_API.toggle()},popup:()=>{n()&&window.Tawk_API.popup()},getWindowType:()=>{if(n())return window.Tawk_API.getWindowType()},showWidget:()=>{n()&&window.Tawk_API.showWidget()},hideWidget:()=>{n()&&window.Tawk_API.hideWidget()},toggleVisibility:()=>{n()&&window.Tawk_API.toggleVisibility()},getStatus:()=>{if(n())return window.Tawk_API.getStatus()},isChatMaximized:()=>{if(n())return window.Tawk_API.isChatMaximized()},isChatMinimized:()=>{if(n())return window.Tawk_API.isChatMinimized()},isChatHidden:()=>{if(n())return window.Tawk_API.isChatHidden()},isChatOngoing:()=>{if(n())return window.Tawk_API.isChatOngoing()},isVisitorEngaged:()=>{if(n())return window.Tawk_API.isVisitorEngaged()},endChat:()=>{n()&&window.Tawk_API.endChat()},setAttributes:(t,e)=>{if(!n())return e(new Error("Tawk is not ready."));window.Tawk_API.setAttributes(t,e)},addEvent:(t,e,i)=>{if(!n())return i(new Error("Tawk is not ready."));window.Tawk_API.addEvent(t,e,i)},addTags:(t,e)=>{if(!n())return e(new Error("Tawk is not ready."));window.Tawk_API.addTags(t,e)},removeTags:(t,e)=>{if(!n())return e(new Error("Tawk is not ready."));window.Tawk_API.removeTags(t,e)}}}function n(){return typeof window>"u"?!1:typeof window.Tawk_API<"u"}exports.TawkEvent=s;exports.TawkLiveChat=f;exports.useTawkAction=k;exports.useTawkEvent=A;
//# sourceMappingURL=main.cjs.js.map