Skip to content

Commit

Permalink
show media tab if enableOpenverseMediaCategory setting is true
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Feb 21, 2023
1 parent b9d8337 commit 545af30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 39 deletions.
4 changes: 0 additions & 4 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import BlockListAppender from '../block-list-appender';
import { useInBetweenInserter } from './use-in-between-inserter';
import { store as blockEditorStore } from '../../store';
import { usePreParsePatterns } from '../../utils/pre-parse-patterns';
import { useCheckEmptyMediaCategories } from '../inserter/media-tab/hooks';
import { LayoutProvider, defaultLayout } from './layout';
import BlockToolsBackCompat from '../block-tools/back-compat';
import { useBlockSelectionClearer } from '../block-selection-clearer';
Expand Down Expand Up @@ -127,9 +126,6 @@ function Root( { className, ...settings } ) {

export default function BlockList( settings ) {
usePreParsePatterns();
// We need to check if there is at least one item for each media category before
// opening the inserter, in order to avoid late rendering of the media tab.
useCheckEmptyMediaCategories();
return (
<BlockToolsBackCompat>
<BlockEditContextProvider value={ DEFAULT_BLOCK_EDIT_CONTEXT }>
Expand Down
21 changes: 0 additions & 21 deletions packages/block-editor/src/components/inserter/media-tab/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,3 @@ export function useMediaCategories( rootClientId ) {
] );
return categories;
}

export function useCheckEmptyMediaCategories() {
const isPreviewMode = useSelect(
( select ) =>
select( blockEditorStore ).getSettings().__unstableIsPreviewMode,
[]
);
const inserterMediaCategories = useInserterMediaCategories();
useEffect( () => {
if ( isPreviewMode || ! inserterMediaCategories ) {
return;
}
// Loop through categories to check if they have at least one media item.
inserterMediaCategories.forEach( ( category ) => {
// Some sources are external and we don't need to make a request.
if ( ! category.isExternalResource ) {
category.fetch( { per_page: 1 } );
}
} );
}, [ isPreviewMode, inserterMediaCategories ] );
}
34 changes: 20 additions & 14 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,33 @@ function InserterMenu(
insertionIndex: __experimentalInsertionIndex,
shouldFocusBlock,
} );
const { showPatterns, inserterItems } = useSelect(
( select ) => {
const { __experimentalGetAllowedPatterns, getInserterItems } =
select( blockEditorStore );
return {
showPatterns: !! __experimentalGetAllowedPatterns(
destinationRootClientId
).length,
inserterItems: getInserterItems( destinationRootClientId ),
};
},
[ destinationRootClientId ]
);
const { showPatterns, inserterItems, enableOpenverseMediaCategory } =
useSelect(
( select ) => {
const {
__experimentalGetAllowedPatterns,
getInserterItems,
getSettings,
} = select( blockEditorStore );
return {
showPatterns: !! __experimentalGetAllowedPatterns(
destinationRootClientId
).length,
inserterItems: getInserterItems( destinationRootClientId ),
enableOpenverseMediaCategory:
getSettings().enableOpenverseMediaCategory,
};
},
[ destinationRootClientId ]
);
const hasReusableBlocks = useMemo( () => {
return inserterItems.some(
( { category } ) => category === 'reusable'
);
}, [ inserterItems ] );

const mediaCategories = useMediaCategories( destinationRootClientId );
const showMedia = !! mediaCategories.length;
const showMedia = !! mediaCategories.length || enableOpenverseMediaCategory;

const onInsert = useCallback(
( blocks, meta, shouldForceFocusBlock ) => {
Expand Down

0 comments on commit 545af30

Please sign in to comment.