Skip to content

Commit

Permalink
Add notice priorities to my jetpack notices (Automattic#36438)
Browse files Browse the repository at this point in the history
* Add priority notices to My Jetpack

* Add comment to clarify condition

* changelog
  • Loading branch information
CodeyGuyDylan authored Mar 20, 2024
1 parent c01fa06 commit 83a4d7c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions projects/packages/my-jetpack/_inc/context/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NOTICE_PRIORITY_LOW = 100;
export const NOTICE_PRIORITY_MEDIUM = 200;
export const NOTICE_PRIORITY_HIGH = 300;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const defaultNotice: Notice = {
options: {
status: '',
},
priority: 0,
};

export const NoticeContext = createContext< NoticeContextType >( {
Expand All @@ -19,7 +20,8 @@ const NoticeContextProvider = ( { children } ) => {
const [ currentNotice, setCurrentNotice ] = useState< Notice >( defaultNotice );

const setNotice = ( notice: Notice ) => {
if ( ! currentNotice.message ) {
// Only update notice if there is not already a notice or the new notice has a higher priority
if ( ! currentNotice.message || notice.priority > currentNotice.priority ) {
setCurrentNotice( notice );
}
};
Expand Down
1 change: 1 addition & 0 deletions projects/packages/my-jetpack/_inc/context/notices/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Notice = {
noDefaultClasses?: boolean;
}[];
};
priority: number;
};

export type NoticeContextType< T = Notice > = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { __, sprintf } from '@wordpress/i18n';
import { useContext, useEffect } from 'react';
import { NOTICE_PRIORITY_LOW } from '../../context/constants';
import { NoticeContext } from '../../context/notices/noticeContext';
import {
QUERY_ACTIVATE_PRODUCT_KEY,
Expand Down Expand Up @@ -38,6 +39,7 @@ export const useFetchingErrorNotice = ( { infoName, isError, overrideMessage }:
setNotice( {
message,
options: { status: 'error' },
priority: NOTICE_PRIORITY_LOW,
} );
}
}, [ message, setNotice, isError, infoName ] );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { __, sprintf } from '@wordpress/i18n';
import { useEffect, useContext } from 'react';
import { MyJetpackRoutes } from '../../constants';
import { NOTICE_PRIORITY_HIGH } from '../../context/constants';
import { NoticeContext } from '../../context/notices/noticeContext';
import { useAllProducts } from '../../data/products/use-product';
import getProductSlugsThatRequireUserConnection from '../../data/utils/get-product-slugs-that-require-user-connection';
Expand Down Expand Up @@ -54,6 +55,7 @@ export default function useConnectionWatcher() {
},
],
},
priority: NOTICE_PRIORITY_HIGH,
} );
}
}, [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Add notice priorities to My Jetpack

0 comments on commit 83a4d7c

Please sign in to comment.