All files / src/apps/user/pages/knowledge KnowledgeSummary.tsx

100% Statements 7/7
87.5% Branches 14/16
100% Functions 3/3
100% Lines 7/7

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                        4x         4x               76x               304x 304x 304x         3x                                                                                
import { Skeleton } from "@/components/detail";
import type { LucideIcon } from "lucide-react";
import React from "react";
 
/**
 * A knowledge list's overview, in `ResourceCollection`'s `summary` slot:
 * the few things worth acting on — what needs attention, what reaches no
 * agent, what no answer cites — each a count and a filter (`?show=`), as
 * the Rules tab's layer flow is. The selected step again shows them all.
 *
 * Neutral, like the page: a step is named by its icon and label. Only
 * "Needs attention" carries a meaning, and only while it counts something.
 */
export interface SummaryStep {
  key: string;
  label: string;
  /** One line on what it counts. */
  hint: string;
  count: number;
  Icon: LucideIcon;
  /** It is a warning while it counts anything. */
  warns?: boolean;
}
 
export interface KnowledgeSummaryProps {
  steps: SummaryStep[];
  selected: string;
  onSelect: (key: string) => void;
  /** A line above the steps: what the list adds up to. */
  headline?: React.ReactNode;
  loading?: boolean;
  label: string;
}
 
/** The strip's columns by how many steps it has: written out, since
 *  Tailwind keeps only class names it can read whole. */
const COLUMNS: Record<number, string> = {
  3: "sm:grid-cols-3",
  4: "sm:grid-cols-2 lg:grid-cols-4",
};
 
const KnowledgeSummary: React.FC<KnowledgeSummaryProps> = ({
  steps,
  selected,
  onSelect,
  headline,
  loading,
  label,
}) => (
  <nav aria-label={label} className="space-y-2">
    {headline && (
      <div className="text-xs text-gray-500 dark:text-gray-400">
        {loading ? <Skeleton className="h-3.5 w-64" /> : headline}
      </div>
    )}
    <ul className={`grid gap-2 ${COLUMNS[steps.length] ?? COLUMNS[3]}`}>
      {steps.map(({ key, label: stepLabel, hint, count, Icon, warns }) => {
        const isSelected = selected === key;
        const warning = warns && count > 0;
        return (
          <li key={key} className="min-w-0">
            <button
              type="button"
              aria-pressed={isSelected}
              onClick={() => onSelect(isSelected ? "all" : key)}
              className={`flex h-full w-full items-start gap-3 rounded-xl border px-3.5 py-3 text-left transition-all ${
                isSelected
                  ? "border-gray-900 dark:border-white bg-gray-50 dark:bg-gray-800"
                  : "border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 hover:border-gray-300 dark:hover:border-gray-500"
              }`}
            >
              <span
                className={`shrink-0 rounded-lg p-1.5 ${
                  warning
                    ? "bg-amber-50 dark:bg-amber-950/40 text-amber-600 dark:text-amber-400"
                    : "bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-300"
                }`}
              >
                <Icon className="size-4" aria-hidden="true" />
              </span>
              <span className="min-w-0 flex-1">
                <span className="block truncate text-sm font-semibold text-gray-900 dark:text-gray-100">
                  {stepLabel}
                </span>
                <span className="mt-0.5 block truncate text-xs text-gray-500 dark:text-gray-400">
                  {hint}
                </span>
              </span>
              {loading ? (
                <Skeleton className="h-5 w-5" />
              ) : (
                <span className="shrink-0 text-base font-semibold text-gray-900 dark:text-gray-100 tabular-nums">
                  {count.toLocaleString()}
                </span>
              )}
            </button>
          </li>
        );
      })}
    </ul>
  </nav>
);
 
export default KnowledgeSummary;