dograh/ui/src/components/ChatwootWidget.tsx
Abhishek d97d1d72cd
feat: add chat based testing for voice agent (#308)
* feat: add backend foundations

* feat: add text chat UI

* chore: simplify the reload behaviour

* fix: fix upgrade banner to be triggered after package upload

* feat: simplify TesterPanel design

* chore: fix formatting and generate client

* chore: fix tracing for text chat mode

* fix: fix revert and edit CTA

* refactor: refactor TesterPanel into smaller components

* feat: enable runtime transition of nodes

* fix: fix review comments
2026-05-21 15:20:02 +05:30

94 lines
2.5 KiB
TypeScript

"use client";
import { usePathname } from "next/navigation";
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() {
const pathname = usePathname();
useEffect(() => {
const isWorkflowPage = /^\/workflow\/[^/]+(?:\/.*)?$/.test(pathname);
if (isWorkflowPage) {
document.getElementById("cw-widget-holder")?.remove();
document.getElementById("cw-bubble-holder")?.remove();
document.getElementById("cw-widget-styles")?.remove();
document
.querySelector(`script[src="${CHATWOOT_BASE_URL}/packs/js/sdk.js"]`)
?.remove();
delete window.chatwootSettings;
return;
}
// 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);
}, [pathname]);
return null;
}