Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns: Fix missing custom patterns in patterns explorer #51889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function BlockPatternList( {
return isShown ? (
<BlockPattern
key={
// User added unsynced patterns do not have a unique name so we use the id instead.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in this file are copied from #51877 to make testing easier.

pattern.name === 'core/block'
? pattern.id
: pattern.name
Expand All @@ -150,7 +151,13 @@ function BlockPatternList( {
showTooltip={ showTitlesAsTooltip }
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
<BlockPatternPlaceholder
key={
pattern.name === 'core/block'
? pattern.id
: pattern.name
}
/>
);
} ) }
</Composite>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import BlockPatternsList from '../../block-patterns-list';
import InserterNoResults from '../no-results';
import useInsertionPoint from '../hooks/use-insertion-point';
import usePatternsState from '../hooks/use-patterns-state';
import useUnsyncedPatterns from '../hooks/use-unsynced-patterns';
import InserterListbox from '../../inserter-listbox';
import { searchItems } from '../search-items';

Expand Down Expand Up @@ -52,6 +53,13 @@ function PatternList( { filterValue, selectedCategory, patternCategories } ) {
onInsertBlocks,
destinationRootClientId
);

const filteredUnsyncedPatterns = useUnsyncedPatterns(
destinationRootClientId,
onInsertBlocks,
true
);

const registeredPatternCategories = useMemo(
() =>
patternCategories.map(
Expand Down Expand Up @@ -91,11 +99,18 @@ function PatternList( { filterValue, selectedCategory, patternCategories } ) {
debouncedSpeak( resultsFoundMessage );
}, [ filterValue, debouncedSpeak ] );

const currentShownPatterns = useAsyncList( filteredBlockPatterns, {
const blockPatterns = useAsyncList( filteredBlockPatterns, {
step: INITIAL_INSERTER_RESULTS,
} );

const hasItems = !! filteredBlockPatterns?.length;
const blockPatternsUnsynced = useAsyncList( filteredUnsyncedPatterns, {
step: INITIAL_INSERTER_RESULTS,
} );

const currentShownPatterns =
selectedCategory === 'reusable' ? blockPatternsUnsynced : blockPatterns;

const hasItems = !! currentShownPatterns?.length;
return (
<div className="block-editor-block-patterns-explorer__list">
{ hasItems && (
Expand All @@ -109,7 +124,7 @@ function PatternList( { filterValue, selectedCategory, patternCategories } ) {
{ hasItems && (
<BlockPatternsList
shownPatterns={ currentShownPatterns }
blockPatterns={ filteredBlockPatterns }
blockPatterns={ currentShownPatterns }
onClickPattern={ onSelectBlockPattern }
isDraggable={ false }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
Button,
} from '@wordpress/components';
import { Icon, chevronRight, chevronLeft } from '@wordpress/icons';
import { parse } from '@wordpress/blocks';
import { focus } from '@wordpress/dom';

/**
Expand All @@ -28,7 +27,7 @@ import usePatternsState from './hooks/use-patterns-state';
import BlockPatternList from '../block-patterns-list';
import PatternsExplorerModal from './block-patterns-explorer/explorer';
import MobileTabNavigation from './mobile-tab-navigation';
import useBlockTypesState from './hooks/use-block-types-state';
import useUnsyncedPatterns from './hooks/use-unsynced-patterns';

const noop = () => {};

Expand All @@ -51,18 +50,8 @@ function usePatternsCategories( rootClientId ) {
rootClientId
);

const [ unsyncedPatterns ] = useBlockTypesState(
rootClientId,
undefined,
'unsynced'
);
const filteredUnsyncedPatterns = useUnsyncedPatterns( rootClientId );

const filteredUnsyncedPatterns = useMemo( () => {
return unsyncedPatterns.filter(
( { category: unsyncedPatternCategory } ) =>
unsyncedPatternCategory === 'reusable'
);
}, [ unsyncedPatterns ] );
const hasRegisteredCategory = useCallback(
( pattern ) => {
if ( ! pattern.categories || ! pattern.categories.length ) {
Expand Down Expand Up @@ -169,24 +158,11 @@ export function BlockPatternsCategoryPanel( {
onInsert,
rootClientId
);
const [ unsyncedPatterns ] = useBlockTypesState(
const filteredUnsyncedPatterns = useUnsyncedPatterns(
rootClientId,
onInsert,
'unsynced'
true
);
const filteredUnsyncedPatterns = useMemo( () => {
return unsyncedPatterns
.filter(
( { category: unsyncedPatternCategory } ) =>
unsyncedPatternCategory === 'reusable'
)
.map( ( syncedPattern ) => ( {
...syncedPattern,
blocks: parse( syncedPattern.content, {
__unstableSkipMigrationLogs: true,
} ),
} ) );
}, [ unsyncedPatterns ] );

const availableCategories = usePatternsCategories( rootClientId );
const currentCategoryPatterns = useMemo(
Expand Down Expand Up @@ -214,7 +190,14 @@ export function BlockPatternsCategoryPanel( {
category.name === 'reusable'
? filteredUnsyncedPatterns
: currentCategoryPatterns;
const currentShownPatterns = useAsyncList( patterns );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did have a single unsynced list here but that caused a lot of problems with list placeholders being left in place when switching between unsynched and other categories

const unsyncedPatternsList = useAsyncList( filteredUnsyncedPatterns );
const categoryPatternsList = useAsyncList( currentCategoryPatterns );

const currentShownPatterns =
category.name === 'reusable'
? unsyncedPatternsList
: categoryPatternsList;

// Hide block pattern preview on unmount.
useEffect( () => () => onHover( null ), [] );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { parse } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import useBlockTypesState from '../hooks/use-block-types-state';

export default function useUnsyncedPatterns(
rootClientId,
onInsert,
withBlocks
) {
const [ unsyncedPatterns ] = useBlockTypesState(
rootClientId,
onInsert,
'unsynced'
);
const filteredUnsyncedPatterns = useMemo( () => {
return unsyncedPatterns
.filter(
( { category: unsyncedPatternCategory } ) =>
unsyncedPatternCategory === 'reusable'
)
.map( ( syncedPattern ) => ( {
...syncedPattern,
blocks: withBlocks
? parse( syncedPattern.content, {
__unstableSkipMigrationLogs: true,
} )
: undefined,
} ) );
}, [ unsyncedPatterns, withBlocks ] );
return filteredUnsyncedPatterns;
}