import type { MouseEvent, ReactNode } from 'react'; import clsx from 'clsx'; import { Placeholder, Spinner as WCSpinner, ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; import { InspectorControls, useBlockProps, RichText, } from '@wordpress/block-editor'; import { decodeEntities } from '@wordpress/html-entities'; import { __, sprintf } from '@wordpress/i18n'; import { pin } from '@wordpress/icons'; import { useEntityRecords } from '@wordpress/core-data'; import type { Taxonomy, Term } from '@wordpress/core-data'; import { useDispatch } from '@wordpress/data'; import { SelectControl, VisuallyHidden } from '@wordpress/ui'; import { store as noticeStore } from '@wordpress/notices'; import type { BlockEditProps } from '@wordpress/blocks'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import type { CategoriesAttributes } from './types'; export default function CategoriesEdit( { attributes: { displayAsDropdown, showHierarchy, showPostCounts, showOnlyTopLevel, showEmpty, label, showLabel, taxonomy: taxonomySlug, }, setAttributes, className, clientId, }: BlockEditProps< CategoriesAttributes > & { className?: string } ) { const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' ); const { records: allTaxonomies, // @ts-expect-error Bug: `useEntityRecords` returns `isResolving`, so this is always `undefined`. isResolvingTaxonomies, } = useEntityRecords< Taxonomy >( 'root', 'taxonomy', { per_page: -1 } ); const taxonomies = allTaxonomies?.filter( ( t ) => t.visibility.public ); const taxonomyItems = ( taxonomies ?? [] ).map( ( t ) => ( { label: t.name, value: t.slug, } ) ); const taxonomy = taxonomies?.find( ( t ) => t.slug === taxonomySlug ); const isHierarchicalTaxonomy = ! isResolvingTaxonomies && taxonomy?.hierarchical; const query: Record< string, unknown > = { per_page: -1, hide_empty: ! showEmpty, context: 'view', }; if ( isHierarchicalTaxonomy && showOnlyTopLevel ) { query.parent = 0; } const { records: categories, isResolving } = useEntityRecords< Term >( 'taxonomy', taxonomySlug, query ); const { createWarningNotice } = useDispatch( noticeStore ); const showRedirectionPreventedNotice = ( event: MouseEvent< HTMLAnchorElement > ) => { event.preventDefault(); createWarningNotice( __( 'Links are disabled in the editor.' ), { id: `block-library/core/categories/redirection-prevented/${ clientId }`, type: 'snackbar', } ); }; const getCategoriesList = ( parentId: number | null ): Term[] => { if ( ! categories?.length ) { return []; } if ( parentId === null ) { return categories; } return categories.filter( ( { parent } ) => parent === parentId ); }; const toggleAttribute = ( attributeName: keyof CategoriesAttributes ) => ( newValue: boolean ) => setAttributes( { [ attributeName ]: newValue } ); const renderCategoryName = ( name?: string ) => ! name ? __( '(Untitled)' ) : decodeEntities( name ).trim(); const renderCategoryList = () => { const parentId = isHierarchicalTaxonomy && showHierarchy ? 0 : null; const categoriesList = getCategoriesList( parentId ); return categoriesList.map( ( category ) => renderCategoryListItem( category ) ); }; const renderCategoryListItem = ( category: Term ) => { const childCategories = getCategoriesList( category.id ); const { id, link, count, name } = category; return (
{ taxonomy!.labels.no_terms }
) } { ! isResolving && categories?.length! > 0 && ( displayAsDropdown ? renderCategoryDropdown() : renderCategoryList() ) }