1- import React , { useCallback , useState } from 'react' ;
1+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
22import {
33 Box ,
44 ButtonIcon ,
@@ -12,14 +12,12 @@ import {
1212 BorderRadius ,
1313 Display ,
1414 IconColor ,
15- Severity ,
1615 TextColor ,
1716 TextVariant ,
1817} from '../../../../helpers/constants/design-system' ;
1918import { useI18nContext } from '../../../../hooks/useI18nContext' ;
2019import useAlerts from '../../../../hooks/useAlerts' ;
2120import { AlertModal } from '../alert-modal' ;
22- import { Alert } from '../../../../ducks/confirm-alerts/confirm-alerts' ;
2321
2422export type MultipleAlertModalProps = {
2523 /** The key of the initial alert to display. */
@@ -168,7 +166,6 @@ export function MultipleAlertModal({
168166 const {
169167 alerts : alertsFromHook ,
170168 fieldAlerts : fieldAlertsFromHook ,
171- isAlertConfirmed,
172169 navigableAlerts : navigableAlertsFromHook ,
173170 navigableFieldAlerts : navigableFieldAlertsFromHook ,
174171 } = useAlerts ( ownerId ) ;
@@ -192,6 +189,82 @@ export function MultipleAlertModal({
192189 initialAlertKey ,
193190 ) ;
194191
192+ const previousAlertKeyRef = useRef < string | undefined > ( ) ;
193+ const pendingAlertKeyRef = useRef < string | undefined > ( ) ;
194+
195+ useEffect ( ( ) => {
196+ const alertKeyExists = alertKey
197+ ? alertsToDisplay . some ( ( alert ) => alert . key === alertKey )
198+ : false ;
199+
200+ if ( alertKey === previousAlertKeyRef . current ) {
201+ return ;
202+ }
203+
204+ previousAlertKeyRef . current = alertKey ;
205+
206+ if ( alertKeyExists && alertKey ) {
207+ if ( alertKey !== currentAlertKey ) {
208+ setCurrentAlertKey ( alertKey ) ;
209+ }
210+ pendingAlertKeyRef . current = undefined ;
211+ } else {
212+ pendingAlertKeyRef . current = alertKey ?? undefined ;
213+ }
214+ } , [ alertKey , alertsToDisplay , currentAlertKey ] ) ;
215+
216+ useEffect ( ( ) => {
217+ const pendingAlertKey = pendingAlertKeyRef . current ;
218+
219+ if ( ! pendingAlertKey ) {
220+ return ;
221+ }
222+
223+ const pendingAlertExists = alertsToDisplay . some (
224+ ( alert ) => alert . key === pendingAlertKey ,
225+ ) ;
226+
227+ if ( ! pendingAlertExists ) {
228+ return ;
229+ }
230+
231+ if ( pendingAlertKey !== currentAlertKey ) {
232+ setCurrentAlertKey ( pendingAlertKey ) ;
233+ }
234+ pendingAlertKeyRef . current = undefined ;
235+ } , [ alertsToDisplay , currentAlertKey ] ) ;
236+
237+ useEffect ( ( ) => {
238+ const currentAlertStillExists = currentAlertKey
239+ ? alertsToDisplay . some ( ( alert ) => alert . key === currentAlertKey )
240+ : false ;
241+ const alertKeyExists = alertKey
242+ ? alertsToDisplay . some ( ( alert ) => alert . key === alertKey )
243+ : false ;
244+ const pendingAlertKey = pendingAlertKeyRef . current ;
245+ const pendingAlertExists = pendingAlertKey
246+ ? alertsToDisplay . some ( ( alert ) => alert . key === pendingAlertKey )
247+ : false ;
248+
249+ if ( currentAlertStillExists ) {
250+ return ;
251+ }
252+
253+ const fallbackKey =
254+ ( alertKeyExists && alertKey ) ??
255+ ( pendingAlertExists && pendingAlertKey ) ??
256+ navigableAlertsToDisplay [ 0 ] ?. key ??
257+ alertsToDisplay [ 0 ] ?. key ;
258+
259+ if ( fallbackKey !== currentAlertKey ) {
260+ setCurrentAlertKey ( fallbackKey ) ;
261+ }
262+
263+ if ( pendingAlertExists && fallbackKey === pendingAlertKey ) {
264+ pendingAlertKeyRef . current = undefined ;
265+ }
266+ } , [ alertKey , alertsToDisplay , navigableAlertsToDisplay , currentAlertKey ] ) ;
267+
195268 const selectedAlert =
196269 alertsToDisplay . find ( ( alert ) => alert . key === currentAlertKey ) ??
197270 alertsToDisplay [ 0 ] ;
@@ -200,11 +273,6 @@ export function MultipleAlertModal({
200273 ( alert ) => alert . key === ( selectedAlert ?. key ?? currentAlertKey ) ,
201274 ) ;
202275
203- const hasUnconfirmedAlerts = alerts . some (
204- ( alert : Alert ) =>
205- ! isAlertConfirmed ( alert . key ) && alert . severity === Severity . Danger ,
206- ) ;
207-
208276 const handleBackButtonClick = useCallback ( ( ) => {
209277 const activeAlertKey = selectedAlert ?. key ?? currentAlertKey ;
210278 const newIndex = navigableAlertsToDisplay . findIndex (
@@ -232,58 +300,8 @@ export function MultipleAlertModal({
232300 } , [ currentAlertKey , navigableAlertsToDisplay , selectedAlert ] ) ;
233301
234302 const handleAcknowledgeClick = useCallback ( ( ) => {
235- const activeAlertKey = selectedAlert ?. key ?? currentAlertKey ;
236- const activeAlertHidesNavigation =
237- selectedAlert ?. hideFromAlertNavigation === true ;
238-
239- if ( skipAlertNavigation || ! activeAlertKey ) {
240- onFinalAcknowledgeClick ( ) ;
241- return ;
242- }
243-
244- if ( activeAlertHidesNavigation ) {
245- onFinalAcknowledgeClick ( ) ;
246- return ;
247- }
248-
249- const navigableIndex = navigableAlertsToDisplay . findIndex (
250- ( alert ) => alert . key === activeAlertKey ,
251- ) ;
252-
253- if ( navigableIndex === - 1 ) {
254- onFinalAcknowledgeClick ( ) ;
255- return ;
256- }
257-
258- const isLastNavigableAlert =
259- navigableIndex === navigableAlertsToDisplay . length - 1 ;
260-
261- if ( isLastNavigableAlert ) {
262- if ( ! hasUnconfirmedAlerts ) {
263- onFinalAcknowledgeClick ( ) ;
264- return ;
265- }
266-
267- const firstNavigableKey = navigableAlertsToDisplay [ 0 ] ?. key ;
268- if ( firstNavigableKey ) {
269- setCurrentAlertKey ( firstNavigableKey ) ;
270- }
271- onFinalAcknowledgeClick ( ) ;
272- return ;
273- }
274-
275- const nextAlertKey = navigableAlertsToDisplay [ navigableIndex + 1 ] ?. key ;
276- if ( nextAlertKey ) {
277- setCurrentAlertKey ( nextAlertKey ) ;
278- }
279- } , [
280- currentAlertKey ,
281- onFinalAcknowledgeClick ,
282- hasUnconfirmedAlerts ,
283- navigableAlertsToDisplay ,
284- selectedAlert ,
285- skipAlertNavigation ,
286- ] ) ;
303+ onFinalAcknowledgeClick ( ) ;
304+ } , [ onFinalAcknowledgeClick ] ) ;
287305
288306 const showNavigationButtons =
289307 ! skipAlertNavigation &&
0 commit comments