tawk-react
Version:
React wrapper for tawk.to live chat
166 lines (165 loc) • 5.24 kB
JavaScript
"use client";
import { useEffect as u } from "react";
async function f(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, n) => {
const r = document.createElement("script");
r.defer = !0, r.src = t, r.type = "text/javascript", r.crossOrigin = "*", r.onload = e, r.onerror = () => n(new Error(`Failed to load script from ${t}.`)), document.body.appendChild(r);
});
}
function h(t) {
typeof document > "u" || document.querySelectorAll(`script[src="${t}"]`).forEach((e) => e.remove());
}
var d = /* @__PURE__ */ ((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))(d || {});
const w = /* @__PURE__ */ new Map();
function g() {
if (typeof window > "u")
return () => {
};
const t = (e) => {
const n = e, r = n.type, a = n.detail, o = w.get(r) ?? [];
for (const s of o)
s(a);
};
for (const e of Object.values(d))
window.addEventListener(e, t);
return () => {
for (const e of Object.values(d))
window.removeEventListener(e, t);
};
}
function c(t, e) {
const n = w.get(t) ?? [];
return n.push(e), w.set(t, n), () => {
const a = (w.get(t) ?? []).filter((o) => o !== e);
w.set(t, a);
};
}
const m = ({
propertyId: t,
widgetId: e,
autoStart: n,
visitor: r,
customStyle: a
}) => (u(() => {
const o = `https://embed.tawk.to/${t}/${e}`;
window.Tawk_API = window.Tawk_API || {}, window.Tawk_API.autoStart = n, window.Tawk_API.visitor = r, window.Tawk_API.customStyle = a, window.Tawk_LoadStart = /* @__PURE__ */ new Date();
const s = g();
return f(o).catch(console.error), () => {
h(o), s();
};
}, [t, e]), null);
m.displayName = "TawkLiveChat";
function p(t, e) {
u(() => c(t, e), [t, e]);
}
function C() {
return {
start: (t) => {
i() && window.Tawk_API.start(t);
},
shutdown: () => {
i() && window.Tawk_API.shutdown();
},
switchWidget: (t, e) => {
if (!i())
return e(new Error("Tawk is not ready."));
window.Tawk_API.switchWidget(t, e);
},
login: (t, e) => {
if (!i())
return e(new Error("Tawk is not ready."));
window.Tawk_API.login(t, e);
},
logout: (t) => {
if (!i())
return t(new Error("Tawk is not ready."));
window.Tawk_API.logout(t);
},
maximize: () => {
i() && window.Tawk_API.maximize();
},
minimize: () => {
i() && window.Tawk_API.minimize();
},
toggle: () => {
i() && window.Tawk_API.toggle();
},
popup: () => {
i() && window.Tawk_API.popup();
},
getWindowType: () => {
if (i())
return window.Tawk_API.getWindowType();
},
showWidget: () => {
i() && window.Tawk_API.showWidget();
},
hideWidget: () => {
i() && window.Tawk_API.hideWidget();
},
toggleVisibility: () => {
i() && window.Tawk_API.toggleVisibility();
},
getStatus: () => {
if (i())
return window.Tawk_API.getStatus();
},
isChatMaximized: () => {
if (i())
return window.Tawk_API.isChatMaximized();
},
isChatMinimized: () => {
if (i())
return window.Tawk_API.isChatMinimized();
},
isChatHidden: () => {
if (i())
return window.Tawk_API.isChatHidden();
},
isChatOngoing: () => {
if (i())
return window.Tawk_API.isChatOngoing();
},
isVisitorEngaged: () => {
if (i())
return window.Tawk_API.isVisitorEngaged();
},
endChat: () => {
i() && window.Tawk_API.endChat();
},
setAttributes: (t, e) => {
if (!i())
return e(new Error("Tawk is not ready."));
window.Tawk_API.setAttributes(t, e);
},
addEvent: (t, e, n) => {
if (!i())
return n(new Error("Tawk is not ready."));
window.Tawk_API.addEvent(t, e, n);
},
addTags: (t, e) => {
if (!i())
return e(new Error("Tawk is not ready."));
window.Tawk_API.addTags(t, e);
},
removeTags: (t, e) => {
if (!i())
return e(new Error("Tawk is not ready."));
window.Tawk_API.removeTags(t, e);
}
};
}
function i() {
return typeof window > "u" ? !1 : typeof window.Tawk_API < "u";
}
export {
d as TawkEvent,
m as TawkLiveChat,
C as useTawkAction,
p as useTawkEvent
};
//# sourceMappingURL=main.es.js.map