Skip to content

Commit 9d8db91

Browse files
add: plug script (#224)
* add: plug script * feat: integrate Plug SDK into main application * Remove Plug component implementation from the codebase. * Remove unused Plug component import from main application file. * chore: add @types/node to devDependencies in package.json * fix: update Plug SDK initialization to use dynamic app ID from fetched data - Changed the app ID assignment in the Plug SDK initialization to retrieve the ID from the fetched data instead of using an environment variable. - Updated the `getPageData` function to include the `plug` object containing the ID fetched from the Sanity database. * feat: add search component and integrate into main application - Introduced a new `Search` component to facilitate search functionality. - Integrated the `Search` component into the main application, replacing the existing search button with the new component. - Ensured the search button is rendered only if certain conditions are met, enhancing the application's dynamic behavior. * refactor: comment out theme switch and search component initialization - Commented out the initialization code for the `ThemeSwitch` and `search-component-wrapper` to streamline the rendering process. - This change prepares the code for potential future enhancements without removing the existing structure. * refactor: streamline search component integration - Removed commented-out code for `theme-switch` initialization and adjusted the rendering logic for the `Search` component. - The `Search` component is now directly appended to the `searchWrapper`, enhancing the structure and clarity of the rendering process. * refactor: remove unused sidenav code from main application - Eliminated the unused sidenav code related to the search bar to enhance code clarity and maintainability. - This change contributes to a cleaner rendering process in the main application. * refactor: update search component integration in main application - Replaced the search button with the search component by targeting the parent element of the button. - Enhanced the rendering logic to ensure the search component is integrated correctly within the sidebar. - This change improves the clarity and maintainability of the rendering process. * refactor: comment out search component rendering logic - Commented out the rendering logic for the `Search` component and `ThemeSwitch` to streamline the integration process. - This change maintains the existing structure while preparing for potential future enhancements. * refactor: comment out theme switch and search component imports * refactor: integrate theme switch and search components into main application * refactor: integrate search component rendering into main application * feat: implement event handling for Plug SDK in main application * refactor: update key event handling for search agent activation * refactor: comment out search component rendering logic * refactor: restore search component rendering logic in main application * refactor: reorder imports for theme switch and search components in main application * refactor: enhance event logging for Plug SDK integration in main application * feat: add Widget component for Plug SDK integration in main application * feat: replace Widget component with Plug SDK integration in main application * refactor: restructure sidenav to include header wrapper for theme switch and search components in main application * refactor: improve event handling for Plug SDK widget readiness in main application * refactor: update sidenav structure and search button styling in main application * refactor: adjust rendering order of theme switch and search components in main application * refactor: remove PLUG_APP_ID environment variable from workflow files * refactor: remove @types/node dependency from custom implementation package * refactor: remove Widget component from custom implementation package
1 parent 5cc525e commit 9d8db91

File tree

7 files changed

+102
-4
lines changed

7 files changed

+102
-4
lines changed

.github/workflows/fern-check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
registry-url: https://npm.pkg.github.com/
2121
env:
2222
NODE_AUTH_TOKEN: ${{secrets.AUTH_TOKEN}}
23+
2324

2425
- name: Build Navigation
2526
run: |

.github/workflows/preview-docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
registry-url: https://npm.pkg.github.com/
2323
env:
2424
NODE_AUTH_TOKEN: ${{secrets.AUTH_TOKEN}}
25+
2526

2627
- name: Install Fern
2728
run: npm install -g fern-api

.github/workflows/publish-docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
registry-url: https://npm.pkg.github.com/
2222
env:
2323
NODE_AUTH_TOKEN: ${{secrets.AUTH_TOKEN}}
24+
2425

2526
- name: Download Fern
2627
run: npm install -g fern-api

.github/workflows/publish-typescript.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
registry-url: https://npm.pkg.github.com/
2929
env:
3030
NODE_AUTH_TOKEN: ${{secrets.AUTH_TOKEN}}
31+
3132

3233
- name: Build Navigation
3334
run: |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export const Search = () => {
2+
return (
3+
<button
4+
id="fern-search-button"
5+
className="fern-search-bar w-full"
6+
style={{ marginBottom: '8px' }}
7+
onClick={() => {
8+
if ((window as any).plugSDK) {
9+
(window as any).plugSDK.toggleSearchAgent()
10+
}
11+
}}>
12+
<span className="search-placeholder">
13+
<svg
14+
width="1.5em"
15+
height="1.5em"
16+
viewBox="0 0 24 24"
17+
strokeWidth="1.5"
18+
fill="none"
19+
xmlns="http://www.w3.org/2000/svg"
20+
color="currentColor"
21+
className="size-icon-md">
22+
<path
23+
d="M17 17L21 21"
24+
stroke="currentColor"
25+
strokeLinecap="round"
26+
strokeLinejoin="round"></path>
27+
<path
28+
d="M3 11C3 15.4183 6.58172 19 11 19C13.213 19 15.2161 18.1015 16.6644 16.6493C18.1077 15.2022 19 13.2053 19 11C19 6.58172 15.4183 3 11 3C6.58172 3 3 6.58172 3 11Z"
29+
stroke="currentColor"
30+
strokeLinecap="round"
31+
strokeLinejoin="round"></path>
32+
</svg>
33+
<span>Search...</span>
34+
</span>
35+
<kbd className="keyboard-shortcut-hint">/</kbd>
36+
</button>
37+
)
38+
}

custom-implementation/src/main.tsx

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import React from 'react'
77

88
import Header from './components/header'
99
import Footer from './components/footer'
10+
11+
import { Search } from './components/search'
1012
import { ThemeSwitch } from './components/theme-switch'
1113

1214
import { getPageData } from './modules/sanity/utils'
@@ -22,16 +24,28 @@ const render = async () => {
2224
*/
2325

2426
const data = await getPageData()
27+
2528
const sidenav = document.querySelector('button.fern-search-bar')
2629
?.parentElement as HTMLElement
2730

2831
const theme = document.getElementsByTagName('html')[0].getAttribute('class')
2932

30-
if (!document.getElementById('theme-switch')) {
33+
if (!document.getElementById('sidenav-header-wrapper')) {
34+
const sidenavHeaderWrapper = document.createElement('div')
35+
sidenavHeaderWrapper.setAttribute('id', 'sidenav-header-wrapper')
36+
sidenav.appendChild(sidenavHeaderWrapper)
37+
38+
const search = document.createElement('div')
39+
search.setAttribute('id', 'search-component')
40+
sidenavHeaderWrapper.appendChild(search)
41+
ReactDOM.render(React.createElement(Search), search)
42+
3143
const wrapper = document.createElement('div')
3244
wrapper.setAttribute('id', 'theme-switch')
33-
sidenav.appendChild(wrapper)
45+
sidenavHeaderWrapper.appendChild(wrapper)
3446
ReactDOM.render(React.createElement(ThemeSwitch), wrapper)
47+
48+
sidenav.replaceWith(sidenavHeaderWrapper)
3549
}
3650

3751
const fernHeaderId = document.getElementById(FERN_CONTENT_WRAPPER_ID)
@@ -92,11 +106,46 @@ const render = async () => {
92106
if (footer) footer.style.display = 'block'
93107
},
94108
)
109+
110+
// Add Plug component directly to body
111+
if (!document.getElementById('plug-platform')) {
112+
const plugScript = document.createElement('script')
113+
plugScript.setAttribute('type', 'text/javascript')
114+
plugScript.setAttribute('id', 'plug-platform')
115+
plugScript.setAttribute('src', 'https://plug-platform.devrev.ai/static/plug.js')
116+
document.body.appendChild(plugScript)
117+
118+
// Initialize Plug SDK after script loads
119+
plugScript.onload = () => {
120+
if ((window as any).plugSDK) {
121+
(window as any).plugSDK?.init?.({
122+
app_id: data?.plug?.id,
123+
enable_session_recording: true,
124+
});
125+
126+
// Wait for the widget to be ready before adding event listeners
127+
(window as any).plugSDK.onEvent((payload: any) => {
128+
if(payload.type === "ON_PLUG_WIDGET_READY") {
129+
// Initialize search agent after widget is ready
130+
(window as any).plugSDK.initSearchAgent();
131+
132+
// Add keyboard shortcut for search agent
133+
document.addEventListener("keydown", function(event) {
134+
// Check if event.key is defined before accessing it
135+
if (event && event.key === "/") {
136+
event.preventDefault();
137+
(window as any).plugSDK.toggleSearchAgent();
138+
}
139+
});
140+
}
141+
});
142+
}
143+
}
144+
}
95145
}
96146

97147
let observations = 0
98148
document.addEventListener('DOMContentLoaded', async () => {
99-
console.log('DOMContentLoaded')
100149
await render()
101150
new MutationObserver(async (e, o) => {
102151
await render()

custom-implementation/src/modules/sanity/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const SANITY_TYPES = {
1515
export const getPageData = async (): Promise<{
1616
header: object
1717
footer: object
18+
plug: { id: string }
1819
}> => {
1920
const header = await CLIENT.fetch(
2021
`*[_type == 'headerV2' && slug.current == 'developer']{
@@ -28,5 +29,11 @@ export const getPageData = async (): Promise<{
2829
}[0]`,
2930
)
3031

31-
return { header, footer }
32+
const plug = await CLIENT.fetch(
33+
`*[_type == 'externalLink' && slug.current == 'devrev-plug-app-id']{
34+
"id": href
35+
}[0]`,
36+
)
37+
38+
return { header, footer, plug }
3239
}

0 commit comments

Comments
 (0)