@@ -32,6 +32,7 @@ export const IntercomProvider: React.FC<
32
32
} ) => {
33
33
const isBooted = React . useRef ( false ) ;
34
34
const isInitialized = React . useRef ( false ) ;
35
+ const [ isOpen , setIsOpen ] = React . useState ( false ) ;
35
36
36
37
// Allow data-x attributes, see https://github.com/devrnt/react-use-intercom/issues/478
37
38
const invalidPropKeys = Object . keys ( rest ) . filter (
@@ -48,6 +49,16 @@ export const IntercomProvider: React.FC<
48
49
) ;
49
50
}
50
51
52
+ const onHideWrapper = React . useCallback ( ( ) => {
53
+ setIsOpen ( false ) ;
54
+ if ( onHide ) onHide ( ) ;
55
+ } , [ onHide , setIsOpen ] ) ;
56
+
57
+ const onShowWrapper = React . useCallback ( ( ) => {
58
+ setIsOpen ( true ) ;
59
+ if ( onShow ) onShow ( ) ;
60
+ } , [ onShow , setIsOpen ] ) ;
61
+
51
62
const boot = React . useCallback (
52
63
( props ?: IntercomProps ) => {
53
64
if ( ! window . Intercom && ! shouldInitialize ) {
@@ -58,6 +69,16 @@ export const IntercomProvider: React.FC<
58
69
return ;
59
70
}
60
71
72
+ // Attach the listeners
73
+ // This is done in the booth method because after shutting down
74
+ // the callbacks should be re-registered
75
+ IntercomAPI ( 'onHide' , onHideWrapper ) ;
76
+ IntercomAPI ( 'onShow' , onShowWrapper ) ;
77
+ IntercomAPI ( 'onUserEmailSupplied' , onUserEmailSupplied ) ;
78
+
79
+ if ( onUnreadCountChange )
80
+ IntercomAPI ( 'onUnreadCountChange' , onUnreadCountChange ) ;
81
+
61
82
const metaData : RawIntercomBootProps = {
62
83
app_id : appId ,
63
84
...( apiBase && { api_base : apiBase } ) ,
@@ -68,38 +89,28 @@ export const IntercomProvider: React.FC<
68
89
IntercomAPI ( 'boot' , metaData ) ;
69
90
isBooted . current = true ;
70
91
} ,
71
- [ apiBase , appId , shouldInitialize ] ,
92
+ [
93
+ apiBase ,
94
+ appId ,
95
+ onHideWrapper ,
96
+ onShowWrapper ,
97
+ onUnreadCountChange ,
98
+ onUserEmailSupplied ,
99
+ shouldInitialize ,
100
+ ] ,
72
101
) ;
73
102
74
- const [ isOpen , setIsOpen ] = React . useState ( false ) ;
103
+ React . useEffect ( ( ) => {
104
+ if ( ! isSSR && shouldInitialize && ! isInitialized . current ) {
105
+ initialize ( appId , initializeDelay ) ;
75
106
76
- const onHideWrapper = React . useCallback ( ( ) => {
77
- setIsOpen ( false ) ;
78
- if ( onHide ) onHide ( ) ;
79
- } , [ onHide , setIsOpen ] ) ;
80
-
81
- const onShowWrapper = React . useCallback ( ( ) => {
82
- setIsOpen ( true ) ;
83
- if ( onShow ) onShow ( ) ;
84
- } , [ onShow , setIsOpen ] ) ;
85
-
86
- if ( ! isSSR && shouldInitialize && ! isInitialized . current ) {
87
- initialize ( appId , initializeDelay ) ;
88
-
89
- // attach listeners
90
- IntercomAPI ( 'onHide' , onHideWrapper ) ;
91
- IntercomAPI ( 'onShow' , onShowWrapper ) ;
92
- IntercomAPI ( 'onUserEmailSupplied' , onUserEmailSupplied ) ;
93
-
94
- if ( onUnreadCountChange )
95
- IntercomAPI ( 'onUnreadCountChange' , onUnreadCountChange ) ;
107
+ if ( autoBoot ) {
108
+ boot ( autoBootProps ) ;
109
+ }
96
110
97
- if ( autoBoot ) {
98
- boot ( autoBootProps ) ;
111
+ isInitialized . current = true ;
99
112
}
100
-
101
- isInitialized . current = true ;
102
- }
113
+ } , [ appId , autoBoot , autoBootProps , boot , initializeDelay , shouldInitialize ] ) ;
103
114
104
115
const ensureIntercom = React . useCallback (
105
116
( functionName : string , callback : ( ( ) => void ) | ( ( ) => string ) ) => {
0 commit comments