Skip to content

Commit 85b23b5

Browse files
feat: add Widget component for Plug SDK integration in main application
1 parent 63e36ee commit 85b23b5

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use client";
2+
3+
import { useState, useEffect } from "react";
4+
5+
declare global {
6+
interface Window {
7+
plugSDK: any;
8+
}
9+
}
10+
11+
export const Widget = ({ id }: { id: string }) => {
12+
const [hasWidgetLoaded, setHasWidgetLoaded] = useState(false);
13+
14+
const runScript = () => {
15+
window.plugSDK.init({
16+
app_id: id,
17+
});
18+
window.plugSDK.onEvent((payload: any) => {
19+
switch (payload.type) {
20+
case "ON_PLUG_WIDGET_READY":
21+
window.plugSDK.initSearchAgent();
22+
document.addEventListener("keydown", function (event) {
23+
if ((event.metaKey || event.ctrlKey) && event.key === "k") {
24+
event.preventDefault();
25+
window.plugSDK.toggleSearchAgent();
26+
}
27+
});
28+
break;
29+
default:
30+
break;
31+
}
32+
});
33+
};
34+
35+
// React.StrictMode causes script.onload to be called twice hence putting a hack in place only instantiate plug widget once
36+
useEffect(() => {
37+
if (hasWidgetLoaded) {
38+
runScript();
39+
}
40+
}, [hasWidgetLoaded]);
41+
42+
return (
43+
<script
44+
onLoad={() => {
45+
setHasWidgetLoaded(true);
46+
}}
47+
type="text/javascript"
48+
src="https://plug-platform.devrev.ai/static/plug.js"
49+
/>
50+
);
51+
};

custom-implementation/src/main.tsx

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Header from './components/header'
99
import Footer from './components/footer'
1010

1111
import { Search } from './components/search'
12+
import { Widget } from './components/widget'
1213
import { ThemeSwitch } from './components/theme-switch'
1314

1415
import { getPageData } from './modules/sanity/utils'
@@ -101,40 +102,14 @@ const render = async () => {
101102
},
102103
)
103104

104-
// Add Plug component directly to body
105-
if (!document.getElementById('plug-platform')) {
106-
const plugScript = document.createElement('script')
107-
plugScript.setAttribute('type', 'text/javascript')
108-
plugScript.setAttribute('id', 'plug-platform')
109-
plugScript.setAttribute('src', 'https://plug-platform.devrev.ai/static/plug.js')
110-
document.body.appendChild(plugScript)
111-
112-
// Initialize Plug SDK after script loads
113-
plugScript.onload = () => {
114-
if ((window as any).plugSDK) {
115-
(window as any).plugSDK?.init?.({
116-
app_id: data?.plug?.id,
117-
enable_session_recording: true,
118-
})
119-
(window as any).plugSDK.onEvent((payload: any) => {
120-
switch (payload.type) {
121-
case "ON_PLUG_WIDGET_READY":
122-
console.log("ON_PLUG_WIDGET_READY");
123-
(window as any).plugSDK.initSearchAgent();
124-
document.addEventListener("keydown", function (event) {
125-
if (event.key === "/") {
126-
console.log("KEYDOWN");
127-
event.preventDefault();
128-
(window as any).plugSDK.toggleSearchAgent();
129-
}
130-
});
131-
break;
132-
default:
133-
break;
134-
}
135-
});
136-
}
137-
}
105+
// Add Widget component to body
106+
if (!document.getElementById('widget-container')) {
107+
const widgetContainer = document.createElement('div')
108+
widgetContainer.setAttribute('id', 'widget-container')
109+
document.body.appendChild(widgetContainer)
110+
ReactDOM.render(React.createElement(Widget, {
111+
...data.plug
112+
}), widgetContainer)
138113
}
139114
}
140115

0 commit comments

Comments
 (0)