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

Show uncategorized patterns on the Editor > Patterns page #52633

Merged
merged 5 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 12 additions & 5 deletions packages/edit-site/src/components/page-patterns/patterns-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,18 @@ export default function PatternsList( { categoryId, type } ) {

const [ syncFilter, setSyncFilter ] = useState( 'all' );
const deferredSyncedFilter = useDeferredValue( syncFilter );
const { patterns, isResolving } = usePatterns( type, categoryId, {
search: deferredFilterValue,
syncStatus:
deferredSyncedFilter === 'all' ? undefined : deferredSyncedFilter,
} );

const { patterns, isResolving } = usePatterns(
type,
categoryId !== 'uncategorized' ? categoryId : '',
{
search: deferredFilterValue,
syncStatus:
deferredSyncedFilter === 'all'
? undefined
: deferredSyncedFilter,
}
);

const id = useId();
const titleId = `${ id }-title`;
Expand Down
17 changes: 12 additions & 5 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,18 @@ const selectThemePatterns = ( select, { categoryId, search = '' } = {} ) => {
blocks: parse( pattern.content ),
} ) );

patterns = searchItems( patterns, search, {
categoryId,
hasCategory: ( item, currentCategory ) =>
item.categories?.includes( currentCategory ),
} );
if ( categoryId ) {
patterns = searchItems( patterns, search, {
categoryId,
hasCategory: ( item, currentCategory ) =>
item.categories?.includes( currentCategory ),
} );
} else {
// If there is no category id, select uncategorized patterns.
patterns = patterns.filter(
( item ) => ! item.hasOwnProperty( 'categories' )
Copy link
Contributor

Choose a reason for hiding this comment

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

The uncategorized patterns would still need to be filtered via the search term correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right, let me see if I can fix that 🤔

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 still don't understand why the search was limited to searching inside categories instead of across all.

);
}

return { patterns, isResolving: false };
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -11,6 +12,10 @@ import useThemePatterns from './use-theme-patterns';

export default function usePatternCategories() {
const defaultCategories = useDefaultPatternCategories();
defaultCategories.push( {
name: 'uncategorized',
label: __( 'Uncategorized' ),
} );
Comment on lines +15 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

The "uncategorized" category for template parts was changed to "General" as that was the previous labelling. In that feedback, it was mentioned that nowhere else in the UI used "Uncategorized", we might need to change it here as well.

cc/ @SaxonF

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uncategorized is used in several places. For example, when a post does not have a category. It is not a term that is unfamiliar.

But I choose it here over "General" because "Uncategorized" is what is used in the pattern inserter.
I still feel that until these two interfaces "blend together" more and feel like one interface, the categories and the order the patterns are displayed in should match:

pattern inserter pattern explorer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It made sense to use General for template parts, because that is what is used in the block editor UI for template parts.

const themePatterns = useThemePatterns();

const patternCategories = useMemo( () => {
Expand All @@ -31,6 +36,10 @@ export default function usePatternCategories() {
categoryMap[ category ].count += 1;
}
} );
// If the pattern has no categories, add it to uncategorized.
if ( ! pattern.categories?.length ) {
categoryMap.uncategorized.count += 1;
}
} );

// Filter categories so we only have those containing patterns.
Expand Down
Loading