@@ -3,6 +3,16 @@ import { useShortcuts } from "../components/primitives/ShortcutsProvider";
3
3
import { useFeatures } from "~/hooks/useFeatures" ;
4
4
import { useCallback , useEffect , useState } from "react" ;
5
5
6
+ declare global {
7
+ interface Window {
8
+ Kapa : (
9
+ command : string ,
10
+ options ?: ( ) => void ,
11
+ remove ?: string | { onRender ?: ( ) => void }
12
+ ) => void ;
13
+ }
14
+ }
15
+
6
16
export function useKapaWidget ( ) {
7
17
const kapa = useKapaConfig ( ) ;
8
18
const features = useFeatures ( ) ;
@@ -20,17 +30,19 @@ export function useKapaWidget() {
20
30
enableShortcuts ( ) ;
21
31
} ;
22
32
33
+ const handleModalOpen = ( ) => {
34
+ setIsKapaOpen ( true ) ;
35
+ disableShortcuts ( ) ;
36
+ } ;
37
+
23
38
const kapaInterval = setInterval ( ( ) => {
24
39
if ( typeof window . Kapa === "function" ) {
25
40
clearInterval ( kapaInterval ) ;
26
41
window . Kapa ( "render" ) ;
27
42
window . Kapa ( "onModalClose" , handleModalClose ) ;
28
43
29
44
// Register onModalOpen handler
30
- window . Kapa ( "onModalOpen" , ( ) => {
31
- setIsKapaOpen ( true ) ;
32
- disableShortcuts ( ) ;
33
- } ) ;
45
+ window . Kapa ( "onModalOpen" , handleModalOpen ) ;
34
46
}
35
47
} , 100 ) ;
36
48
@@ -39,12 +51,13 @@ export function useKapaWidget() {
39
51
clearInterval ( kapaInterval ) ;
40
52
if ( typeof window . Kapa === "function" ) {
41
53
window . Kapa ( "unmount" ) ;
54
+
55
+ window . Kapa ( "onModalOpen" , handleModalOpen , "remove" ) ;
56
+ window . Kapa ( "onModalClose" , handleModalClose , "remove" ) ;
42
57
}
43
58
} ;
44
59
} , [ features . isManagedCloud , kapa ?. websiteId , disableShortcuts , enableShortcuts ] ) ;
45
60
46
- //todo remove listeners
47
-
48
61
const openKapa = useCallback ( ( ) => {
49
62
if ( ! features . isManagedCloud || ! kapa ?. websiteId ) return ;
50
63
0 commit comments