@@ -80,8 +80,10 @@ import { useShortcutKeys } from "~/hooks/useShortcutKeys";
8080import { AISparkleIcon } from "~/assets/icons/AISparkleIcon" ;
8181import { ShortcutKey } from "../primitives/ShortcutKey" ;
8282import { useFeatures } from "~/hooks/useFeatures" ;
83- import { useKapa } from "~/root" ;
83+ import { useKapaConfig } from "~/root" ;
8484import { useShortcuts } from "~/components/primitives/ShortcutsProvider" ;
85+ import { useKapaWidget } from "../../hooks/useKapaWidget" ;
86+ import { ShortcutsAutoOpen } from "../Shortcuts" ;
8587
8688type SideMenuUser = Pick < User , "email" | "admin" > & { isImpersonating : boolean } ;
8789export type SideMenuProject = Pick <
@@ -548,104 +550,41 @@ function SelectorDivider() {
548550}
549551
550552function HelpAndAI ( ) {
551- const [ isKapaOpen , setIsKapaOpen ] = useState ( false ) ;
552- const features = useFeatures ( ) ;
553- const kapa = useKapa ( ) ;
554- const { disableShortcuts, enableShortcuts } = useShortcuts ( ) ;
555-
556- useEffect ( ( ) => {
557- if ( ! features . isManagedCloud || ! kapa ?. websiteId ) return ;
558-
559- loadScriptIfNotExists ( kapa . websiteId ) ;
560-
561- // Define the handler function
562- const handleModalClose = ( ) => {
563- setIsKapaOpen ( false ) ;
564- enableShortcuts ( ) ;
565- } ;
566-
567- const kapaInterval = setInterval ( ( ) => {
568- if ( typeof window . Kapa === "function" ) {
569- clearInterval ( kapaInterval ) ;
570- window . Kapa ( "render" ) ;
571- window . Kapa ( "onModalClose" , handleModalClose ) ;
572- // Register onModalOpen handler
573- window . Kapa ( "onModalOpen" , ( ) => {
574- setIsKapaOpen ( true ) ;
575- disableShortcuts ( ) ;
576- } ) ;
577- }
578- } , 100 ) ;
579-
580- // Clear interval on unmount to prevent memory leaks
581- return ( ) => {
582- clearInterval ( kapaInterval ) ;
583- if ( typeof window . Kapa === "function" ) {
584- window . Kapa ( "unmount" ) ;
585- }
586- } ;
587- } , [ features . isManagedCloud , kapa ?. websiteId , disableShortcuts , enableShortcuts ] ) ;
553+ const { isKapaEnabled, openKapa, isKapaOpen } = useKapaWidget ( ) ;
588554
589555 return (
590556 < >
557+ < ShortcutsAutoOpen />
591558 < HelpAndFeedback disableShortcut = { isKapaOpen } />
592- < TooltipProvider disableHoverableContent >
593- < Tooltip >
594- < TooltipTrigger asChild >
595- < div className = "inline-flex" >
596- < Button
597- variant = "small-menu-item"
598- data-action = "ask-ai"
599- shortcut = { { modifiers : [ "mod" ] , key : "/" , enabledOnInputElements : true } }
600- hideShortcutKey
601- data-modal-override-open-class-ask-ai = "true"
602- onClick = { ( ) => {
603- if ( typeof window . Kapa === "function" ) {
604- window . Kapa ( "open" ) ;
605- setIsKapaOpen ( true ) ;
606- disableShortcuts ( ) ;
607- }
608- } }
609- >
610- < AISparkleIcon className = "size-5" />
611- </ Button >
612- </ div >
613- </ TooltipTrigger >
614- < TooltipContent side = "top" className = "flex items-center gap-1 py-1.5 pl-2.5 pr-2 text-xs" >
615- Ask AI
616- < ShortcutKey shortcut = { { modifiers : [ "mod" ] , key : "/" } } variant = "medium/bright" />
617- </ TooltipContent >
618- </ Tooltip >
619- </ TooltipProvider >
559+ { isKapaEnabled && (
560+ < TooltipProvider disableHoverableContent >
561+ < Tooltip >
562+ < TooltipTrigger asChild >
563+ < div className = "inline-flex" >
564+ < Button
565+ variant = "small-menu-item"
566+ data-action = "ask-ai"
567+ shortcut = { { modifiers : [ "mod" ] , key : "/" , enabledOnInputElements : true } }
568+ hideShortcutKey
569+ data-modal-override-open-class-ask-ai = "true"
570+ onClick = { ( ) => {
571+ openKapa ( ) ;
572+ } }
573+ >
574+ < AISparkleIcon className = "size-5" />
575+ </ Button >
576+ </ div >
577+ </ TooltipTrigger >
578+ < TooltipContent
579+ side = "top"
580+ className = "flex items-center gap-1 py-1.5 pl-2.5 pr-2 text-xs"
581+ >
582+ Ask AI
583+ < ShortcutKey shortcut = { { modifiers : [ "mod" ] , key : "/" } } variant = "medium/bright" />
584+ </ TooltipContent >
585+ </ Tooltip >
586+ </ TooltipProvider >
587+ ) }
620588 </ >
621589 ) ;
622590}
623-
624- function loadScriptIfNotExists ( websiteId : string ) {
625- const scriptSrc = "https://widget.kapa.ai/kapa-widget.bundle.js" ;
626-
627- if ( document . querySelector ( `script[src="${ scriptSrc } "]` ) ) {
628- return ;
629- }
630-
631- const script = document . createElement ( "script" ) ;
632- script . async = true ;
633- script . src = scriptSrc ;
634-
635- const attributes = {
636- "data-website-id" : websiteId ,
637- "data-project-name" : "Trigger.dev" ,
638- "data-project-color" : "#6366F1" ,
639- "data-project-logo" : "https://content.trigger.dev/trigger-logo-triangle.png" ,
640- "data-render-on-load" : "false" ,
641- "data-button-hide" : "true" ,
642- "data-modal-disclaimer-bg-color" : "#1A1B1F" ,
643- "data-modal-disclaimer-text-color" : "#878C99" ,
644- } ;
645-
646- Object . entries ( attributes ) . forEach ( ( [ key , value ] ) => {
647- script . setAttribute ( key , value ) ;
648- } ) ;
649-
650- document . head . appendChild ( script ) ;
651- }
0 commit comments