All files / src/components/detail SectionCard.tsx

100% Statements 7/7
96.66% Branches 29/30
100% Functions 1/1
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                                105x       105x                 105x           105x                         4146x     4146x   4146x                                                                                                                  
import React from "react";
 
/**
 * The bordered, titled panel every detail and form page is built from.
 *
 * Twenty-three files declared their own copy of this. They were not
 * genuinely different components — they were the same shell with a different
 * subset of an additive prop list, and they fell into exactly three clusters:
 *
 * | Where | Header | Body |
 * |---|---|---|
 * | viewer pages (`Agent`, `ToolkitViewer`, …) | plain | `px-5 py-4` |
 * | create/edit forms (`NewAgent`, `EditKnowledge`, …) | tinted | `px-5 py-5 space-y-5` |
 * | settings pages (`project/Settings`, `organisation/Settings`) | plain, with description | rows divided by a hairline |
 *
 * So the clusters became `variant`, and everything that varied *within* a
 * cluster became an optional prop. Reach for a variant first; drop to
 * `className` / `bodyClassName` only for something genuinely one-off.
 */
export type SectionCardVariant = "panel" | "form" | "settings";
 
export interface SectionCardProps {
  /** Lucide icon component (passed as the component, not an element). */
  icon?: React.ElementType;
  title: React.ReactNode;
  /** Muted text *beside* the title — a count, a type, an id. */
  subtitle?: React.ReactNode;
  /** Muted text *below* the title. Switches the header to top-aligned. */
  description?: React.ReactNode;
  /** Right-aligned header content — a button, a toggle, a badge. */
  action?: React.ReactNode;
  children: React.ReactNode;
  /** Extra classes on the card itself, e.g. `mb-4` or a column span. */
  className?: string;
  /** Replaces the variant's body padding outright. */
  bodyClassName?: string;
  variant?: SectionCardVariant;
}
 
const SHELL =
  "rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm overflow-hidden";
 
/** Body treatment per variant. `settings` divides its children into rows
 *  rather than padding them, because its children *are* the rows. */
const BODY: Record<SectionCardVariant, string> = {
  panel: "px-5 py-4",
  form: "px-5 py-5 space-y-5",
  settings: "divide-y divide-gray-50 dark:divide-gray-700/50",
};
 
/** A form's header is tinted so a page of stacked inputs still reads as
 *  discrete steps; a viewer's is not, because its panels are already
 *  separated by their content. */
const HEADER_TINT: Record<SectionCardVariant, string> = {
  panel: "",
  form: "bg-gray-50 dark:bg-gray-800/60",
  settings: "",
};
 
export const SectionCard: React.FC<SectionCardProps> = ({
  icon: Icon,
  title,
  subtitle,
  description,
  action,
  children,
  className = "",
  bodyClassName,
  variant = "panel",
}) => {
  // A description makes the header two lines tall, so the icon aligns to the
  // top of the text rather than to the centre of the block.
  const stacked = !!description;
  // Forms box their icon: the tinted header would otherwise leave a bare
  // glyph floating on a tinted ground with nothing to sit in.
  const boxed = variant === "form" && stacked;
 
  return (
    <div className={`${SHELL} ${className}`.trim()}>
      {(Icon || title || action) && (
        <div
          className={`flex gap-2 border-b border-gray-100 dark:border-gray-700 ${
            stacked ? "items-start gap-3 px-5 py-4" : "items-center px-5 py-3.5"
          } ${HEADER_TINT[variant]}`.trim()}
        >
          {Icon &&
            (boxed ? (
              <div className="p-1.5 rounded-lg bg-white dark:bg-gray-700 border border-gray-200 dark:border-gray-600 shrink-0 mt-0.5">
                <Icon className="size-3.5 text-gray-500 dark:text-gray-400" />
              </div>
            ) : (
              <Icon
                className={`size-4 text-gray-500 dark:text-gray-400 shrink-0 ${
                  stacked ? "mt-0.5" : ""
                }`.trim()}
              />
            ))}
 
          <div
            className={
              stacked
                ? "min-w-0"
                : "flex items-center gap-2 min-w-0 flex-1"
            }
          >
            <h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300 leading-snug">
              {title}
            </h3>
            {subtitle !== undefined && subtitle !== null && subtitle !== "" && (
              <span
                className={`text-xs text-gray-400 dark:text-gray-500 truncate ${
                  stacked ? "block mt-0.5" : ""
                }`.trim()}
              >
                {subtitle}
              </span>
            )}
            {description && (
              <p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
                {description}
              </p>
            )}
          </div>
 
          {action && <div className="ml-auto shrink-0">{action}</div>}
        </div>
      )}
 
      <div className={bodyClassName ?? BODY[variant]}>{children}</div>
    </div>
  );
};
 
export default SectionCard;