22 * Internal dependencies
33 */
44import { TOUR_STEPS } from './tour-steps'
5+ import {
6+ setActiveTour ,
7+ clearActiveTour ,
8+ isTourActive ,
9+ getActiveTourId ,
10+ addTourStateListener ,
11+ } from './util'
512
613/**
714 * External dependencies
@@ -42,6 +49,17 @@ const GuidedModalTour = props => {
4249 // We need this to prevent the tour from being shown again if it's just completed.
4350 const [ justCompleted , setJustCompleted ] = useState ( false )
4451
52+ // Check if another tour is already active
53+ const [ isAnotherTourActive , setIsAnotherTourActive ] = useState ( isTourActive ( ) && getActiveTourId ( ) !== tourId )
54+
55+ // Listen for tour state changes
56+ useEffect ( ( ) => {
57+ const removeListener = addTourStateListener ( activeId => {
58+ setIsAnotherTourActive ( activeId !== null && activeId !== tourId )
59+ } )
60+ return removeListener
61+ } , [ tourId ] )
62+
4563 const {
4664 steps = [ ] ,
4765 condition = null ,
@@ -53,6 +71,11 @@ const GuidedModalTour = props => {
5371 return null
5472 }
5573
74+ // If another tour is already active, don't show this tour
75+ if ( isAnotherTourActive ) {
76+ return null
77+ }
78+
5679 // If there is a condition, check if it's met, if not, don't show the tour.
5780 // condition can be true, false, or null. true will show the tour (even if
5881 // it's already done), false will not show the tour, null will show the tour
@@ -71,13 +94,17 @@ const GuidedModalTour = props => {
7194 }
7295
7396 return < ModalTour
97+ tourId = { tourId }
7498 steps = { steps }
7599 hasConfetti = { hasConfetti }
76100 initialize = { initialize }
77101 onClose = { ( ) => {
78102 setIsDone ( true )
79103 setJustCompleted ( true )
80104
105+ // Clear the active tour
106+ clearActiveTour ( )
107+
81108 // Update the stackable_guided_tour_states setting
82109 if ( ! guidedTourStates . includes ( tourId ) ) {
83110 // eslint-disable-next-line camelcase
@@ -98,6 +125,7 @@ const GuidedModalTour = props => {
98125
99126const ModalTour = props => {
100127 const {
128+ tourId,
101129 steps,
102130 onClose = NOOP ,
103131 hasConfetti = true ,
@@ -141,6 +169,22 @@ const ModalTour = props => {
141169 } , 50 )
142170 } , [ initialize ] )
143171
172+ // Set active tour when modal becomes visible
173+ useEffect ( ( ) => {
174+ if ( isVisible ) {
175+ setActiveTour ( tourId )
176+ }
177+ } , [ isVisible , tourId ] )
178+
179+ // Clear active tour when component unmounts
180+ useEffect ( ( ) => {
181+ return ( ) => {
182+ if ( getActiveTourId ( ) === tourId ) {
183+ clearActiveTour ( )
184+ }
185+ }
186+ } , [ tourId ] )
187+
144188 // While the modal is visible, just keep on force refreshing the modal in an interval to make sure the modal is always in the correct position.
145189 useEffect ( ( ) => {
146190 let interval
0 commit comments