import React, { useEffect, useState } from 'react';
import { __, _n, sprintf } from '@wordpress/i18n';
import { api } from '../api';
import { Card, Notice, MetricGrid, SegmentBar, Button } from './ui';
import Explain from './Explain';
import { IconRefresh, IconDoc } from './icons';
/**
* Word counts across the site.
*
* The bands are the useful part, not the average. An average of 640 words
* hides a site where half the pages are 1,200 and half are 80 — and the 80s
* are the whole problem. So the distribution comes first and the average is a
* footnote.
*/
const TONE = {
empty: '#c0392b',
thin: '#e2952b',
standard: '#4436c8',
deep: '#2b7a4b',
long: '#8b5cf0',
unknown: '#b7bcca',
};
export default function WordStats({ onOpen }) {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [busy, setBusy] = useState(false);
const load = (fresh = false) => {
setBusy(true);
api
.get(`/content/words${fresh ? '?fresh=1' : ''}`)
.then(setData)
.catch((e) => setError(e.message))
.finally(() => setBusy(false));
};
useEffect(() => {
load();
}, []);
if (error) return {error};
if (!data) return {__('Counting…', 'ai-rank-suite')};
const bands = data.bands || {};
const catalog = data.catalog || [];
const known = (data.posts || 0) - (bands.unknown || 0);
return (
0 ? data.average || 0 : '—',
sub: known > 0 ? sprintf(__('across %s counted pages', 'ai-rank-suite'), known.toLocaleString()) : __('nothing counted yet', 'ai-rank-suite'),
},
{
id: 'thin',
label: __('Thin or empty', 'ai-rank-suite'),
hint: __('Too little for an assistant to quote or a search engine to rank.', 'ai-rank-suite'),
value: (bands.thin || 0) + (bands.empty || 0),
accent: (bands.thin || 0) + (bands.empty || 0) > 0 ? 'var(--ars-accent)' : undefined,
sub: __('under 300 words', 'ai-rank-suite'),
},
{
id: 'deep',
label: __('In depth', 'ai-rank-suite'),
value: (bands.deep || 0) + (bands.long || 0),
sub: __('800 words and over', 'ai-rank-suite'),
},
]}
/>
}
title={__('How long your pages are', 'ai-rank-suite')}
subtitle={__('Counted from the rendered page, not the raw post content — so a page built entirely of blocks is counted properly', 'ai-rank-suite')}
action={
}
>
{/*
* The bar describes the pages that have been counted, and only
* those.
*
* "Not counted yet" used to be a band inside it, so on a site
* where most pages have never been analysed the distribution was
* four fifths grey and the real shape was unreadable — and worse,
* it implied a measurement where there is none. Coverage is now
* stated above the bar as what it is: how much of the site the
* bar speaks for.
*/}
{(() => {
const counted = catalog.reduce((n, b) => n + (bands[b.id] || 0), 0);
const unknown = bands.unknown || 0;
const total = counted + unknown;
const pct = (n) => (counted > 0 ? Math.round((100 * n) / counted) : 0);
return (
{__('Nothing has been counted yet — run the site audit and this fills in.', 'ai-rank-suite')}
) : (
<>
({
label: b.label,
value: bands[b.id] || 0,
color: TONE[b.id],
}))}
/>
{/* Each band is a row with its share, and a row is a
filter: the Pages tab can show exactly these. */}
>
)}
{unknown > 0 && (
{sprintf(
_n(
'%s published page has never been analysed, so it is not in the bar above at all — counting it as empty would be a guess. Run the site audit, or use the analyzer on the dashboard, and it joins the distribution.',
'%s published pages have never been analysed, so they are not in the bar above at all — counting them as empty would be a guess. Run the site audit, or use the analyzer on the dashboard, and they join the distribution.',
unknown,
'ai-rank-suite'
),
unknown.toLocaleString()
)}
)}
{__('WordPress counts the raw post content, so it counts shortcode attributes, block comments and the alt text inside an image block — and it counts nothing that a block renders at display time. On a site built with blocks or a page builder the two numbers can differ by hundreds of words.', 'ai-rank-suite')}
{__('This counts the rendered page, which is what a crawler and an assistant actually read. It is stored on each post when the page is analyzed, so listing a thousand rows does not mean rendering a thousand pages.', 'ai-rank-suite')}
{__('Reading time uses 220 words per minute, the middle of the range for adult silent reading of general prose. Treat it as an estimate, not a measurement.', 'ai-rank-suite')}