opnform/client/composables/useCrisp.js

89 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-12-09 14:47:03 +00:00
export const useCrisp = () => {
let crisp = process.client ? window.Crisp : null
2023-12-09 14:47:03 +00:00
function openChat() {
if (!crisp) return
2023-12-20 15:10:32 +00:00
showChat()
crisp.chat.open()
2023-12-09 14:47:03 +00:00
}
function showChat() {
if (!crisp) return
crisp.chat.show()
2023-12-09 14:47:03 +00:00
}
function hideChat() {
if (!crisp) return
crisp.chat.hide()
2023-12-09 14:47:03 +00:00
}
function closeChat() {
if (!crisp) return
crisp.chat.close()
2023-12-09 14:47:03 +00:00
}
function openAndShowChat(message = null) {
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() {
if (!crisp) return
openChat()
crisp.chat.setHelpdeskView()
2023-12-09 14:47:03 +00:00
}
2023-12-20 15:10:32 +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) {
if (!crisp) return
crisp.message.send('text', message)
}
2023-12-20 15:10:32 +00:00
function setUser(user) {
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'
});
if (user?.is_subscribed ?? false) {
2024-01-13 17:17:24 +00:00
setSegments(['subscribed', user?.has_enterprise_subscription ? 'enterprise' : 'pro'])
}
}
2023-12-20 15:10:32 +00:00
function pushEvent(event, data = {}) {
if (!crisp) return
crisp.session.pushEvent(event, data)
2023-12-20 15:10:32 +00:00
}
function setSegments(segments, overwrite = false) {
if (!crisp) return
crisp.session.setSegments(segments, overwrite)
2023-12-09 14:47:03 +00:00
}
return {
crisp,
2023-12-09 14:47:03 +00:00
openChat,
showChat,
hideChat,
closeChat,
openAndShowChat,
openHelpdesk,
openHelpdeskArticle,
sendTextMessage,
2023-12-20 15:10:32 +00:00
pushEvent,
setUser
2023-12-09 14:47:03 +00:00
}
}