All files / src/components/charts RankedList.tsx

100% Statements 7/7
87.5% Branches 7/8
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                    20x           10x 3x       18x 7x     18x                                                                            
import React from "react";
import { Link } from "react-router-dom";
 
/**
 * Things ranked by a count — the agents citing a source, its most cited
 * documents, the sites its pages come from: each row its label, its value
 * and a thin bar against the largest. One series, so one hue and no legend.
 */
export interface RankedRow {
  key: string;
  label: React.ReactNode;
  value: number;
  /** A link for the label, inside the app. */
  href?: string | null;
  /** A quiet note after the label: "built in", "removed since". */
  note?: string;
}
 
export const RankedList: React.FC<{
  rows: RankedRow[];
  /** What a value counts, for each bar's screen-reader label. */
  unit: string;
  empty?: React.ReactNode;
}> = ({ rows, unit, empty }) => {
  if (!rows.length) {
    return (
      <p className="text-sm text-gray-400 dark:text-gray-500 italic">{empty ?? "Nothing yet."}</p>
    );
  }
  const peak = Math.max(...rows.map((r) => r.value), 1);
  return (
    <ol className="space-y-2.5">
      {rows.map((row) => (
        <li key={row.key} className="min-w-0">
          <div className="flex items-baseline justify-between gap-3 text-sm">
            <span className="min-w-0 truncate text-gray-700 dark:text-gray-200">
              {row.href ? (
                <Link to={row.href} className="hover:underline">
                  {row.label}
                </Link>
              ) : (
                row.label
              )}
              {row.note && (
                <span className="ml-1.5 text-xs text-gray-400 dark:text-gray-500">{row.note}</span>
              )}
            </span>
            <span className="shrink-0 text-gray-900 dark:text-gray-100 tabular-nums font-medium">
              {row.value.toLocaleString()}
            </span>
          </div>
          <div
            className="mt-1 h-1.5 rounded-full bg-indigo-100 dark:bg-indigo-950/60 overflow-hidden"
            role="meter"
            aria-valuemin={0}
            aria-valuemax={peak}
            aria-valuenow={row.value}
            aria-label={`${row.value.toLocaleString()} ${unit}`}
          >
            <div
              className="h-full rounded-full bg-indigo-600 dark:bg-indigo-500"
              style={{ width: `${(row.value / peak) * 100}%` }}
            />
          </div>
        </li>
      ))}
    </ol>
  );
};
 
export default RankedList;