import { debounce } from '@wordpress/compose'; import { useEffect, useState, useRef } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; import { addQueryArgs } from '@wordpress/url'; import { sanitizeBlockAttributes } from '@wordpress/blocks'; import { parseErrorMessage } from './errors'; export function rendererPath( block: string, attributes: Record< string, unknown > | null = null, urlQueryArgs: Record< string, unknown > = {} ): string { return addQueryArgs( `/wp/v2/block-renderer/${ block }`, { context: 'edit', ...( null !== attributes ? { attributes } : {} ), ...urlQueryArgs, } ); } export function removeBlockSupportAttributes( attributes: Record< string, unknown > & { style?: Record< string, unknown >; } ): Record< string, unknown > { const { backgroundColor, borderColor, fontFamily, fontSize, gradient, textColor, className, ...restAttributes } = attributes; const { border, color, elements, shadow, spacing, typography, ...restStyles } = attributes?.style || {}; return { ...restAttributes, style: restStyles, }; } /** * Server-side render response object. */ export interface ServerSideRenderResponse { /** The current request status: 'idle', 'loading', 'success', or 'error'. */ status: 'idle' | 'loading' | 'success' | 'error'; /** The rendered block content (available when status is 'success'). */ content?: string; /** The error message (available when status is 'error'). */ error?: string; } /** * Configuration object for the useServerSideRender hook. */ export interface UseServerSideRenderArgs { /** The block attributes to be sent to the server for rendering. */ attributes: Record< string, unknown >; /** The identifier of the block to be serverside rendered. Example: 'core/archives'. */ block: string; /** Whether to remove block support attributes before sending. */ skipBlockSupportAttributes?: boolean; /** The HTTP method to use ('GET' or 'POST'). Default is 'GET'. */ httpMethod?: 'GET' | 'POST'; /** Additional query arguments to append to the request URL. */ urlQueryArgs?: Record< string, unknown >; } /** * A hook for server-side rendering a preview of dynamic blocks to display in the editor. * * Handles fetching server-rendered previews for blocks, managing loading states, * and automatically debouncing requests to prevent excessive API calls. It supports both * GET and POST requests, with POST requests used for larger attribute payloads. * * @example * Basic usage: * * ```jsx * import { RawHTML } from '@wordpress/element'; * import { useServerSideRender } from '@wordpress/server-side-render'; * * function MyServerSideRender( { attributes, block } ) { * const { content, status, error } = useServerSideRender( { * attributes, * block, * } ); * * if ( status === 'loading' ) { * return