videogen/apps/web/components/analytics/CountBreakdown.tsx
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

109 lines
3.9 KiB
TypeScript

import * as React from "react";
import type { LucideIcon } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { cn } from "@/lib/utils";
import { formatCount } from "./format";
/** A single labelled row rendered as a proportional horizontal bar. */
export interface BreakdownRow {
/** Display label (already human-readable). */
readonly label: string;
/** The bar's count. */
readonly count: number;
}
export interface CountBreakdownProps {
/** Card heading, e.g. "By layout". */
title: string;
/** Rows to render (rendered in the given order; caller sorts). */
rows: readonly BreakdownRow[];
/** Optional heading icon. */
icon?: LucideIcon;
/** Bar colour class (default a muted accent). */
barClassName?: string;
/** Copy shown when there are no rows. */
emptyLabel?: string;
/** Cap the number of rows shown (default: all). */
maxRows?: number;
}
/**
* A compact horizontal-bar breakdown — no chart library. Each row is a labelled
* proportional bar sized against the largest bucket, with the raw count and its
* share of the total. Reused for layout / language / variants / templates /
* hooks. Presentational only; props are read-only.
*/
export function CountBreakdown({
title,
rows,
icon: Icon,
barClassName = "bg-primary",
emptyLabel = "No data yet.",
maxRows,
}: Readonly<CountBreakdownProps>) {
const visible =
typeof maxRows === "number" ? rows.slice(0, Math.max(0, maxRows)) : rows;
const total = rows.reduce((sum, row) => sum + row.count, 0);
const max = rows.reduce((m, row) => Math.max(m, row.count), 0);
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2 text-base">
{Icon ? <Icon className="size-4 text-muted-foreground" aria-hidden /> : null}
{title}
</CardTitle>
</CardHeader>
<CardContent>
{visible.length === 0 || total === 0 ? (
<p className="text-sm text-muted-foreground">{emptyLabel}</p>
) : (
<ul className="space-y-3">
{visible.map((row, index) => {
const pctOfMax = max > 0 ? (row.count / max) * 100 : 0;
const pctOfTotal = total > 0 ? (row.count / total) * 100 : 0;
return (
<li key={`${row.label}-${index}`}>
<div className="mb-1 flex items-center justify-between gap-3 text-sm">
<span className="min-w-0 truncate font-medium" title={row.label}>
{row.label}
</span>
<span className="shrink-0 tabular-nums text-muted-foreground">
{formatCount(row.count)}
<span className="ml-1 text-xs">
({Math.round(pctOfTotal)}%)
</span>
</span>
</div>
<div
className="h-2 w-full overflow-hidden rounded-full bg-muted"
role="img"
aria-label={`${row.label}: ${formatCount(row.count)} (${Math.round(
pctOfTotal,
)}%)`}
>
<div
className={cn("h-full rounded-full transition-all", barClassName)}
style={{ width: `${Math.max(pctOfMax, 4)}%` }}
/>
</div>
</li>
);
})}
</ul>
)}
</CardContent>
</Card>
);
}
/** Convert a `{ value: count }` map into descending-sorted breakdown rows. */
export function toBreakdownRows(
counts: Readonly<Record<string, number>>,
labelFor: (key: string) => string = (key) => key,
): BreakdownRow[] {
return Object.entries(counts)
.map(([key, count]) => ({ label: labelFor(key), count }))
.sort((a, b) => b.count - a.count || a.label.localeCompare(b.label));
}