Skip to content

Commit

Permalink
feat: load kapa async on click (#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Jan 15, 2025
1 parent 444987d commit 6b8277d
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/theme/KapaWidget.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
. mantine-UnstyledButton-root {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
}

.mantine-1wpc1xj {
display: flex;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
height: 100%;
overflow: visible;
}

.mantine-124rx6h {
padding: 0px 1.625rem;
appearance: none;
text-align: left;
text-decoration: none;
box-sizing: border-box;
height: 3.125rem;
-webkit-tap-highlight-color: transparent;
display: inline-block;
width: auto;
border-radius: 0.25rem;
font-weight: 600;
position: relative;
line-height: 1;
font-size: 1.125rem;
user-select: none;
cursor: pointer;
border: 0.0625rem solid transparent;
background-color: rgb(17, 28, 111);
color: rgb(255, 255, 255);
}

.mantine-1wpc1xj {
display: flex;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
height: 100%;
overflow: visible;
}

.mantine-1ryt1ht {
white-space: nowrap;
height: 100%;
overflow: hidden;
display: flex;
-webkit-box-align: center;
align-items: center;
}

.mantine-1b4vo4u {
display: flex;
flex-direction: column;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
gap: 4px;
}

.mantine-qenwvq {
margin: 0px;
}

.mantine-1iugybl {
position: relative;
}

.mantine-1sc70ew {
display: block;
width: 100%;
height: 100%;
border: 0px;
border-radius: 0.25rem;
}

.mantine-1y9jqg9 {
-webkit-tap-highlight-color: transparent;
color: inherit;
font-size: 1.125rem;
line-height: 1.55;
text-decoration: none;
margin-left: 6px;
margin-right: 6px;
}
94 changes: 94 additions & 0 deletions src/theme/KapaWidget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React, { useState } from "react"
import "./KapaWidget.css"

export default function KapaWidget() {
const [isScriptLoaded, setIsScriptLoaded] = useState(false)
const [child, setChild] = useState<HTMLScriptElement | undefined>()

const loadKapaWidget = () => {
console.log("Kapai", window.Kapa)

if (isScriptLoaded) {
document.body.removeChild(child)
}

const script = document.createElement("script")
script.src = "https://widget.kapa.ai/kapa-widget.bundle.js"
script.async = true
script.setAttribute(
"data-website-id",
"e89e7663-df2c-4c7f-974a-1bf8accdd615",
)
script.setAttribute("data-project-name", "Ory")
script.setAttribute("data-project-color", "#1A237E")
script.setAttribute(
"data-modal-disclaimer",
"By utilizing this chatbot, you consent to the collection and transmission of data to kapa.ai, which may include your IP address. Please be advised that your privacy and data protection are of utmost importance to us. We assure you that any data collected will be handled in compliance with applicable laws and regulations. For further details on how your data is processed and used, we encourage you to review our Privacy Policy. If you do not agree with these terms, we kindly request that you refrain from using this chatbot.",
)
script.setAttribute("data-modal-title", "Ory AI Copilot")
script.setAttribute("data-button-text", "Ask AI")
script.setAttribute("data-project-logo", "/docs/img/kapa-logo.png")
script.setAttribute("data-consent-required", "true")
script.setAttribute("data-button-hide", "true")
script.setAttribute("data-modal-override-open-id", "custom-button-id")
script.setAttribute("data-modal-open-by-default", "true")
script.onload = () => setIsScriptLoaded(true)
setChild(document.body.appendChild(script))
}

return (
<button
id="custom-button-id"
className="mantine-UnstyledButton-root mantine-Button-root mantine-124rx6h"
type="button"
data-button="true"
style={{
position: "fixed",
bottom: "20px",
right: "20px",
height: "5rem",
width: "4.5rem",
padding: "0px",
backgroundColor: "rgb(26, 35, 126)",
boxShadow: "rgba(0, 0, 0, 0.2) 0px 0px 10px",
zIndex: 199,
}}
onClick={loadKapaWidget}
>
<div className="mantine-1wpc1xj mantine-Button-inner">
<span className="mantine-1ryt1ht mantine-Button-label">
<div
className="mantine-Stack-root mantine-1b4vo4u"
style={{ marginTop: "4px" }}
>
<div
className="mantine-Image-root mantine-yxmaw9"
style={{ width: "2rem" }}
>
<figure className="mantine-qenwvq mantine-Image-figure">
<div className="mantine-1iugybl mantine-Image-imageWrapper">
<img
src="/docs/img/kapa-logo.png"
alt="Project Logo"
className="mantine-1sc70ew mantine-Image-image"
style={{
objectFit: "cover",
width: "2rem",
height: "2rem",
}}
/>
</div>
</figure>
</div>
<div
className="mantine-Text-root mantine-1y9jqg9"
style={{ textShadow: "rgba(0, 0, 0, 0.2) 1px 1px 2px" }}
>
Ask AI
</div>
</div>
</span>
</div>
</button>
)
}
8 changes: 8 additions & 0 deletions src/theme/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@

import React from "react"
import { QueryClient, QueryClientProvider, useQuery } from "react-query"
import KapaWidget from "./KapaWidget"

const queryClient = new QueryClient()

function Root({ children }) {
return (
<>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
<KapaWidget />
<script
dangerouslySetInnerHTML={{
__html: `(function(){let k=window.Kapa;if(!k){let i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};window.Kapa=i}})();`,
}}
/>
</>
)
}
Expand Down

0 comments on commit 6b8277d

Please sign in to comment.