import { __, sprintf } from '@wordpress/i18n';
import { Block } from '@/components/Blocks/Block';
import { BlockContent } from '@/components/Blocks/BlockContent';
import { BlockFooter } from '@/components/Blocks/BlockFooter';
import { BlockHeading } from '@/components/Blocks/BlockHeading';
import Icon from '@/utils/Icon';
import { ScrollDepthChart } from './ScrollDepthChart';
import { ScrollDepthInsight } from './ScrollDepthInsight';
import { ScrollDepthDatum } from '@/api/getScrollAnalyticsData';
/**
* Minimum number of scroll-tracked visits before the chart is shown without a warning.
* Below this threshold we show a "limited data" notice. Below MIN_BUILDING we show an
* empty state instead.
*/
const MIN_BUILDING = 0; // 0 meaningful visits → building state
const MIN_RELIABLE = 100; // ≥ 100 → full graph, no warning
interface ScrollDepthBlockProps {
data?: {
totalPageviews: number;
meaningfulVisits?: number;
totalRawVisits?: number;
data: ScrollDepthDatum[];
insight: {
range: string;
dwellSeconds: number;
};
};
isLoadingData?: boolean;
}
/**
* Skeleton loader shown while scroll analytics data is loading.
*/
const ScrollDepthSkeleton = () => (
{ [ 95, 68, 42, 22 ].map( ( width, i ) => (
) ) }
);
/**
* "Building dataset" empty state shown when no scroll data exists yet.
*/
const BuildingState = () => (
{ __( 'Building your scroll dataset', 'burst-statistics' ) }
{ __(
'Once visitors start scrolling this page, depth data will appear here automatically.',
'burst-statistics'
) }
);
/**
* Warning badge shown when the dataset is small and results may be unreliable.
*
* @param props - Component props.
* @param props.count - Number of meaningful visits so far.
* @param props.minReliable - Threshold to be considered reliable.
*/
const LimitedDataBadge = ({ count, minReliable }: { count: number; minReliable: number }) => (
{ sprintf(
/* translators: 1: current count, 2: min reliable threshold */
__( 'Limited data — %1$d sessions tracked (need %2$d+ for reliable insights)', 'burst-statistics' ),
count,
minReliable
) }
);
/**
* Displays scroll reach and dwell time for the selected page.
* Renders three quality states: building (no data), limited-data warning, and full chart.
*
* @param props - Component props.
* @param props.data - Live scroll depth statistics from the API.
* @param props.isLoadingData - Whether the data is currently loading.
* @return Scroll-depth analytics block.
*/
export const ScrollDepthBlock = ({ data, isLoadingData = false }: ScrollDepthBlockProps ) => {
const meaningfulVisits = data?.meaningfulVisits ?? data?.totalPageviews ?? 0;
const isBuilding = ! isLoadingData && ( ! data || meaningfulVisits <= MIN_BUILDING );
const isLimited = ! isLoadingData && ! isBuilding && meaningfulVisits < MIN_RELIABLE;
return (
{ isLoadingData ? (
) : isBuilding ? (
) : (
<>
{ isLimited && (
) }
>
) }
{ ! isLoadingData && ! isBuilding && data?.insight?.range && (
) }
);
};