2024-05-06 14:19:06 +02:00
|
|
|
export function useCrisp () {
|
|
|
|
|
const crisp = import.meta.client ? window.Crisp : null
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function onCrispInit () {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
|
|
|
|
crisp.chat.onChatOpened(() => {
|
|
|
|
|
useAppStore().crisp.chatOpened = true
|
|
|
|
|
})
|
|
|
|
|
crisp.chat.onChatClosed(() => {
|
|
|
|
|
useAppStore().crisp.chatOpened = false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openChat () {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function showChat () {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp.chat.show()
|
2024-05-06 14:19:06 +02:00
|
|
|
useAppStore().crisp.hidden = false
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function hideChat () {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp.chat.hide()
|
2024-05-06 14:19:06 +02:00
|
|
|
useAppStore().crisp.hidden = true
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function closeChat () {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp.chat.close()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function openAndShowChat (message = null) {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2023-12-09 15:47:03 +01:00
|
|
|
openChat()
|
2024-05-06 14:19:06 +02:00
|
|
|
if (message)
|
|
|
|
|
sendTextMessage(message)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function openHelpdesk () {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2023-12-09 16:33:56 +01:00
|
|
|
openChat()
|
|
|
|
|
crisp.chat.setHelpdeskView()
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
2023-12-20 16:10:32 +01:00
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function openHelpdeskArticle (articleSlug, locale = 'en') {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2024-04-15 19:39:03 +02:00
|
|
|
crisp.chat.openHelpdeskArticle(locale, articleSlug)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function sendTextMessage (message) {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
|
|
|
|
crisp.message.send('text', message)
|
2023-12-09 16:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function setUser (user) {
|
|
|
|
|
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({
|
2024-05-06 14:19:06 +02:00
|
|
|
'user_id': user.id,
|
|
|
|
|
'pro-subscription': user?.is_subscribed ?? false,
|
|
|
|
|
'stripe-id': user?.stripe_id ?? '',
|
|
|
|
|
'subscription': user?.has_enterprise_subscription ? 'enterprise' : 'pro'
|
2024-04-15 19:39:03 +02:00
|
|
|
})
|
2023-12-09 16:33:56 +01:00
|
|
|
|
|
|
|
|
if (user?.is_subscribed ?? false) {
|
2024-04-15 19:39:03 +02:00
|
|
|
setSegments([
|
2024-05-06 14:19:06 +02:00
|
|
|
'subscribed',
|
|
|
|
|
user?.has_enterprise_subscription ? 'enterprise' : 'pro'
|
2024-04-15 19:39:03 +02:00
|
|
|
])
|
2023-12-09 16:33:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02: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
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:19:06 +02:00
|
|
|
function setSegments (segments, overwrite = false) {
|
|
|
|
|
if (!crisp)
|
|
|
|
|
return
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp.session.setSegments(segments, overwrite)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2023-12-09 16:33:56 +01:00
|
|
|
crisp,
|
2024-05-06 14:19:06 +02:00
|
|
|
onCrispInit,
|
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-05-06 14:19:06 +02:00
|
|
|
setSegments,
|
|
|
|
|
setUser
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
}
|