Skip to content

Commit 2df6ba2

Browse files
authored
Merge pull request #430 from HiEventsDev/develop
main <- develop
2 parents c966545 + c5b0b55 commit 2df6ba2

File tree

5 files changed

+111
-72
lines changed

5 files changed

+111
-72
lines changed

backend/app/Resources/User/UserResource.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function toArray(Request $request): array
1818
'timezone' => $this->getTimezone(),
1919
'first_name' => $this->getFirstName(),
2020
'last_name' => $this->getLastName(),
21+
'full_name' => $this->getFullName(),
2122
'email' => $this->getEmail(),
2223
'is_email_verified' => $this->getEmailVerifiedAt() !== null,
2324
'has_pending_email_change' => $this->getPendingEmail() !== null,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import ZohoChatWidget from "../ZohoChatWidget";
1+
import ChatwootWidget from "../ChatwootWidget";
22

33
export const ThirdPartyScripts = () => {
44
return (
55
<>
6-
<ZohoChatWidget/>
6+
<ChatwootWidget/>
77
</>
88
);
99
}

frontend/src/components/common/ZohoChatWidget/index.tsx

-70
This file was deleted.

frontend/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export type ConfigKeys = 'VITE_FRONTEND_URL'
77
| 'VITE_API_URL_CLIENT'
88
| 'VITE_STRIPE_PUBLISHABLE_KEY'
99
| 'VITE_API_URL_SERVER'
10+
| 'VITE_CHATWOOT_WEBSITE_TOKEN'
11+
| 'VITE_CHATWOOT_BASE_URL'
1012
| string;
1113

1214
export type IdParam = string | undefined | number;
@@ -41,6 +43,7 @@ export interface User {
4143
account_id?: IdParam;
4244
first_name: string;
4345
last_name: string;
46+
full_name: string;
4447
email: string;
4548
timezone?: string;
4649
password?: string;

0 commit comments

Comments
 (0)