import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
RangeControl,
Spinner as WCSpinner,
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useServerSideRender } from '@wordpress/server-side-render';
import { useDisabled } from '@wordpress/compose';
import { SelectControl } from '@wordpress/ui';
import type { BlockEditProps } from '@wordpress/blocks';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import HtmlRenderer from '../utils/html-renderer';
import type { LatestCommentsAttributes } from './types';
/**
* Minimum number of comments a user can show using this block.
*/
const MIN_COMMENTS = 1;
/**
* Maximum number of comments a user can show using this block.
*/
const MAX_COMMENTS = 100;
const DISPLAY_CONTENT_ITEMS: {
label: string;
value: NonNullable< LatestCommentsAttributes[ 'displayContent' ] >;
}[] = [
{ label: __( 'No content' ), value: 'none' },
{ label: __( 'Excerpt' ), value: 'excerpt' },
{ label: __( 'Full content' ), value: 'full' },
];
export default function LatestComments( {
attributes,
setAttributes,
name,
}: BlockEditProps< LatestCommentsAttributes > ) {
const { commentsToShow, displayAvatar, displayDate, displayContent } =
attributes;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const { content, status, error } = useServerSideRender( {
attributes,
skipBlockSupportAttributes: true,
block: name,
urlQueryArgs: {
// The preview uses the site's locale to make it more true to how
// the block appears on the frontend. Setting the locale
// explicitly prevents any middleware from setting it to 'user'.
_locale: 'site',
},
} );
const disabledRef = useDisabled();
const blockProps = useBlockProps( { ref: disabledRef } );
return (
<>
{ sprintf( /* translators: %s: error message returned when rendering the block. */ __( 'Error: %s' ), error! ) }