Skip to content

Commit 49d2a97

Browse files
fix: enhance inserter notice handling for block patterns (#127)
1 parent 700d945 commit 49d2a97

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

‎assets/js/src/components/DesignPackNotice.js‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { useSelect } from '@wordpress/data';
33
import { Icon, closeSmall } from '@wordpress/icons';
44
import { useEffect, useState } from '@wordpress/element';
5+
import { __ } from '@wordpress/i18n';
56

67
const logo = (
78
<svg
@@ -20,17 +21,41 @@ const logo = (
2021
</svg>
2122
);
2223

24+
// WP < 6.8 id; 6.8+ shares 'inserter-notice' with other inserter snackbars.
2325
const BLOCK_PATTERN_INSERTED_NOTICE = 'block-pattern-inserted-notice';
26+
const INSERTER_NOTICE = 'inserter-notice';
27+
28+
const isPatternInsertedNotice = ( notice ) => {
29+
if ( notice.id === BLOCK_PATTERN_INSERTED_NOTICE ) {
30+
return true;
31+
}
32+
33+
if ( notice.id !== INSERTER_NOTICE || typeof notice.content !== 'string' ) {
34+
return false;
35+
}
36+
37+
// Core's wording before and after its rename (6.9 → 7.1).
38+
return [
39+
/* translators: %s: block pattern title. */
40+
__( 'Block pattern "%s" inserted.' ),
41+
/* translators: %s: block pattern title. */
42+
__( 'Pattern "%s" inserted.' ),
43+
].some( ( template ) => {
44+
const [ before, after = '' ] = template.split( /%(?:\d+\$)?s/ );
45+
46+
return (
47+
notice.content.startsWith( before ) &&
48+
notice.content.endsWith( after )
49+
);
50+
} );
51+
};
52+
2453
export default () => {
2554
const [ dismissed, setDismissed ] = useState( false );
2655
const [ noticeTriggered, setNoticeTriggered ] = useState( false );
2756

28-
const patternInserted = useSelect(
29-
( select ) =>
30-
select( 'core/notices' )
31-
.getNotices()
32-
.filter( ( n ) => n.id === BLOCK_PATTERN_INSERTED_NOTICE )
33-
.length > 0
57+
const patternInserted = useSelect( ( select ) =>
58+
select( 'core/notices' ).getNotices().some( isPatternInsertedNotice )
3459
);
3560

3661
useEffect( () => {

0 commit comments

Comments
 (0)