Skip to content

Commit ffef6b3

Browse files
committed
fix: use React useEffect for widget initialization
1 parent 19b632d commit ffef6b3

File tree

1 file changed

+58
-50
lines changed

1 file changed

+58
-50
lines changed

snippets/widget.mdx

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
1-
<div dangerouslySetInnerHTML={{__html: `
2-
<script>
3-
(function() {
4-
if (typeof window === 'undefined') return;
5-
if (window.__knowrithmWidgetLoaded) return;
6-
window.__knowrithmWidgetLoaded = true;
1+
export const KnowrithmWidget = () => {
2+
if (typeof window !== 'undefined') {
3+
// Only run on client side
4+
React.useEffect(() => {
5+
// Prevent duplicate loads
6+
if (window.__knowrithmWidgetLoaded) return;
7+
window.__knowrithmWidgetLoaded = true;
78

8-
window.AIChatWidget = {
9-
"agentId": "2b041b45-a585-47e9-abaf-ebaad766bce9",
10-
"apiUrl": "https://app.knowrithm.org/api/v1",
11-
"color": "#0972A3",
12-
"position": "right",
13-
"appearance": {
14-
"theme": "light",
15-
"primaryColor": "#0972A3",
16-
"placement": "bottom-right",
17-
"buttonText": "Chat with us",
18-
"showBranding": true
19-
},
20-
"behavior": {
21-
"autoOpen": false,
22-
"initialDelay": 0,
23-
"persistSession": true,
24-
"pollingInterval": 10000,
25-
"enableSounds": false
26-
},
27-
"callbacks": {
28-
"onLoad": null,
29-
"onError": null,
30-
"onMessage": null,
31-
"onClose": null
32-
},
33-
"assets": {
34-
"scriptUrl": "https://app.knowrithm.org/api/widget.js",
35-
"styleUrl": "https://minio.knowrithm.org/browser/knowrithm-bucket/chat-widget.css"
36-
}
37-
};
38-
39-
const script = document.createElement('script');
40-
script.src = 'https://app.knowrithm.org/api/widget.js';
41-
script.type = 'text/javascript';
42-
script.async = true;
43-
document.head.appendChild(script);
44-
45-
const link = document.createElement('link');
46-
link.rel = 'stylesheet';
47-
link.href = 'https://minio.knowrithm.org/browser/knowrithm-bucket/chat-widget.css';
48-
document.head.appendChild(link);
49-
})();
50-
</script>
51-
`}} />
9+
// Set widget configuration
10+
window.AIChatWidget = {
11+
agentId: "2b041b45-a585-47e9-abaf-ebaad766bce9",
12+
apiUrl: "https://app.knowrithm.org/api/v1",
13+
color: "#0972A3",
14+
position: "right",
15+
appearance: {
16+
theme: "light",
17+
primaryColor: "#0972A3",
18+
placement: "bottom-right",
19+
buttonText: "Chat with us",
20+
showBranding: true
21+
},
22+
behavior: {
23+
autoOpen: false,
24+
initialDelay: 0,
25+
persistSession: true,
26+
pollingInterval: 10000,
27+
enableSounds: false
28+
},
29+
callbacks: {
30+
onLoad: null,
31+
onError: null,
32+
onMessage: null,
33+
onClose: null
34+
},
35+
assets: {
36+
scriptUrl: "https://app.knowrithm.org/api/widget.js",
37+
styleUrl: "https://minio.knowrithm.org/browser/knowrithm-bucket/chat-widget.css"
38+
}
39+
};
40+
41+
// Load the widget script
42+
const script = document.createElement('script');
43+
script.src = 'https://app.knowrithm.org/api/widget.js';
44+
script.type = 'text/javascript';
45+
script.async = true;
46+
document.head.appendChild(script);
47+
48+
// Load the widget CSS
49+
const link = document.createElement('link');
50+
link.rel = 'stylesheet';
51+
link.href = 'https://minio.knowrithm.org/browser/knowrithm-bucket/chat-widget.css';
52+
document.head.appendChild(link);
53+
}, []);
54+
}
55+
56+
return null;
57+
};
58+
59+
<KnowrithmWidget />

0 commit comments

Comments
 (0)