From 3485cf8c3141417e7e26fd3eeb6c22e6e596041c Mon Sep 17 00:00:00 2001 From: Xavier Lozano Carreras Date: Fri, 9 Oct 2026 11:54:17 +0200 Subject: [PATCH 1/3] My Jetpack: Show the highest-priority notice first setNotice compared each call against the notice from the last render, so when several watchers set a notice in one effect pass the last caller won and the right notice only replaced it a render later. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QqfKRQfxuwm723Y1prq8mM --- .../_inc/context/notices/noticeContext.tsx | 27 ++++++------ .../notices/test/notice-context.test.tsx | 42 +++++++++++++++++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++ 3 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 projects/packages/my-jetpack/_inc/context/notices/test/notice-context.test.tsx create mode 100644 projects/packages/my-jetpack/changelog/fix-my-jetpack-notice-priority-stale-state diff --git a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx index 073e9f54f650..d521615eaa66 100644 --- a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx +++ b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx @@ -28,19 +28,22 @@ 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 ), + }; + + // Compare against the queued state: 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..c7d3a0a2cfd8 --- /dev/null +++ b/projects/packages/my-jetpack/_inc/context/notices/test/notice-context.test.tsx @@ -0,0 +1,42 @@ +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( 'keeps the higher-priority notice when a lower one is set later in the same effect pass', () => { + const rendered: string[] = []; + + const Watchers = () => { + const { setNotice } = useContext( NoticeContext ); + useEffect( () => { + setNotice( { + message: 'High', + options: { level: 'error', priority: NOTICE_PRIORITY_HIGH }, + } ); + setNotice( { + message: 'Medium', + options: { level: 'error', priority: NOTICE_PRIORITY_MEDIUM }, + } ); + }, [ setNotice ] ); + return null; + }; + + const CurrentNotice = () => { + const { currentNotice } = useContext( NoticeContext ); + if ( currentNotice.message ) { + rendered.push( String( currentNotice.message ) ); + } + return null; + }; + + render( + + + + + ); + + expect( rendered ).toEqual( [ 'High' ] ); + } ); +} ); 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..247671b78cd7 --- /dev/null +++ b/projects/packages/my-jetpack/changelog/fix-my-jetpack-notice-priority-stale-state @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Show the highest-priority notice first instead of briefly showing a lower-priority one. From 0d86ec3c3c6a3abf33fb79e106271098d2b2534a Mon Sep 17 00:00:00 2001 From: Xavier Lozano Carreras Date: Fri, 9 Oct 2026 12:32:44 +0200 Subject: [PATCH 2/3] My Jetpack: State the notice priority rule in setNotice's comment Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QqfKRQfxuwm723Y1prq8mM --- .../packages/my-jetpack/_inc/context/notices/noticeContext.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx index d521615eaa66..e39d03cb5842 100644 --- a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx +++ b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx @@ -34,7 +34,8 @@ const NoticeContextProvider = ( { children } ) => { notice.options?.onClose || ( ! notice.options?.hideCloseButton ? onClose : undefined ), }; - // Compare against the queued state: watchers call this several times in one effect pass. + // Replace the notice only if none is showing or the new one has a higher priority. 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 } From f9005a3b30ee39676151feecaff885d26d9083f5 Mon Sep 17 00:00:00 2001 From: Xavier Lozano Carreras Date: Tue, 13 Oct 2026 10:55:00 +0200 Subject: [PATCH 3/3] My Jetpack: Cover the notice priority tie, add plugin changelog entries Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QqfKRQfxuwm723Y1prq8mM --- .../_inc/context/notices/noticeContext.tsx | 5 +++-- .../notices/test/notice-context.test.tsx | 21 ++++++++++++++----- ...fix-my-jetpack-notice-priority-stale-state | 2 +- ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ ...fix-my-jetpack-notice-priority-stale-state | 4 ++++ 11 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 projects/plugins/backup/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/boost/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/jetpack/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/protect/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/search/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/social/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/stats/changelog/fix-my-jetpack-notice-priority-stale-state create mode 100644 projects/plugins/videopress/changelog/fix-my-jetpack-notice-priority-stale-state diff --git a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx index e39d03cb5842..8a4826ff03d8 100644 --- a/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx +++ b/projects/packages/my-jetpack/_inc/context/notices/noticeContext.tsx @@ -34,8 +34,9 @@ const NoticeContextProvider = ( { children } ) => { notice.options?.onClose || ( ! notice.options?.hideCloseButton ? onClose : undefined ), }; - // Replace the notice only if none is showing or the new one has a higher priority. Read the - // queued state, since watchers call this several times in one effect pass. + // 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 } 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 index c7d3a0a2cfd8..9dbbcb20fcf2 100644 --- 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 @@ -4,19 +4,30 @@ import { NOTICE_PRIORITY_HIGH, NOTICE_PRIORITY_MEDIUM } from '../../constants'; import NoticeContextProvider, { NoticeContext } from '../noticeContext'; describe( 'NoticeContextProvider', () => { - it( 'keeps the higher-priority notice when a lower one is set later in the same effect pass', () => { + 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: 'High', + message: 'First', options: { level: 'error', priority: NOTICE_PRIORITY_HIGH }, } ); setNotice( { - message: 'Medium', - options: { level: 'error', priority: NOTICE_PRIORITY_MEDIUM }, + message: 'Second', + options: { level: 'error', priority: secondPriority }, } ); }, [ setNotice ] ); return null; @@ -37,6 +48,6 @@ describe( 'NoticeContextProvider', () => { ); - expect( rendered ).toEqual( [ 'High' ] ); + 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 index 247671b78cd7..31b55cc0c157 100644 --- 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 @@ -1,4 +1,4 @@ Significance: patch Type: fixed -Show the highest-priority notice first instead of briefly showing a lower-priority one. +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.