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

Hide inserter pattern/media panel when there are no categories #66246

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import { Button, Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -24,6 +24,7 @@ function BlockPatternsTab( {
selectedCategory,
onInsert,
rootClientId,
onHasCategories,
children,
} ) {
const [ showPatternsExplorer, setShowPatternsExplorer ] = useState( false );
Expand All @@ -37,6 +38,10 @@ function BlockPatternsTab( {
[]
);

useEffect( () => {
onHasCategories( !! categories.length );
}, [ categories, onHasCategories ] );
Copy link
Member

Choose a reason for hiding this comment

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

The onHasCategories prop will have different references on each render, which makes it unsafe to pass it to the effect.

I think the only reason we're not getting an infinite loop is that the state is a boolean value.

Copy link
Member

Choose a reason for hiding this comment

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

Since we're close to the release, I took the liberty to fix this. I also changed the function name to just setHasCategories to better convey what it does. I hope it's okay for you, @talldan 🙇 !


if ( isResolvingPatterns ) {
return (
<div className="block-editor-inserter__patterns-loading">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __ } from '@wordpress/i18n';
import { useViewportMatch } from '@wordpress/compose';
import { Button } from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';
import { useCallback, useEffect, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -24,6 +24,7 @@ function MediaTab( {
rootClientId,
selectedCategory,
onSelectCategory,
onHasCategories,
onInsert,
children,
} ) {
Expand All @@ -49,6 +50,10 @@ function MediaTab( {
[ mediaCategories ]
);

useEffect( () => {
onHasCategories( !! categories.length );
}, [ categories, onHasCategories ] );

if ( ! categories.length ) {
return <InserterNoResults />;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ function InserterMenu(
const [ patternFilter, setPatternFilter ] = useState( 'all' );
const [ selectedMediaCategory, setSelectedMediaCategory ] =
useState( null );

const [ hasCategories, setHasCategories ] = useState( true );
function getInitialTab() {
if ( __experimentalInitialTab ) {
return __experimentalInitialTab;
Expand Down Expand Up @@ -146,10 +148,12 @@ function InserterMenu(

const showPatternPanel =
selectedTab === 'patterns' &&
hasCategories &&
! delayedFilterValue &&
!! selectedPatternCategory;

const showMediaPanel = selectedTab === 'media' && !! selectedMediaCategory;
const showMediaPanel =
selectedTab === 'media' && !! selectedMediaCategory && hasCategories;

const inserterSearch = useMemo( () => {
if ( selectedTab === 'media' ) {
Expand Down Expand Up @@ -242,6 +246,9 @@ function InserterMenu(
onInsert={ onInsertPattern }
onSelectCategory={ onClickPatternCategory }
selectedCategory={ selectedPatternCategory }
onHasCategories={ ( _hasCategories ) =>
setHasCategories( _hasCategories )
}
>
{ showPatternPanel && (
<PatternCategoryPreviews
Expand Down Expand Up @@ -270,6 +277,9 @@ function InserterMenu(
selectedCategory={ selectedMediaCategory }
onSelectCategory={ setSelectedMediaCategory }
onInsert={ onInsert }
onHasCategories={ ( _hasCategories ) =>
setHasCategories( _hasCategories )
}
>
{ showMediaPanel && (
<MediaCategoryPanel
Expand Down
Loading