|
| 1 | +import { useEffect, useRef } from "react"; |
| 2 | +import { isSsr } from "../../../utilites/helpers.ts"; |
| 3 | +import { useGetMe } from "../../../queries/useGetMe.ts"; |
| 4 | +import { User } from "../../../types.ts"; |
| 5 | +import { getConfig } from "../../../utilites/config.ts"; |
| 6 | + |
| 7 | +const CHATWOOT_ROUTES = ["/manage", "/account", "/welcome"]; |
| 8 | + |
| 9 | +const ChatwootWidget = () => { |
| 10 | + const scriptRef = useRef<HTMLScriptElement | null>(null); |
| 11 | + const { data: me, isLoading } = useGetMe(); |
| 12 | + const chatwootToken = getConfig('VITE_CHATWOOT_WEBSITE_TOKEN'); |
| 13 | + const chatwootUrl = getConfig('VITE_CHATWOOT_BASE_URL') || 'https://app.chatwoot.com'; |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + if (isSsr() || isLoading || !chatwootToken) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + const pathname = window.location.pathname; |
| 22 | + if (!CHATWOOT_ROUTES.some(route => pathname.includes(route))) return; |
| 23 | + |
| 24 | + if (!document.getElementById("chatwoot-script")) { |
| 25 | + const script = document.createElement("script"); |
| 26 | + script.id = "chatwoot-script"; |
| 27 | + script.src = `${chatwootUrl}/packs/js/sdk.js`; |
| 28 | + script.defer = true; |
| 29 | + script.async = true; |
| 30 | + script.onload = () => { |
| 31 | + window.chatwootSDK.run({ |
| 32 | + websiteToken: chatwootToken, |
| 33 | + baseUrl: chatwootUrl |
| 34 | + }); |
| 35 | + if (me) { |
| 36 | + setChatwootUserDetails(me); |
| 37 | + } |
| 38 | + }; |
| 39 | + document.body.appendChild(script); |
| 40 | + scriptRef.current = script; |
| 41 | + } else if (me) { |
| 42 | + setChatwootUserDetails(me); |
| 43 | + } |
| 44 | + |
| 45 | + const handleUrlChange = () => { |
| 46 | + try { |
| 47 | + const currentPath = window.location.pathname; |
| 48 | + if (!CHATWOOT_ROUTES.some(route => currentPath.includes(route))) { |
| 49 | + removeChatwootScript(); |
| 50 | + } |
| 51 | + } catch (error) { |
| 52 | + console.error("Error in ChatwootWidget URL change handler:", error); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + window.addEventListener("popstate", handleUrlChange); |
| 57 | + |
| 58 | + return () => { |
| 59 | + window.removeEventListener("popstate", handleUrlChange); |
| 60 | + removeChatwootScript(); |
| 61 | + }; |
| 62 | + } catch (error) { |
| 63 | + console.error("Error initializing ChatwootWidget:", error); |
| 64 | + } |
| 65 | + }, [isLoading, me]); |
| 66 | + |
| 67 | + const removeChatwootScript = () => { |
| 68 | + try { |
| 69 | + const script = document.getElementById("chatwoot-script"); |
| 70 | + if (script) { |
| 71 | + script.remove(); |
| 72 | + scriptRef.current = null; |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + console.error("Error removing ChatwootWidget script:", error); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const setChatwootUserDetails = (user: User) => { |
| 80 | + try { |
| 81 | + window.addEventListener("chatwoot:ready", () => { |
| 82 | + if (!window.$chatwoot) return; |
| 83 | + |
| 84 | + window.$chatwoot.setUser(String(user.id), { |
| 85 | + email: user.email, |
| 86 | + name: user.full_name, |
| 87 | + locale: user.locale || "en", |
| 88 | + }); |
| 89 | + |
| 90 | + window.$chatwoot.setCustomAttributes({ |
| 91 | + locale: user.locale || "en", |
| 92 | + account_id: user.account_id || "Unknown", |
| 93 | + user_role: user.role || "Unknown", |
| 94 | + is_user_verified: user.is_email_verified, |
| 95 | + }); |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + console.error("Error setting Chatwoot user details:", error); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + return null; |
| 103 | +}; |
| 104 | + |
| 105 | +export default ChatwootWidget; |
0 commit comments