@@ -7,7 +7,7 @@ import { createStyles } from 'antd-style';
77import { ArgsProps } from 'antd/lib/notification' ;
88import { atom , useAtomValue , useSetAtom } from 'jotai' ;
99import _ from 'lodash' ;
10- import { Key , ReactNode , useCallback , useEffect , useRef } from 'react' ;
10+ import React , { Key , ReactNode , useCallback , useEffect , useRef } from 'react' ;
1111import { useTranslation } from 'react-i18next' ;
1212import { To , createPath } from 'react-router-dom' ;
1313import { BAINodeNotificationItemFragment$key } from 'src/__generated__/BAINodeNotificationItemFragment.graphql' ;
@@ -289,23 +289,42 @@ export const useSetBAINotification = () => {
289289 const webuiNavigate = useWebUINavigate ( ) ;
290290 const { styles } = useStyle ( ) ;
291291
292- const destroyAllNotifications = useCallback ( ( ) => {
292+ const closeAllNotifications = useCallback ( ( ) => {
293293 _activeNotificationKeys . splice ( 0 , _activeNotificationKeys . length ) ;
294294 app . notification . destroy ( ) ;
295295 } , [ app . notification ] ) ;
296296
297+ /**
298+ * Function to permanently clear all notifications.
299+ */
297300 const clearAllNotifications = useCallback ( ( ) => {
298301 setNotifications ( [ ] ) ;
299- destroyAllNotifications ( ) ;
300- } , [ setNotifications , destroyAllNotifications ] ) ;
302+ closeAllNotifications ( ) ;
303+ } , [ setNotifications , closeAllNotifications ] ) ;
301304
302- const destroyNotification = useCallback (
305+ /**
306+ * Function to hide specific notification. It remains in the drawer.
307+ */
308+ const closeNotification = useCallback (
303309 ( key : React . Key ) => {
304310 app . notification . destroy ( key ) ;
305311 } ,
306312 [ app . notification ] ,
307313 ) ;
308314
315+ /**
316+ * Function to remove specific notification from the list and hide it.
317+ */
318+ const clearNotification = useCallback (
319+ ( key : React . Key ) => {
320+ setNotifications ( ( prev ) => {
321+ return prev . filter ( ( n ) => n . key !== key ) ;
322+ } ) ;
323+ closeNotification ( key ) ;
324+ } ,
325+ [ setNotifications , closeNotification ] ,
326+ ) ;
327+
309328 /**
310329 * Function to upsert a notification.
311330 * @param params - The parameters for the notification.
@@ -398,7 +417,7 @@ export const useSetBAINotification = () => {
398417 if ( newNotification . to ) {
399418 webuiNavigate ( newNotification . to ) ;
400419 }
401- destroyNotification ( newNotification . key ) ;
420+ closeNotification ( newNotification . key ) ;
402421 } }
403422 />
404423 ) ,
@@ -412,6 +431,12 @@ export const useSetBAINotification = () => {
412431 } ) ;
413432 if ( idx >= 0 ) {
414433 setNotifications ( ( prevList ) => {
434+ // check the notification is removed by clearNotification function. If so, do nothing.
435+ const exists = prevList . some (
436+ ( n ) => n . key === newNotification . key ,
437+ ) ;
438+ if ( ! exists ) return prevList ;
439+
415440 const newList = [ ...prevList ] ;
416441 newList [ idx ] = {
417442 ...newList [ idx ] ,
@@ -423,7 +448,7 @@ export const useSetBAINotification = () => {
423448 } ,
424449 } ) ;
425450 } else if ( newNotification . open === false && newNotification . key ) {
426- destroyNotification ( newNotification . key ) ;
451+ closeNotification ( newNotification . key ) ;
427452 }
428453 currentKey = newNotification . key ;
429454 return nextNotifications ;
@@ -435,7 +460,7 @@ export const useSetBAINotification = () => {
435460 [
436461 app . notification ,
437462 setNotifications ,
438- destroyNotification ,
463+ closeNotification ,
439464 desktopNotification ,
440465 ] ,
441466 ) ;
@@ -467,9 +492,10 @@ export const useSetBAINotification = () => {
467492
468493 return {
469494 upsertNotification,
495+ clearNotification,
470496 clearAllNotifications,
471- destroyNotification ,
472- destroyAllNotifications ,
497+ closeNotification ,
498+ closeAllNotifications ,
473499 } ;
474500} ;
475501
0 commit comments