All files / packages/sds-customization-compliance/src/components ComplianceRow.tsx

63.63% Statements 14/22
41.42% Branches 29/70
55.55% Functions 5/9
77.77% Lines 14/18

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                                        27x 27x       27x 27x   27x                                                   23x                             27x 27x 27x                                                                                                                             23x         23x                   23x 23x 23x                                          
import type { ReactNode } from 'react';
import type {
  ComplianceRow as ComplianceRowData,
  ComplianceRowCategory,
  ComplianceRowSummary,
  Offender,
} from '../types.js';
import { formatOffenderLocation, statusGlyph } from './copy.js';
 
/**
 * Host-supplied inline-markdown renderer. Receives the raw `label` or
 * `detail` string (which may contain backticks, bold, links, etc.) and
 * returns arbitrary React nodes. Hosts that already carry a markdown
 * renderer (sandbox uses `renderMarkdownInline`) wire it in here so
 * `` `--slds-c-*` `` tokens render as `<code>` instead of literal
 * backticks. Omit to render the string verbatim.
 */
export type InlineRenderer = (source: string) => ReactNode;
 
export interface ComplianceRowProps {
  row: ComplianceRowData;
  /** Maximum offenders to render; pass 0 / omit to suppress the offender list. */
  maxOffenders?: number;
  /** Optional markdown/inline renderer applied to `row.label` and `row.detail`. */
  renderInline?: InlineRenderer;
}
 
/**
 * Renders a single compliance result as a tonal row.
 *
 * Markup is intentionally minimal — host surfaces (sandbox TaskPanel,
 * Storybook addon) can either `import '…/components/styles.css'` for
 * the default look or paint their own via the documented `data-status`
 * and `data-check-id` attributes.
 */
export function ComplianceRow({ row, maxOffenders = 5, renderInline }: Readonly<ComplianceRowProps>) {
  const offenders = row.offenders ?? [];
  const visibleOffenders = maxOffenders > 0 ? offenders.slice(0, maxOffenders) : [];
  // `row.count` is authoritative — some checks pre-truncate `offenders` to a
  // sample (e.g. first 10) while `count` keeps the full population. Falling
  // back to `offenders.length` keeps checks that ship every offender honest.
  const totalOffenders = Math.max(row.count ?? 0, offenders.length);
  const remaining = Math.max(0, totalOffenders - visibleOffenders.length);
 
  return (
    <div className="scp-row" data-status={row.status} data-check-id={row.id}>
      <span className="scp-row__status" aria-label={row.status}>
        {statusGlyph(row.status)}
      </span>
      <div className="scp-row__body">
        <div className="scp-row__label">
          <span className="scp-row__label-text">{renderInline ? renderInline(row.label) : row.label}</span>
          {row.count > 0 ? (
            <span
              className="scp-row__count"
              aria-label={`${row.count} ${row.count === 1 ? 'offender' : 'offenders'}`}
            >
              {row.count}
            </span>
          ) : null}
        </div>
        {renderRowBody(row, renderInline)}
        {visibleOffenders.length > 0 ? (
          <ul className="scp-row__offenders">
            {visibleOffenders.map((offender, i) => (
              // Key format: `offender-{check-id}-{i}`. The check id scopes
              // the key to this compliance row so DevTools shows which
              // check the offender belongs to; the trailing index is the
              // within-row tiebreaker. The offender list itself is derived
              // from a read-only compliance report, so index-stable.
              <li key={`offender-${row.id}-${i}`} className="scp-row__offender">
                {renderOffender(offender, renderInline)}
              </li>
            ))}
            {remaining > 0 ? (
              <li className="scp-row__offender scp-row__offender--more">+{remaining} more</li>
            ) : null}
          </ul>
        ) : null}
      </div>
    </div>
  );
}
 
function renderRowBody(row: ComplianceRowData, renderInline?: InlineRenderer): ReactNode {
  Iif (row.summary) return renderSummary(row.summary, row.status, renderInline);
  Iif (!row.detail) return null;
  return <div className="scp-row__detail">{renderInline ? renderInline(row.detail) : row.detail}</div>;
}
 
function renderSummary(
  summary: ComplianceRowSummary,
  status: ComplianceRowData['status'],
  renderInline?: InlineRenderer,
): ReactNode {
  return (
    <div className="scp-row__summary">
      {summary.callout ? (
        <div className="scp-row__callout" data-status={status}>
          {renderInline ? renderInline(summary.callout) : summary.callout}
        </div>
      ) : null}
      {summary.categories?.length ? (
        <ul className="scp-row__categories">
          {summary.categories.map((category) => (
            <li
              key={category.id ?? category.heading}
              className="scp-row__category"
              data-category-id={category.id}
            >
              {renderCategory(category, renderInline)}
            </li>
          ))}
        </ul>
      ) : null}
      {summary.guidance?.length ? (
        <div className="scp-row__guidance">
          {summary.guidance.map((paragraph) => (
            <p key={paragraph} className="scp-row__guidance-paragraph">
              {renderInline ? renderInline(paragraph) : paragraph}
            </p>
          ))}
        </div>
      ) : null}
    </div>
  );
}
 
function renderCategory(category: ComplianceRowCategory, renderInline?: InlineRenderer): ReactNode {
  return (
    <>
      <div className="scp-row__category-heading">
        <span className="scp-row__category-title">{category.heading}</span>
        {typeof category.count === 'number' && category.count > 0 ? (
          <span
            className="scp-row__category-count"
            aria-label={`${category.count} ${category.count === 1 ? 'item' : 'items'}`}
          >
            {category.count}
          </span>
        ) : null}
      </div>
      <p className="scp-row__category-description">
        {renderInline ? renderInline(category.description) : category.description}
      </p>
    </>
  );
}
 
function renderOffenderIdentity(offender: Offender): ReactNode {
  Iif (!offender.selector && !offender.hook && !offender.prop) return null;
  // Hook + prop carry the "what" (the customer-facing token name) and lead;
  // selector carries the "where" and trails. CSS pushes the trailing
  // selector to the right edge of the identity row so the eye lands on the
  // hook first.
  return (
    <span className="scp-row__offender-identity">
      {offender.hook ? <span className="scp-row__offender-hook">{offender.hook}</span> : null}
      {offender.prop ? <span className="scp-row__offender-prop">{offender.prop}</span> : null}
      {offender.selector ? <span className="scp-row__offender-selector">{offender.selector}</span> : null}
    </span>
  );
}
 
function renderOffender(offender: Offender, renderInline?: InlineRenderer) {
  const identity = renderOffenderIdentity(offender);
  Iif (!identity && !offender.location && !offender.note && !offender.fix) return null;
  return (
    <>
      {identity}
      {offender.location ? (
        <span className="scp-row__offender-location" title={offender.location}>
          {formatOffenderLocation(offender.location)}
        </span>
      ) : null}
      {offender.note ? (
        <span className="scp-row__offender-note">
          {renderInline ? renderInline(offender.note) : offender.note}
        </span>
      ) : null}
      {offender.fix ? (
        <span className="scp-row__offender-fix">
          {renderInline ? renderInline(offender.fix) : offender.fix}
        </span>
      ) : null}
    </>
  );
}