import clsx from 'clsx'; import { useTranslate } from 'i18n-calypso'; import StatsHeroCard from './stats-hero-card'; import type { StatsCardProps } from './types'; import './stats-card.scss'; const BASE_CLASS_NAME = 'stats-card'; const StatsCard = ( props: StatsCardProps ) => { const translate = useTranslate(); const { heroElement, splitHeader, toggleControl } = props; // Isolate the rendering logic for the Locations module into a new component. // This ensures the existing StatsCard component remains unchanged, allowing us to safely iterate on it. if ( heroElement && splitHeader && toggleControl ) { return ; } const { children, className, title, titleURL, titleAriaLevel = 4, titleNodes, downloadCsv, footerAction, isEmpty, emptyMessage, multiHeader, metricLabel, mainItemLabel, additionalHeaderColumns, headerClassName, overlay, } = props; const titleNode = titleURL ? ( { title } ) : (
{ title }
{ titleNodes }
); // On one line shows card title and value column header const simpleHeaderNode = (
{ titleNode } { ! isEmpty &&
{ metricLabel ?? translate( 'Views' ) }
}
); // Show card title and download csv button on one line, description and metric label on another: const multiHeaderNode = ( <>
{ titleNode } { ! isEmpty && downloadCsv }
{ ! isEmpty && (
All { title.toLowerCase() }
{ metricLabel ?? translate( 'Views' ) }
) } ); // Show Card title on one line and all other column header(s) below: // (main item, optional additional columns and value) const splitHeaderNode = (
{ ! heroElement && titleNode } { downloadCsv }
{ toggleControl }
{ ! isEmpty && (
{ splitHeader && mainItemLabel } { additionalHeaderColumns && (
{ additionalHeaderColumns }
) }
{ ! isEmpty && (
{ metricLabel ?? translate( 'Views' ) }
) }
) }
); const getHeaderNode = () => { if ( multiHeader ) { return multiHeaderNode; } if ( splitHeader ) { return splitHeaderNode; } return simpleHeaderNode; }; return (
{ !! heroElement && (
{ splitHeader && (
{ titleNode }
) } { heroElement }
) }
{ getHeaderNode() }
{ isEmpty ? emptyMessage : children }
{ footerAction && ( { footerAction.label || translate( 'View all' ) } ) }
{ overlay &&
{ overlay }
}
); }; export default StatsCard;