import { DonutChart } from '@mantine/charts'; import { Card, Center, Flex, Group, Indicator, Loader, Progress, Stack, Text } from '@mantine/core'; import { __ } from '@wordpress/i18n'; import type { ReactNode } from 'react'; import { collapseToTopN } from './collapse-to-top-n'; import type { DataItem } from './collapse-to-top-n'; import classes from './OverviewCard.module.scss'; interface OverviewCardProps { data?: { count: number | string; data: DataItem[]; }; title: string; unit?: string; isLoading: boolean; error?: { message: string }; children?: ReactNode; onClick?: () => void; /** * When set, keep only the top N items by value and aggregate the rest into * a single "Others" entry. Keeps the chart readable on sites with many * custom post types or many option groups. */ topN?: number; /** * Chart variant. 'donut' is the default; 'progress' renders a horizontal * stacked progress bar instead — a better fit for 2-segment data where a * donut feels oversized. */ variant?: 'donut' | 'progress'; } export function OverviewCard({ data, title, unit = '%', isLoading, error, children, onClick, topN, variant = 'donut', }: OverviewCardProps) { const allItems = data ? data.data : []; const items = topN ? collapseToTopN(allItems, topN) : allItems; return ( {title} {data && data.count} {__('Totals', 'scotty')} {children} {isLoading && (
)} {error && ( {__('Error:', 'scotty')} {error.message} )} {!isLoading && data && items.length > 0 && variant === 'donut' && ( {items.map(({ name, value, color, count }) => (
{name} {value} {unit} ({count})
))}
)} {!isLoading && data && items.length > 0 && variant === 'progress' && ( {items.map(({ name, value, color }) => ( {value >= 12 && ( {value} {unit} )} ))} {items.map(({ name, value, color, count }) => ( {name}{' '} {value} {unit} ({count}) ))} )}
); }