2023-03-27 20:20:36 +00:00
|
|
|
import { useCallback, useEffect } from "react";
|
2023-10-20 11:40:17 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
// hooks
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-03-27 20:20:36 +00:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
$crisp: any;
|
|
|
|
CRISP_WEBSITE_ID: any;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 11:40:17 +00:00
|
|
|
const Crisp = observer(() => {
|
|
|
|
const { user: userStore } = useMobxStore();
|
|
|
|
const { currentUser } = userStore;
|
2023-03-27 20:20:36 +00:00
|
|
|
|
|
|
|
const validateCurrentUser = useCallback(() => {
|
2023-10-20 11:40:17 +00:00
|
|
|
if (currentUser) return currentUser.email;
|
2023-03-27 20:20:36 +00:00
|
|
|
return null;
|
2023-10-20 11:40:17 +00:00
|
|
|
}, [currentUser]);
|
2023-03-27 20:20:36 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (typeof window && validateCurrentUser()) {
|
|
|
|
window.$crisp = [];
|
|
|
|
window.CRISP_WEBSITE_ID = process.env.NEXT_PUBLIC_CRISP_ID;
|
|
|
|
(function () {
|
|
|
|
var d = document;
|
|
|
|
var s = d.createElement("script");
|
|
|
|
s.src = "https://client.crisp.chat/l.js";
|
|
|
|
s.async = true;
|
|
|
|
d.getElementsByTagName("head")[0].appendChild(s);
|
|
|
|
// defining email when logged in
|
|
|
|
if (validateCurrentUser()) {
|
|
|
|
window.$crisp.push(["set", "user:email", [validateCurrentUser()]]);
|
|
|
|
window.$crisp.push(["do", "chat:hide"]);
|
|
|
|
window.$crisp.push(["do", "chat:close"]);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}, [validateCurrentUser]);
|
|
|
|
|
|
|
|
return <></>;
|
2023-10-20 11:40:17 +00:00
|
|
|
});
|
2023-03-27 20:20:36 +00:00
|
|
|
export default Crisp;
|