2023-12-09 15:47:03 +01:00
|
|
|
export const useCrisp = () => {
|
2024-03-28 17:59:41 +01:00
|
|
|
let crisp = import.meta.client ? window.Crisp : null
|
2023-12-09 15:47:03 +01:00
|
|
|
|
|
|
|
|
function openChat() {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
2023-12-20 16:10:32 +01:00
|
|
|
showChat()
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp.chat.open()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showChat() {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
|
|
|
|
crisp.chat.show()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hideChat() {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
|
|
|
|
crisp.chat.hide()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeChat() {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
|
|
|
|
crisp.chat.close()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openAndShowChat(message = null) {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
2023-12-09 15:47:03 +01:00
|
|
|
openChat()
|
|
|
|
|
if (message) sendTextMessage(message)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 16:10:32 +01:00
|
|
|
function openHelpdesk() {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
|
|
|
|
openChat()
|
|
|
|
|
crisp.chat.setHelpdeskView()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
2023-12-20 16:10:32 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
function openHelpdeskArticle(articleSlug, locale = "en") {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
2024-04-15 19:39:03 +02:00
|
|
|
crisp.chat.openHelpdeskArticle(locale, articleSlug)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendTextMessage(message) {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
2024-04-15 19:39:03 +02:00
|
|
|
crisp.message.send("text", message)
|
2023-12-09 16:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-20 16:10:32 +01:00
|
|
|
function setUser(user) {
|
2023-12-09 16:33:56 +01:00
|
|
|
if (!crisp) return
|
2024-04-15 19:39:03 +02:00
|
|
|
crisp.user.setEmail(user.email)
|
|
|
|
|
crisp.user.setNickname(user.name)
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp.session.setData({
|
2023-12-20 16:10:32 +01:00
|
|
|
user_id: user.id,
|
2024-04-15 19:39:03 +02:00
|
|
|
"pro-subscription": user?.is_subscribed ?? false,
|
|
|
|
|
"stripe-id": user?.stripe_id ?? "",
|
|
|
|
|
subscription: user?.has_enterprise_subscription ? "enterprise" : "pro",
|
|
|
|
|
})
|
2023-12-09 16:33:56 +01:00
|
|
|
|
|
|
|
|
if (user?.is_subscribed ?? false) {
|
2024-04-15 19:39:03 +02:00
|
|
|
setSegments([
|
|
|
|
|
"subscribed",
|
|
|
|
|
user?.has_enterprise_subscription ? "enterprise" : "pro",
|
|
|
|
|
])
|
2023-12-09 16:33:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 16:10:32 +01:00
|
|
|
function pushEvent(event, data = {}) {
|
|
|
|
|
if (!crisp) return
|
2024-01-29 10:25:00 +01:00
|
|
|
crisp.session.pushEvent(event, data)
|
2023-12-20 16:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:33:56 +01:00
|
|
|
function setSegments(segments, overwrite = false) {
|
|
|
|
|
if (!crisp) return
|
|
|
|
|
crisp.session.setSegments(segments, overwrite)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp,
|
2023-12-09 15:47:03 +01:00
|
|
|
openChat,
|
|
|
|
|
showChat,
|
|
|
|
|
hideChat,
|
|
|
|
|
closeChat,
|
|
|
|
|
openAndShowChat,
|
|
|
|
|
openHelpdesk,
|
|
|
|
|
openHelpdeskArticle,
|
2023-12-09 16:33:56 +01:00
|
|
|
sendTextMessage,
|
2023-12-20 16:10:32 +01:00
|
|
|
pushEvent,
|
2024-04-15 19:39:03 +02:00
|
|
|
setUser,
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
}
|