@@ -74,6 +74,7 @@ const builtInCssClasses: ChatPopUpCssClasses = withStylelessCssClasses(
7474 } ,
7575 }
7676) ;
77+ const popupLocalStorageKey = "YEXT_CHAT_OPEN_ON_LOAD" ;
7778
7879/**
7980 * The props for the {@link ChatPopUp} component.
@@ -151,6 +152,7 @@ export function ChatPopUp(props: ChatPopUpProps) {
151152 //only show initial message popup (if specified) when CTA label is not provided
152153 ! ctaLabel && showInitialMessagePopUp
153154 ) ;
155+
154156 const onCloseInitialMessage = useCallback ( ( ) => {
155157 setshowInitialMessage ( false ) ;
156158 } , [ ] ) ;
@@ -162,41 +164,58 @@ export function ChatPopUp(props: ChatPopUpProps) {
162164 // to avoid message requests immediately on load while the popup is still "hidden"
163165 const [ renderChat , setRenderChat ] = useState ( false ) ;
164166
167+ // Set the initial value of the local storage flag for opening on load only if it doesn't already exist
168+
169+ if ( window . localStorage . getItem ( popupLocalStorageKey ) === null ) {
170+ window . localStorage . setItem (
171+ popupLocalStorageKey ,
172+ openOnLoad ? "true" : "false"
173+ ) ;
174+ }
175+ const openOnLoadLocalStorage =
176+ window . localStorage . getItem ( popupLocalStorageKey ) ;
177+
178+ /* Open panel on load if:
179+ - openOnLoad prop is true or there are messages in state (from browser storage), and local storage flag is true */
180+ const isOpenOnLoad =
181+ ( messages . length > 1 && openOnLoadLocalStorage === "true" ) || openOnLoad ;
182+
165183 // only fetch initial message when ChatPanel is closed on load (otherwise, it will be fetched in ChatPanel)
166184 useFetchInitialMessage (
167185 showInitialMessagePopUp ? console . error : handleError ,
168186 false ,
169187 ( showUnreadNotification || showInitialMessagePopUp ) &&
170188 ! renderChat &&
171- ! openOnLoad
189+ ! isOpenOnLoad
172190 ) ;
173191
174192 useEffect ( ( ) => {
175- // Open panel on load if openOnLoad prop is true or there are messages in state (from browser storage)
176- if ( ! renderChat && ( openOnLoad || messages . length > 1 ) ) {
193+ if ( ! renderChat && isOpenOnLoad ) {
177194 setShowChat ( true ) ;
178195 setRenderChat ( true ) ;
179196 setshowInitialMessage ( false ) ;
180197 }
181- } , [ messages . length , openOnLoad , renderChat ] ) ;
198+ } , [ renderChat , messages . length , isOpenOnLoad ] ) ;
182199
183200 const onClick = useCallback ( ( ) => {
184201 setShowChat ( ( prev ) => ! prev ) ;
185202 setRenderChat ( true ) ;
186203 setshowInitialMessage ( false ) ;
204+ window . localStorage . setItem ( popupLocalStorageKey , "true" ) ;
187205 } , [ ] ) ;
188206
189207 const onClose = useCallback ( ( ) => {
190208 setShowChat ( false ) ;
191209 customOnClose ?.( ) ;
192210 // consider all the messages are read while the panel was open
193211 setNumReadMessagesLength ( messages . length ) ;
194- } , [ customOnClose , messages ] ) ;
212+ window . localStorage . setItem ( popupLocalStorageKey , "false" ) ;
213+ } , [ customOnClose , messages . length ] ) ;
195214
196215 useEffect ( ( ) => {
197216 // update number of unread messages if there are new messages added while the panel is closed
198217 setNumUnreadMessagesLength ( messages . length - numReadMessages ) ;
199- } , [ messages , numReadMessages ] ) ;
218+ } , [ messages . length , numReadMessages ] ) ;
200219
201220 const cssClasses = useComposedCssClasses ( builtInCssClasses , customCssClasses ) ;
202221 const panelCssClasses = twMerge (
0 commit comments