import { ToggleControl, Spinner as WCSpinner, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; 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 { ArchivesAttributes } from './types'; const ARCHIVE_TYPE_ITEMS = [ { label: __( 'Year' ), value: 'yearly' }, { label: __( 'Month' ), value: 'monthly' }, { label: __( 'Week' ), value: 'weekly' }, { label: __( 'Day' ), value: 'daily' }, ]; export default function ArchivesEdit( { attributes, setAttributes, name, }: BlockEditProps< ArchivesAttributes > ) { const { showLabel, showPostCounts, displayAsDropdown, type } = attributes; const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const { content, status, error } = useServerSideRender( { attributes, skipBlockSupportAttributes: true, block: name, } ); const disabledRef = useDisabled(); const blockProps = useBlockProps( { ref: disabledRef } ); return ( <> { setAttributes( { displayAsDropdown: false, showLabel: true, showPostCounts: false, type: 'monthly', } ); } } dropdownMenuProps={ dropdownMenuProps } > displayAsDropdown } onDeselect={ () => setAttributes( { displayAsDropdown: false } ) } > setAttributes( { displayAsDropdown: ! displayAsDropdown, } ) } /> { displayAsDropdown && ( ! showLabel } onDeselect={ () => setAttributes( { showLabel: true } ) } > setAttributes( { showLabel: ! showLabel, } ) } /> ) } showPostCounts } onDeselect={ () => setAttributes( { showPostCounts: false } ) } > setAttributes( { showPostCounts: ! showPostCounts, } ) } /> type !== 'monthly' } onDeselect={ () => setAttributes( { type: 'monthly' } ) } > item.value === type ) ?? ARCHIVE_TYPE_ITEMS[ 0 ] } onValueChange={ ( item ) => item && setAttributes( { type: item.value! } ) } /> { status === 'loading' && (
) } { status === 'error' && (

{ sprintf( /* translators: %s: error message returned when rendering the block. */ __( 'Error: %s' ), error! ) }

) } { status === 'success' && ( ) } ); }