|
| 1 | +import { useEffect, useCallback, useState } from 'react'; |
| 2 | +// @ts-ignore - Virtual module injected by Vite |
| 3 | +import { kapaWebsiteId } from 'virtual:kapa-ai-config'; |
| 4 | + |
| 5 | +declare global { |
| 6 | + interface Window { |
| 7 | + Kapa?: { |
| 8 | + open: (options?: { |
| 9 | + mode?: 'ai' | 'search'; |
| 10 | + query?: string; |
| 11 | + submit?: boolean; |
| 12 | + }) => void; |
| 13 | + close: () => void; |
| 14 | + render: (options?: { onRender?: () => void }) => void; |
| 15 | + unmount: () => void; |
| 16 | + }; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const KAPA_SCRIPT_ID = 'kapa-widget-script'; |
| 21 | + |
| 22 | +export function useKapaAI() { |
| 23 | + const [isLoaded, setIsLoaded] = useState(false); |
| 24 | + const isConfigured = Boolean(kapaWebsiteId); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (!isConfigured) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Check if script already exists |
| 32 | + if (document.getElementById(KAPA_SCRIPT_ID)) { |
| 33 | + if (window.Kapa) { |
| 34 | + setIsLoaded(true); |
| 35 | + } |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const script = document.createElement('script'); |
| 40 | + script.id = KAPA_SCRIPT_ID; |
| 41 | + script.src = 'https://widget.kapa.ai/kapa-widget.bundle.js'; |
| 42 | + script.async = true; |
| 43 | + script.setAttribute('data-website-id', kapaWebsiteId); |
| 44 | + script.setAttribute('data-project-name', 'WordPress Playground'); |
| 45 | + script.setAttribute('data-project-color', '#3858e9'); |
| 46 | + script.setAttribute( |
| 47 | + 'data-project-logo', |
| 48 | + 'https://playground.wordpress.net/logo-square.png' |
| 49 | + ); |
| 50 | + script.setAttribute('data-button-hide', 'true'); |
| 51 | + |
| 52 | + script.onload = () => { |
| 53 | + const checkKapa = setInterval(() => { |
| 54 | + if (window.Kapa) { |
| 55 | + clearInterval(checkKapa); |
| 56 | + setIsLoaded(true); |
| 57 | + } |
| 58 | + }, 100); |
| 59 | + |
| 60 | + setTimeout(() => clearInterval(checkKapa), 5000); |
| 61 | + }; |
| 62 | + |
| 63 | + document.body.appendChild(script); |
| 64 | + }, [isConfigured]); |
| 65 | + |
| 66 | + const openWithQuery = useCallback((query: string) => { |
| 67 | + if (window.Kapa) { |
| 68 | + window.Kapa.open({ |
| 69 | + mode: 'ai', |
| 70 | + query: `Suggest a solution or troubleshooting steps for the following error: ${query}`, |
| 71 | + submit: true, |
| 72 | + }); |
| 73 | + |
| 74 | + // Move widget container inside screen overlay so it appears on top |
| 75 | + const moveContainer = () => { |
| 76 | + const container = document.getElementById( |
| 77 | + 'kapa-widget-container' |
| 78 | + ); |
| 79 | + const overlay = document.querySelector( |
| 80 | + '.components-modal__screen-overlay' |
| 81 | + ); |
| 82 | + if (container && overlay) { |
| 83 | + overlay.insertBefore(container, overlay.firstChild); |
| 84 | + } else { |
| 85 | + setTimeout(moveContainer, 50); |
| 86 | + } |
| 87 | + }; |
| 88 | + setTimeout(moveContainer, 50); |
| 89 | + } |
| 90 | + }, []); |
| 91 | + |
| 92 | + return { |
| 93 | + isConfigured, |
| 94 | + isLoaded, |
| 95 | + openWithQuery, |
| 96 | + }; |
| 97 | +} |
0 commit comments