Add a workaround for replacing core notice Site updated

I considered doing this using a filter 'i18n.gettext', but
with this approach, we would just replace the text. The notice also
contained an action with a link to the site's homepage, so I chose this mechanism instead.
Let's see if there will be more use-cases. We can eventually move it somewhere into the store
and proxy all messages via our store.
[MAILPOET-6336]
This commit is contained in:
Rostislav Wolny
2024-12-06 11:06:00 +01:00
committed by Rostislav Wolný
parent 2334805b95
commit 411943f39b

View File

@ -1,4 +1,6 @@
import { useMemo } from '@wordpress/element';
import { SnackbarList } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
@ -12,11 +14,33 @@ export function EditorSnackbars( { context = 'email-editor' } ) {
[ context ]
);
// Some global notices are not suitable for the email editor context
// This map allows us to change the content of the notice
const globalNoticeChangeMap = useMemo( () => {
return {
'site-editor-save-success': {
content: __( 'Template updated.', 'mailpoet' ),
removeActions: true,
},
};
}, [] );
const { removeNotice } = useDispatch( noticesStore );
const snackbarNotices = notices.filter(
( { type } ) => type === 'snackbar'
);
const snackbarNotices = notices
.filter( ( { type } ) => type === 'snackbar' )
.map( ( notice ) => {
if ( ! globalNoticeChangeMap[ notice.id ] ) {
return notice;
}
return {
...notice,
content: globalNoticeChangeMap[ notice.id ].content,
actions: globalNoticeChangeMap[ notice.id ].removeActions
? []
: notice.actions,
};
} );
return (
<SnackbarList