/**
* Content Analytics — the Content Generator landing page.
*
* Five equal KPI cards, a hand-rolled div bar chart and a row of status chips
* floating in an empty card is what this was. Nothing on it outranked anything
* else, and the one number worth acting on — how much finished content is
* sitting unpublished — was not on the page at all.
*/
import { useState, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Button } from '@aime/wp-components';
import {
ResponsiveContainer,
ComposedChart,
Bar,
Line,
XAxis,
YAxis,
Tooltip,
CartesianGrid,
Legend,
} from 'recharts';
import useApi from '../../../hooks/useApi';
import Card from '../../common/Card';
import Loader from '../../common/Loader';
import Notice from '../../common/Notice';
import EmptyState from '../../common/EmptyState';
import ArcGauge from '../../common/ArcGauge';
import { RankRow } from '../../common/RankBar';
import { ARTICLE_STATUS_LABELS, ARTICLE_STATUS_COLORS } from '../../../utils/constants';
const RANGES = [ 7, 30, 90 ];
/*
* Recharts writes stroke and fill as SVG presentation attributes, where a
* `var()` never resolves — so the Content Generator accent has to be repeated
* as a literal here. This is the one documented exception to the cascade rule,
* and these must stay in step with `[data-module="content-generator"]` in
* global.scss.
*
* Published is a lighter step of the same indigo, not a second colour: the two
* series are the same articles at two moments in their life, and two unrelated
* hues would claim they were unrelated things.
*/
const CONTENT = '#4338CA';
const CONTENT_SOFT = '#A5B4FC';
const CHART_GRID = '#eaf2ea';
const CHART_TICK = '#5f7562';
const TOOLTIP_STYLE = {
borderRadius: 8,
border: '1px solid var(--aime-border-light, #e3ece4)',
boxShadow: '0 8px 24px rgba(20, 45, 22, 0.12)',
fontSize: 13,
};
/*
* Pipeline order — not alphabetical, and deliberately not sorted by count. Read
* in count order a status list tells you which pile is biggest. Read in stage
* order it tells you *where the work stopped*, which is the only thing this
* card can say that the totals above it cannot.
*/
const PIPELINE_ORDER = [
'draft',
'generating',
'ready',
'review',
'scheduled',
'published',
'archived',
];
const isoDay = ( date ) => {
const y = date.getFullYear();
const m = String( date.getMonth() + 1 ).padStart( 2, '0' );
const d = String( date.getDate() ).padStart( 2, '0' );
return `${ y }-${ m }-${ d }`;
};
/*
* The endpoint groups by date and returns only the days that had a row, so a
* 30-day window with four busy days comes back as four points. Plotted raw,
* those four sit shoulder to shoulder and three weeks of silence between two of
* them renders exactly as wide as a single overnight gap — the axis lies about
* the shape of the work. Scaffolding every day in the range back in at zero is
* what makes the spacing true.
*
* Dates the API returns that fall outside the scaffold are kept rather than
* dropped: a timezone difference between PHP's gmdate and the browser can put a
* real row one day either side of the window, and silently losing it would be
* worse than one extra tick.
*/
const buildSeries = ( days, created, published ) => {
const byDate = {};
const cursor = new Date();
cursor.setHours( 0, 0, 0, 0 );
for ( let i = days - 1; i >= 0; i-- ) {
const day = new Date( cursor );
day.setDate( day.getDate() - i );
const key = isoDay( day );
byDate[ key ] = { date: key, created: 0, published: 0 };
}
const apply = ( rows, field ) => {
( rows || [] ).forEach( ( row ) => {
const key = String( row.date || '' ).slice( 0, 10 );
if ( ! key ) {
return;
}
if ( ! byDate[ key ] ) {
byDate[ key ] = { date: key, created: 0, published: 0 };
}
byDate[ key ][ field ] = Number( row.count ) || 0;
} );
};
apply( created, 'created' );
apply( published, 'published' );
return Object.values( byDate )
.sort( ( a, b ) => a.date.localeCompare( b.date ) )
.map( ( point ) => ( { ...point, label: point.date.slice( 5 ) } ) );
};
const ContentAnalytics = ( { onNavigate } ) => {
const { get, loading, error, clearError } = useApi();
const [ stats, setStats ] = useState( null );
const [ days, setDays ] = useState( 30 );
const load = useCallback( async ( range ) => {
try {
const res = await get( `/content/analytics/overview?days=${ range }` );
setStats( res );
} catch ( e ) {
// The Notice above renders whatever useApi captured.
}
}, [ get ] );
useEffect( () => {
load( days );
}, [ load, days ] );
if ( loading && ! stats ) {
return
{ avgSeo > 0 ? __( 'Averaged across every article that has been scored.', 'ai-marketing-expert' ) : __( 'Nothing scored yet — generate an article to get a score.', 'ai-marketing-expert' ) }
{ avgWords > 0 ? sprintf( /* translators: %s: average word count per article. */ __( 'Across articles averaging %s words each.', 'ai-marketing-expert' ), avgWords.toLocaleString() ) : __( 'No finished articles to measure yet.', 'ai-marketing-expert' ) }
{ sprintf( /* translators: %s: total number of articles. */ __( '%s articles, every status, all time.', 'ai-marketing-expert' ), pipelineTotal.toLocaleString() ) }
| { __( 'Title', 'ai-marketing-expert' ) } | { __( 'Status', 'ai-marketing-expert' ) } | { __( 'Words', 'ai-marketing-expert' ) } | { __( 'SEO', 'ai-marketing-expert' ) } |
|---|---|---|---|
| { a.title || __( 'Untitled', 'ai-marketing-expert' ) } | { ARTICLE_STATUS_LABELS[ a.status ] || a.status } | { ( Number( a.actual_word_count ) || 0 ).toLocaleString() } | { a.seo_score || '—' } |