-
-
Notifications
You must be signed in to change notification settings - Fork 312
feat: add subscription and org details to Chatwoot support widget #3277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,129 @@ | ||||||||||||||||||||||
| import { useEffect } from 'react'; | ||||||||||||||||||||||
| import { useTheme } from '@mui/material'; | ||||||||||||||||||||||
| import { | ||||||||||||||||||||||
| useConfig, | ||||||||||||||||||||||
| usePreferredOrganization, | ||||||||||||||||||||||
| useUser, | ||||||||||||||||||||||
| } from 'tg.globalContext/helpers'; | ||||||||||||||||||||||
| import { components } from 'tg.service/apiSchema.generated'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| type User = components['schemas']['PrivateUserAccountModel']; | ||||||||||||||||||||||
| type Organization = components['schemas']['PrivateOrganizationModel']; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const BASE_URL = 'https://app.chatwoot.com'; | ||||||||||||||||||||||
| let chatwootLoadPromise: Promise<void> | null = null; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function loadScript(doc: Document, url: string) { | ||||||||||||||||||||||
| return new Promise<void>((resolve) => { | ||||||||||||||||||||||
| const element = doc.createElement('script') as HTMLScriptElement; | ||||||||||||||||||||||
| const existingElement = doc.getElementsByTagName( | ||||||||||||||||||||||
| 'script' | ||||||||||||||||||||||
| )[0] as HTMLScriptElement; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| element.src = url; | ||||||||||||||||||||||
| element.defer = true; | ||||||||||||||||||||||
| element.async = true; | ||||||||||||||||||||||
| element.onload = () => { | ||||||||||||||||||||||
| resolve(); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existingElement?.parentNode?.insertBefore(element, existingElement); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| async function loadChatwoot(websiteToken: string, darkMode: boolean) { | ||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||
| window.chatwootSettings = { | ||||||||||||||||||||||
| darkMode: darkMode ? 'auto' : 'light', | ||||||||||||||||||||||
| hideMessageBubble: true, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await loadScript(document, BASE_URL + '/packs/js/sdk.js'); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||
| window.chatwootSDK?.run({ | ||||||||||||||||||||||
| websiteToken, | ||||||||||||||||||||||
| baseUrl: BASE_URL, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| async function loadChatwootOnce(websiteToken: string, darkMode: boolean) { | ||||||||||||||||||||||
| if (!chatwootLoadPromise) { | ||||||||||||||||||||||
| chatwootLoadPromise = loadChatwoot(websiteToken, darkMode); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await chatwootLoadPromise; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function setChatwootUser(user: User) { | ||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||
| window.$chatwoot?.setUser(user.id, { | ||||||||||||||||||||||
| email: user!.username, | ||||||||||||||||||||||
| name: user!.name, | ||||||||||||||||||||||
| url: window.location, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function setChatwootAttributes(organization: Organization) { | ||||||||||||||||||||||
| const subscription = organization.activeCloudSubscription; | ||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||
| window.$chatwoot?.setCustomAttributes({ | ||||||||||||||||||||||
| plan: subscription?.plan?.name || 'free', | ||||||||||||||||||||||
| subscriptionStatus: subscription?.status || 'inactive', | ||||||||||||||||||||||
| organizationId: organization.id, | ||||||||||||||||||||||
| organizationName: organization.name, | ||||||||||||||||||||||
| enabledFeatures: organization.enabledFeatures.join(', '), | ||||||||||||||||||||||
| currentUserRole: organization.currentUserRole, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function toggleChatwoot() { | ||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||
| window.$chatwoot?.toggle(); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export function useChatwoot() { | ||||||||||||||||||||||
| const user = useUser(); | ||||||||||||||||||||||
| const { preferredOrganization } = usePreferredOrganization(); | ||||||||||||||||||||||
| const config = useConfig(); | ||||||||||||||||||||||
| const token = config?.chatwootToken; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const enabledFeatures = preferredOrganization?.enabledFeatures; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const hasStandardSupport = | ||||||||||||||||||||||
| enabledFeatures?.includes('STANDARD_SUPPORT') || | ||||||||||||||||||||||
| enabledFeatures?.includes('PREMIUM_SUPPORT'); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const available = !!(token && user && hasStandardSupport); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const { | ||||||||||||||||||||||
| palette: { mode }, | ||||||||||||||||||||||
| } = useTheme(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const darkMode = mode === 'dark'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||
| if (token) { | ||||||||||||||||||||||
| loadChatwootOnce(token, darkMode); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }, [token]); | ||||||||||||||||||||||
|
Comment on lines
+105
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add darkMode to the dependency array. The Apply this diff: useEffect(() => {
if (token) {
loadChatwootOnce(token, darkMode);
}
- }, [token]);
+ }, [token, darkMode]);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const openChatwoot = async () => { | ||||||||||||||||||||||
| if (!available) { | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await loadChatwootOnce(token, darkMode); | ||||||||||||||||||||||
| setChatwootUser(user); | ||||||||||||||||||||||
| if (preferredOrganization) { | ||||||||||||||||||||||
| setChatwootAttributes(preferredOrganization); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| toggleChatwoot(); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return { | ||||||||||||||||||||||
| chatwootAvailable: available, | ||||||||||||||||||||||
| openChatwoot, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid @ts-ignore by declaring proper Window interface augmentation.
The
@ts-ignorecomments on lines 35 and 43 directly contradict the previous review feedback from JanCizmar who stated "I wouldn't use@ts-ignoreignore here." Instead, declare proper TypeScript types for the Chatwoot properties.Add a type declaration file or augment the Window interface at the top of this file:
Then remove all
@ts-ignorecomments throughout the file.🤖 Prompt for AI Agents