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