diff --git a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx index 073e9f54f650..8a4826ff03d8 100644 --- a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx +++ b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx @@ -28,19 +28,24 @@ const NoticeContextProvider = ( { children } ) => { const setNotice = useCallback( // If onClose is not provided in the "notice", and close button is not hidden, use the custom onClose function ( notice: Notice, onClose?: NoticeOptions[ 'onClose' ] ) => { - // Only update notice if there is not already a notice or the new notice has a higher priority - if ( ! currentNotice.message || notice.options.priority > currentNotice.options.priority ) { - const newOptions = { - ...notice.options, - onClose: - notice.options?.onClose || ( ! notice.options?.hideCloseButton ? onClose : undefined ), - }; - - resetNotice(); - setCurrentNotice( { ...notice, options: newOptions } ); - } + const newOptions = { + ...notice.options, + onClose: + notice.options?.onClose || ( ! notice.options?.hideCloseButton ? onClose : undefined ), + }; + + // Replace the notice only if none is showing or the new one has a higher priority, so on a tie + // the watcher that runs first keeps it. Read the queued state, since watchers call this several + // times in one effect pass. + setCurrentNotice( prev => + ! prev.message || notice.options.priority > prev.options.priority + ? { ...notice, options: newOptions } + : prev + ); }, - [ currentNotice.message, currentNotice.options.priority, resetNotice ] + // Changing identity on each notice change re-runs the watchers, e.g. to show the next notice after a close. + // eslint-disable-next-line react-hooks/exhaustive-deps + [ currentNotice.message, currentNotice.options.priority ] ); return ( diff --git a/projects/packages/my-jetpack/_inc/context/notices/test/notice-context.test.tsx b/projects/packages/my-jetpack/_inc/context/notices/test/notice-context.test.tsx new file mode 100644 index 000000000000..9dbbcb20fcf2 --- /dev/null +++ b/projects/packages/my-jetpack/_inc/context/notices/test/notice-context.test.tsx @@ -0,0 +1,53 @@ +import { render } from '@testing-library/react'; +import { useContext, useEffect } from 'react'; +import { NOTICE_PRIORITY_HIGH, NOTICE_PRIORITY_MEDIUM } from '../../constants'; +import NoticeContextProvider, { NoticeContext } from '../noticeContext'; + +describe( 'NoticeContextProvider', () => { + it.each( [ + [ + 'keeps the higher-priority notice when a lower one follows', + NOTICE_PRIORITY_MEDIUM, + [ 'First' ], + ], + [ + 'keeps the first notice when an equal-priority one follows', + NOTICE_PRIORITY_HIGH, + [ 'First' ], + ], + ] )( '%s in the same effect pass', ( _, secondPriority, expected ) => { + const rendered: string[] = []; + + const Watchers = () => { + const { setNotice } = useContext( NoticeContext ); + useEffect( () => { + setNotice( { + message: 'First', + options: { level: 'error', priority: NOTICE_PRIORITY_HIGH }, + } ); + setNotice( { + message: 'Second', + options: { level: 'error', priority: secondPriority }, + } ); + }, [ setNotice ] ); + return null; + }; + + const CurrentNotice = () => { + const { currentNotice } = useContext( NoticeContext ); + if ( currentNotice.message ) { + rendered.push( String( currentNotice.message ) ); + } + return null; + }; + + render( + + + + + ); + + expect( rendered ).toEqual( expected ); + } ); +} ); diff --git a/projects/packages/my-jetpack/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/packages/my-jetpack/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..31b55cc0c157 --- /dev/null +++ b/projects/packages/my-jetpack/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Notices: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/backup/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/backup/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/backup/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/boost/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/boost/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/boost/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/jetpack/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/jetpack/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..be46cc23d333 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/protect/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/protect/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/protect/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/search/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/search/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/search/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/social/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/social/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/social/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/stats/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/stats/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/stats/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once. diff --git a/projects/plugins/videopress/changelog/fix-my-jetpack-notice-priority-stale-state b/projects/plugins/videopress/changelog/fix-my-jetpack-notice-priority-stale-state new file mode 100644 index 000000000000..1d7ae1238d22 --- /dev/null +++ b/projects/plugins/videopress/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +My Jetpack: Show the highest-priority notice first when several are set at once.