mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-22 08:38:13 +02:00
feat: add chatwoot integration (#39)
This commit is contained in:
parent
d58f37ff42
commit
5c1fe2c6af
3 changed files with 82 additions and 0 deletions
78
ui/src/components/ChatwootWidget.tsx
Normal file
78
ui/src/components/ChatwootWidget.tsx
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
chatwootSDK?: {
|
||||
run: (config: {
|
||||
websiteToken: string;
|
||||
baseUrl: string;
|
||||
}) => void;
|
||||
};
|
||||
chatwootSettings?: {
|
||||
position?: "left" | "right";
|
||||
type?: "standard" | "expanded_bubble";
|
||||
launcherTitle?: string;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const CHATWOOT_BASE_URL = process.env.NEXT_PUBLIC_CHATWOOT_URL;
|
||||
const CHATWOOT_WEBSITE_TOKEN = process.env.NEXT_PUBLIC_CHATWOOT_TOKEN;
|
||||
|
||||
export default function ChatwootWidget() {
|
||||
useEffect(() => {
|
||||
// Don't initialize if environment variables are not set
|
||||
if (!CHATWOOT_BASE_URL || !CHATWOOT_WEBSITE_TOKEN) {
|
||||
console.warn("Chatwoot not configured: Missing NEXT_PUBLIC_CHATWOOT_URL or NEXT_PUBLIC_CHATWOOT_TOKEN");
|
||||
return;
|
||||
}
|
||||
|
||||
// Prevent duplicate initialization
|
||||
if (window.chatwootSettings) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Configure Chatwoot widget settings
|
||||
window.chatwootSettings = {
|
||||
position: "right",
|
||||
type: "standard",
|
||||
launcherTitle: "Chat with us",
|
||||
};
|
||||
|
||||
// Check if script is already loaded
|
||||
const existingScript = document.querySelector(
|
||||
`script[src="${CHATWOOT_BASE_URL}/packs/js/sdk.js"]`
|
||||
);
|
||||
|
||||
if (existingScript) {
|
||||
// Script already exists, just initialize if SDK is available
|
||||
if (window.chatwootSDK) {
|
||||
window.chatwootSDK.run({
|
||||
websiteToken: CHATWOOT_WEBSITE_TOKEN,
|
||||
baseUrl: CHATWOOT_BASE_URL,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Create and inject the Chatwoot SDK script
|
||||
const script = document.createElement("script");
|
||||
script.src = `${CHATWOOT_BASE_URL}/packs/js/sdk.js`;
|
||||
script.async = true;
|
||||
script.defer = true;
|
||||
script.onload = () => {
|
||||
if (window.chatwootSDK) {
|
||||
window.chatwootSDK.run({
|
||||
websiteToken: CHATWOOT_WEBSITE_TOKEN,
|
||||
baseUrl: CHATWOOT_BASE_URL,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
document.body.appendChild(script);
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue