1
- import { useShortcuts } from "../components/primitives/ShortcutsProvider" ;
2
- import { useFeatures } from "~/hooks/useFeatures" ;
1
+ import { useMatches , useSearchParams } from "@remix-run/react" ;
3
2
import { useCallback , useEffect , useState } from "react" ;
4
- import { useMatches } from "@remix-run/react" ;
5
- import { useTypedMatchesData } from "./useTypedMatchData" ;
3
+ import { useFeatures } from "~/hooks/useFeatures" ;
6
4
import { type loader } from "~/root" ;
5
+ import { useShortcuts } from "../components/primitives/ShortcutsProvider" ;
6
+ import { useTypedMatchesData } from "./useTypedMatchData" ;
7
7
8
8
type OpenOptions = { mode : string ; query : string ; submit : boolean } ;
9
9
10
10
declare global {
11
11
interface Window {
12
12
Kapa : (
13
13
command : string ,
14
- options ?: ( ( ) => void ) | OpenOptions ,
14
+ options ?: ( ( ) => void ) | { onRender ?: ( ) => void } | OpenOptions ,
15
15
remove ?: string | { onRender ?: ( ) => void }
16
16
) => void ;
17
17
}
@@ -77,6 +77,7 @@ export function useKapaWidget() {
77
77
const features = useFeatures ( ) ;
78
78
const { disableShortcuts, enableShortcuts, areShortcutsEnabled } = useShortcuts ( ) ;
79
79
const [ isKapaOpen , setIsKapaOpen ] = useState ( false ) ;
80
+ const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
80
81
81
82
const handleModalClose = useCallback ( ( ) => {
82
83
setIsKapaOpen ( false ) ;
@@ -88,19 +89,35 @@ export function useKapaWidget() {
88
89
disableShortcuts ( ) ;
89
90
} , [ disableShortcuts ] ) ;
90
91
92
+ // Handle opening/closing
91
93
useEffect ( ( ) => {
92
94
if ( ! features . isManagedCloud || ! kapa ?. websiteId ) return ;
93
95
94
- window . Kapa ( "render" ) ;
96
+ window . Kapa ( "render" , {
97
+ onRender : ( ) => {
98
+ const aiHelp = searchParams . get ( "aiHelp" ) ;
99
+ if ( aiHelp ) {
100
+ setSearchParams ( ( prev ) => {
101
+ prev . delete ( "aiHelp" ) ;
102
+ return prev ;
103
+ } ) ;
104
+
105
+ //we need to decode the aiHelp string because it's urlencoded
106
+ const decodedAiHelp = decodeURIComponent ( aiHelp ) ;
107
+ openKapa ( decodedAiHelp ) ;
108
+ }
109
+ } ,
110
+ } ) ;
95
111
window . Kapa ( "onModalOpen" , handleModalOpen ) ;
96
112
window . Kapa ( "onModalClose" , handleModalClose ) ;
97
113
98
114
return ( ) => {
99
115
window . Kapa ( "onModalOpen" , handleModalOpen , "remove" ) ;
100
116
window . Kapa ( "onModalClose" , handleModalClose , "remove" ) ;
101
117
} ;
102
- } , [ features . isManagedCloud , kapa ?. websiteId ] ) ;
118
+ } , [ features . isManagedCloud , kapa ?. websiteId , searchParams , setSearchParams ] ) ;
103
119
120
+ // Handle opening the Kapa widget
104
121
const openKapa = useCallback (
105
122
( query ?: string ) => {
106
123
if ( ! features . isManagedCloud || ! kapa ?. websiteId ) return ;
0 commit comments