Skip to content

Commit 7880a3b

Browse files
committed
WIP using the react example from the docs
1 parent bfcbd09 commit 7880a3b

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

apps/webapp/app/root.tsx

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import { appEnvTitleTag } from "./utils";
1717
import { type Handle } from "./utils/handle";
1818
import { useEffect } from "react";
1919

20+
declare global {
21+
interface Window {
22+
Kapa: (command: string, options?: { onRender?: () => void }) => void;
23+
}
24+
}
25+
2026
export const links: LinksFunction = () => {
2127
return [{ rel: "stylesheet", href: tailwindStylesheetUrl }];
2228
};
@@ -97,23 +103,23 @@ export default function App() {
97103
const { posthogProjectKey, kapa, features } = useTypedLoaderData<typeof loader>();
98104
usePostHog(posthogProjectKey);
99105

106+
useEffect(() => {
107+
if (!features.isManagedCloud || !kapa.websiteId) return;
108+
109+
loadScriptIfNotExists(kapa.websiteId);
110+
window.Kapa("render", {
111+
onRender: () => window.Kapa("open"),
112+
});
113+
114+
return () => window.Kapa("unmount");
115+
}, [features.isManagedCloud, kapa.websiteId]);
116+
100117
return (
101118
<>
102119
<html lang="en" className="h-full">
103120
<head>
104121
<Meta />
105122
<Links />
106-
{features.isManagedCloud && (
107-
<script
108-
src="/resources/kapa-widget"
109-
crossOrigin="anonymous"
110-
async
111-
data-website-id={kapa.websiteId}
112-
data-project-name="Trigger.dev"
113-
data-project-color="#ff9900"
114-
data-project-logo="content.trigger.dev/trigger-logo-triangle.png"
115-
/>
116-
)}
117123
</head>
118124
<body className="bg-darkBackground h-full overflow-hidden">
119125
<Outlet />
@@ -127,3 +133,29 @@ export default function App() {
127133
</>
128134
);
129135
}
136+
137+
function loadScriptIfNotExists(websiteId: string) {
138+
const scriptSrc = "https://widget.kapa.ai/kapa-widget.bundle.js";
139+
140+
if (document.querySelector(`script[src="${scriptSrc}"]`)) {
141+
return;
142+
}
143+
144+
const script = document.createElement("script");
145+
script.async = true;
146+
script.src = scriptSrc;
147+
148+
const attributes = {
149+
"data-website-id": websiteId,
150+
"data-project-name": "Trigger.dev",
151+
"data-project-color": "#ff9900",
152+
"data-project-logo": "content.trigger.dev/trigger-logo-triangle.png",
153+
"data-render-on-load": "false",
154+
};
155+
156+
Object.entries(attributes).forEach(([key, value]) => {
157+
script.setAttribute(key, value);
158+
});
159+
160+
document.head.appendChild(script);
161+
}

0 commit comments

Comments
 (0)