Skip to content
Closed
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 @@ -135,9 +135,12 @@ function BlockPatternList( {
>
{ blockPatterns.map( ( pattern ) => {
const isShown = shownPatterns.includes( pattern );
// User added unsynced patterns do not have a unique name so we use the id instead.
const key =
pattern.name === 'core/block' ? pattern.id : pattern.name;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this component is BlockPatternsList is supposed to render patterns and patterns are expected to have unique names, so I think a better fix would be on the "caller" of this component, in the place where we transform "reusable blocks" to "patterns".

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing that means useUnsyncedPatterns

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 included this change in this alternative PR

return isShown ? (
<BlockPattern
key={ pattern.name }
key={ key }
pattern={ pattern }
onClick={ onClickPattern }
onHover={ onHover }
Expand All @@ -146,7 +149,7 @@ function BlockPatternList( {
showTooltip={ showTitlesAsTooltip }
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
<BlockPatternPlaceholder key={ key } />
);
} ) }
</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 );

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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it a bit weird, to have useBlockTypesState responsible for returning the "unsynced" patterns? Should we move these to usePatternsState instead? which would probably ensure that we don't need a special case for unsynced pattern and the useUnsyncedPatterns hook is not needed anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sense, but is a reasonable refactor, so have done it as a separate PR here.

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;
}