import React, { useMemo, useState } from 'react';
import { __ } from '@wordpress/i18n';
import { Block } from '@/components/Blocks/Block';
import { BlockContent } from '@/components/Blocks/BlockContent';
import { BlockHeading } from '@/components/Blocks/BlockHeading';
import Icon from '@/utils/Icon';
import type { PageRevisionsData } from '@/api/getPageRevisionsData';
import PageRevisionsGraph from './PageRevisionsGraph';
import PageRevisionsList from './PageRevisionsList';
interface PageRevisionsBlockProps {
data?: PageRevisionsData;
isLoadingData?: boolean;
fallbackPageUrl?: string;
}
/**
* Stable loading placeholder matching the block's approximate layout.
*
* @return {JSX.Element} Loading skeleton.
*/
const PageRevisionsSkeleton: React.FC = () => (
{Array.from({ length: 3 }).map( ( _, i ) => (
) )}
);
/**
* Empty state for when no revisions exist for the page.
*
* @return {JSX.Element} Empty state.
*/
const PageRevisionsEmpty: React.FC = () => (
{__( 'No revisions found for this page in the selected date range', 'burst-statistics' )}
{__( 'WordPress revisions will appear here automatically when content updates are published, tracking their effect on user engagement.', 'burst-statistics' )}
);
/**
* PageRevisionsBlock — main block component for the Revision Impact feature.
* Renders engagement score chart with revision markers and a ranked revision list.
*
* @param {PageRevisionsBlockProps} props - Component props.
* @return {JSX.Element} The revision impact block.
*/
// fallow-ignore-next-line complexity
export const PageRevisionsBlock: React.FC = ({
data,
isLoadingData = false
}) => {
const hasNoRevisions = ! data || 0 === data.revisions.length;
// Default-select the latest (most recent) revision.
const defaultSelected = useMemo( (): number | null => {
if ( ! data?.revisions.length ) {
return null;
}
return data.revisions[0]?.id ?? null;
}, [ data ]);
const [ selectedRevisionId, setSelectedRevisionId ] = useState(
defaultSelected
);
// Sync default selection when data first loads.
const effectiveSelected =
null === selectedRevisionId ? defaultSelected : selectedRevisionId;
return (
{isLoadingData && }
{! isLoadingData && hasNoRevisions && }
{! isLoadingData && ! hasNoRevisions && (
<>
{/* Chart area */}
{/* Divider */}
{/* Revision list */}
>
)}
);
};