import type { CSSProperties } from 'react'; import { __, sprintf } from '@wordpress/i18n'; import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; import { ToggleControl, TextControl, CheckboxControl as WCCheckboxControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, Spinner as WCSpinner, } from '@wordpress/components'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import type { Post } from '@wordpress/core-data'; import { useEffect, useRef, useState } from '@wordpress/element'; import { useServerSideRender } from '@wordpress/server-side-render'; import { useDisabled } from '@wordpress/compose'; import { canUserEditPostContext, useToolsPanelDropdownMenuProps, } from '../utils/hooks'; import HtmlRenderer from '../utils/html-renderer'; import type { BreadcrumbsEditProps } from './types'; const separatorDefaultValue = '/'; export default function BreadcrumbEdit( { attributes, setAttributes, name, context: { postId, postType, templateSlug }, }: BreadcrumbsEditProps ) { const { separator, showHomeItem, showCurrentItem, prefersTaxonomy, showOnHomePage, } = attributes; const { post, canEditPost, isPostTypeHierarchical, postTypeHasTaxonomies, hasTermsAssigned, isLoading, } = useSelect( ( select ) => { if ( ! postType ) { return {}; } const _post = select( coreStore ).getEntityRecord< Post & Record< string, unknown > >( 'postType', postType, postId ); const _canReadPost = postId ? select( coreStore ).canUser( 'read', { kind: 'postType', name: postType, id: postId, } ) : undefined; const _canEditPost = canUserEditPostContext( select, postType, postId ); const postTypeObject = select( coreStore ).getPostType( postType ); const _postTypeHasTaxonomies = postTypeObject && postTypeObject.taxonomies.length; let taxonomies; if ( _postTypeHasTaxonomies ) { taxonomies = select( coreStore ).getTaxonomies( { type: postType, per_page: -1, } ); } return { post: _post, canEditPost: _canEditPost, isPostTypeHierarchical: postTypeObject?.hierarchical, postTypeHasTaxonomies: _postTypeHasTaxonomies, hasTermsAssigned: _post && ( taxonomies || [] ) .filter( ( { visibility } ) => visibility?.publicly_queryable ) .some( ( taxonomy ) => { return !! ( _post[ taxonomy.rest_base ] as unknown[] | undefined )?.length; } ), isLoading: // Wait for the post record only while it can still be // expected. The record is requested with `context=edit`, // so it never arrives when the user can't edit the post // (`_canEditPost === false`), nor when the post isn't // readable under `postType` at all // (`_canReadPost === false`) — the placeholder or the // server-side render is shown instead. ( postId && _canEditPost !== false && _canReadPost !== false && ! _post ) || ! postTypeObject || ( _postTypeHasTaxonomies && ! taxonomies ), }; }, [ postType, postId ] ); /** * Counter used to cache-bust `useServerSideRender`. * * This is a catch-all signal to re-render the block when a post's title, * parent ID, or terms change. * * This is fundamentally imperfect, because there are other entities which * could change in the meantime (the titles of ancestor posts, or the * labels of taxonomy terms), hence the choice to re-render systematically * upon saving. */ const [ invalidationKey, setInvalidationKey ] = useState( 0 ); useEffect( () => { setInvalidationKey( ( c ) => c + 1 ); }, [ post ] ); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const { content, status, error } = useServerSideRender( { attributes, skipBlockSupportAttributes: true, block: name, urlQueryArgs: { // The block-renderer endpoint requires `edit_post` permissions // for the post referenced by `post_id`, and the `postId` block // context can reference a post the current user can't edit (e.g. // another author's post in a query-like block), which failed with // a 403 error. Withhold `post_id` only when the post is confirmed // non-editable; when that can't be determined (e.g. `postId` and // `postType` coming from different providers don't designate a // readable post), forward it and let the server decide, as before. post_id: canEditPost === false ? undefined : postId, invalidationKey, }, } ); const prevContentRef = useRef( '' ); useEffect( () => { if ( status === 'success' ) { prevContentRef.current = content!; } }, [ content, status ] ); const [ showLoader, setShowLoader ] = useState( false ); useEffect( () => { if ( status !== 'loading' ) { return; } const timeout = setTimeout( () => { setShowLoader( true ); }, 400 ); return () => { clearTimeout( timeout ); setShowLoader( false ); }; }, [ status ] ); const disabledRef = useDisabled(); const blockProps = useBlockProps( { ref: disabledRef } ); if ( isLoading ) { return (
{ sprintf( /* translators: %s: error message returned when rendering the block. */ __( 'Error: %s' ), error! ) }