Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

13.1 KB
Raw Blame
/**
 * Shared color tokens + helpers for the code-health surface.
 *
 * Single source of truth so the score pill on a file row, the severity
 * chip on a finding card, and the KPI card text colors all agree.
 * All colors come from the semantic CSS tokens (--color-error/warning/
 * caution/success) so the surface themes correctly in both modes.
 */

import {
  bandForScore,
  formatScore,
  HEALTH_BAND_LABEL,
  HEALTHY_MIN,
  NEEDS_WORK_MIN,
  type HealthBand,
} from "@repowise-dev/types/health";

export type Severity = "critical" | "high" | "medium" | "low";

export const SEVERITY_ORDER: Record<Severity, number> = {
  low: 0,
  medium: 1,
  high: 2,
  critical: 3,
};

export const SEVERITY_LABEL: Record<Severity, string> = {
  critical: "Critical",
  high: "High",
  medium: "Medium",
  low: "Low",
};

/**
 * @deprecated Use `SeverityMark`. This paints a tinted ground, a border and
 * coloured text for one token that repeats several times per row, and in a
 * findings list the grounds tile into stripes that outweigh the marker names
 * beside them. Kept exported because `@repowise-dev/ui` is consumed outside
 * this repo; nothing in here should reach for it.
 */
export const SEVERITY_CHIP: Record<Severity, string> = {
  critical:
    "bg-[var(--color-error)]/15 text-[var(--color-error)] border border-[var(--color-error)]/30",
  high: "bg-[var(--color-warning)]/15 text-[var(--color-warning)] border border-[var(--color-warning)]/30",
  medium:
    "bg-[var(--color-caution)]/15 text-[var(--color-caution)] border border-[var(--color-caution)]/30",
  low: "bg-[var(--color-text-tertiary)]/15 text-[var(--color-text-tertiary)] border border-[var(--color-text-tertiary)]/30",
};

/** The quiet mono label: a field name, a state word, a small tag. */
export const MICRO = "font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]";

/** Segment fills for a severity split (ProportionBar `color`). */
export const SEVERITY_SEGMENT: Record<Severity, string> = {
  critical: "var(--color-error)",
  high: "var(--color-warning)",
  medium: "var(--color-caution)",
  low: "var(--color-text-tertiary)",
};

/**
 * The one health band -> colour vocabulary. Excellent and Good share the
 * green; the band word carries the difference. Every table below is written
 * out in full because Tailwind only sees class names it can read literally.
 */
const HEALTH_BAND_VAR: Record<HealthBand, string> = {
  excellent: "var(--color-success)",
  good: "var(--color-success)",
  fair: "var(--color-caution)",
  needs_work: "var(--color-warning)",
  at_risk: "var(--color-error)",
};

const HEALTH_BAND_TEXT: Record<HealthBand, string> = {
  excellent: "text-[var(--color-success)]",
  good: "text-[var(--color-success)]",
  fair: "text-[var(--color-caution)]",
  needs_work: "text-[var(--color-warning)]",
  at_risk: "text-[var(--color-error)]",
};

const HEALTH_BAND_BADGE: Record<HealthBand, string> = {
  excellent:
    "bg-[var(--color-success)]/15 text-[var(--color-success)] border border-[var(--color-success)]/30",
  good: "bg-[var(--color-success)]/15 text-[var(--color-success)] border border-[var(--color-success)]/30",
  fair: "bg-[var(--color-caution)]/15 text-[var(--color-caution)] border border-[var(--color-caution)]/30",
  needs_work:
    "bg-[var(--color-warning)]/15 text-[var(--color-warning)] border border-[var(--color-warning)]/30",
  at_risk:
    "bg-[var(--color-error)]/15 text-[var(--color-error)] border border-[var(--color-error)]/30",
};

const HEALTH_BAND_BADGE_SOFT: Record<HealthBand, string> = {
  excellent: "bg-[var(--color-success)]/15 text-[var(--color-success)]",
  good: "bg-[var(--color-success)]/15 text-[var(--color-success)]",
  fair: "bg-[var(--color-caution)]/15 text-[var(--color-caution)]",
  needs_work: "bg-[var(--color-warning)]/15 text-[var(--color-warning)]",
  at_risk: "bg-[var(--color-error)]/15 text-[var(--color-error)]",
};

/** SVG `fill-` classes, for the marks the scatter charts draw. */
export const HEALTH_BAND_FILL: Record<HealthBand, string> = {
  excellent: "fill-[var(--color-success)]",
  good: "fill-[var(--color-success)]",
  fair: "fill-[var(--color-caution)]",
  needs_work: "fill-[var(--color-warning)]",
  at_risk: "fill-[var(--color-error)]",
};

/**
 * Band fills. Good is the same green stepped down in opacity, so it stays
 * legible where it sits next to Excellent.
 */
export const HEALTH_BAND_BAR: Record<HealthBand, string> = {
  excellent: "bg-[var(--color-success)]",
  good: "bg-[var(--color-success)]/55",
  fair: "bg-[var(--color-caution)]",
  needs_work: "bg-[var(--color-warning)]",
  at_risk: "bg-[var(--color-error)]",
};

/** The same fills as CSS values, for a ProportionBar `color`. */
export const HEALTH_BAND_SEGMENT: Record<HealthBand, string> = {
  excellent: "var(--color-success)",
  good: "color-mix(in srgb, var(--color-success) 55%, transparent)",
  fair: "var(--color-caution)",
  needs_work: "var(--color-warning)",
  at_risk: "var(--color-error)",
};

/**
 * The canvas ramp. Dense filled shapes carry no band word, so Excellent and
 * Good separate by value here rather than sharing the one green.
 */
const HEALTH_BAND_NODE_FILL: Record<HealthBand, string> = {
  excellent: "var(--color-node-excellent)",
  good: "var(--color-node-good)",
  fair: "var(--color-node-fair)",
  needs_work: "var(--color-node-needs-work)",
  at_risk: "var(--color-node-at-risk)",
};

export function healthBandNodeFill(band: HealthBand): string {
  return HEALTH_BAND_NODE_FILL[band];
}

/** The canvas ramp for a 1-10 score. */
export function healthNodeFill(score: number): string {
  return HEALTH_BAND_NODE_FILL[bandForScore(score)];
}

/**
 * The map's fills: the five canonical bands, softened.
 *
 * The same bands every score pill on the page uses, so a dot and the pill
 * beside it never disagree. Each band's node token is pulled a little toward
 * the neutral, so a field of thousands reads as pigment rather than as light.
 * Five fills also keep the canvas at five draw batches.
 */
function softened(token: string): string {
  return `color-mix(in srgb, var(${token}) 82%, var(--color-node-neutral))`;
}

const NODE_BAND_FILL: Record<HealthBand, string> = {
  excellent: softened("--color-node-excellent"),
  good: softened("--color-node-good"),
  fair: softened("--color-node-fair"),
  needs_work: softened("--color-node-needs-work"),
  at_risk: softened("--color-node-at-risk"),
};

/** The fill for a file that needs nothing under a banded lens. */
export const NODE_HEALTHY_FILL = NODE_BAND_FILL.good;

/** Three problem stops, for banded lenses (coverage, churn, performance). */
export const NODE_RAMP_STOP = {
  warning: NODE_BAND_FILL.fair,
  serious: NODE_BAND_FILL.needs_work,
  alert: NODE_BAND_FILL.at_risk,
} as const;

/** The map fill for a 1-10 score, on the canonical band. */
export function healthRampFill(score: number): string {
  return NODE_BAND_FILL[bandForScore(score)];
}

/** The map fill for a band, for keys. */
export function healthBandMapFill(band: HealthBand): string {
  return NODE_BAND_FILL[band];
}

/**
 * Whether a map fill marks a file that needs attention, which is what earns a
 * glow and a place in the "needs attention" count: the Needs work and At risk
 * fills. Green, gold and the neutral stay quiet.
 */
export function isLoudFill(fill: string): boolean {
  return fill === NODE_BAND_FILL.needs_work || fill === NODE_BAND_FILL.at_risk;
}

export function healthBandColor(band: HealthBand): string {
  return HEALTH_BAND_VAR[band];
}

export function healthBandTextColor(band: HealthBand): string {
  return HEALTH_BAND_TEXT[band];
}

export function healthBandSoftBadgeClass(band: HealthBand): string {
  return HEALTH_BAND_BADGE_SOFT[band];
}

/** Tailwind text colour for a 1-10 score. */
export function scoreTextColor(score: number | null | undefined): string {
  if (score == null) return "text-[var(--color-text-primary)]";
  return HEALTH_BAND_TEXT[bandForScore(score)];
}

/** Bordered score pill, for a figure sitting in a table row. */
export function scoreBadgeClass(score: number): string {
  return HEALTH_BAND_BADGE[bandForScore(score)];
}

/** Borderless score pill, for a figure sitting inline in prose. */
export function scoreSoftBadgeClass(score: number): string {
  return HEALTH_BAND_BADGE_SOFT[bandForScore(score)];
}

/** Raw colour for a 1-10 score, for canvas ink and inline styles. */
export function healthInk(score10: number): string {
  return HEALTH_BAND_VAR[bandForScore(score10)];
}

/** The same, for the surfaces that carry health on a 0-100 scale. */
export function healthInk100(score100: number): string {
  return healthInk(score100 / 10);
}

export function healthBand100(score100: number): HealthBand {
  return bandForScore(score100 / 10);
}

/**
 * A 1-10 score as the colour and the word together, so a lede printing the
 * label and the figure beside it taking the colour cannot disagree.
 */
export function healthBand(score: number): { color: string; label: string } {
  const band = bandForScore(score);
  return { color: HEALTH_BAND_VAR[band], label: HEALTH_BAND_LABEL[band] };
}

/**
 * The canonical three bands a label takes beside a health mark: Healthy at
 * `HEALTHY_MIN` or above, Warning from `NEEDS_WORK_MIN`, Alert below. The five
 * bands above are for a standalone lede; a dot, a figure and a word drawn
 * together read on these three, so every mark agrees with every other.
 */
export type HealthMarkBand = "healthy" | "warning" | "alert";

/** Top to bottom, with the lower edge of each band, for keys and grounds. */
export const HEALTH_MARK_BANDS: readonly {
  band: HealthMarkBand;
  label: string;
  color: string;
  min: number;
}[] = [
  { band: "healthy", label: "Healthy", color: "var(--color-success)", min: HEALTHY_MIN },
  { band: "warning", label: "Warning", color: "var(--color-warning)", min: NEEDS_WORK_MIN },
  { band: "alert", label: "Alert", color: "var(--color-error)", min: 0 },
];

export interface HealthMark {
  /** The score at one decimal, floored so it never crosses a band edge. */
  value: string;
  band: HealthMarkBand;
  label: string;
  color: string;
}

/** A 1-10 score as a health mark: figure, word and colour on the three bands. */
export function healthMark(score: number): HealthMark {
  const b = HEALTH_MARK_BANDS.find((x) => score >= x.min) ?? HEALTH_MARK_BANDS[HEALTH_MARK_BANDS.length - 1]!;
  return { value: formatScore(score), band: b.band, label: b.label, color: b.color };
}

export function riskInk(risk01: number): string {
  if (risk01 >= 0.66) return "var(--color-error)";
  if (risk01 >= 0.33) return "var(--color-warning)";
  return "var(--color-success)";
}

export function coverageColor(pct: number): string {
  if (pct < 30) return "bg-[var(--color-error)]";
  if (pct < 60) return "bg-[var(--color-warning)]";
  if (pct < 80) return "bg-[var(--color-caution)]";
  return "bg-[var(--color-success)]";
}

/**
 * Coverage as a band, at the thresholds {@link coverageColor} already paints.
 *
 * One function for the same reason `healthBand()` is one function: the coverage
 * lede prints the label, the figure takes the colour and the distribution bar
 * segments by it, and three call sites disagreeing about where "Strong" starts
 * is worse than the duplication that avoids it.
 */
export function coverageBand(pct: number): { color: string; label: string } {
  if (pct < 30) return { color: "var(--color-error)", label: "Thin" };
  if (pct < 60) return { color: "var(--color-warning)", label: "Partial" };
  if (pct < 80) return { color: "var(--color-caution)", label: "Solid" };
  return { color: "var(--color-success)", label: "Strong" };
}

/** Tailwind text colour for a coverage figure, on the same bands. */
export function coverageTextColor(pct: number | null | undefined): string {
  if (pct == null) return "text-[var(--color-text-primary)]";
  if (pct < 30) return "text-[var(--color-error)]";
  if (pct < 60) return "text-[var(--color-warning)]";
  if (pct < 80) return "text-[var(--color-caution)]";
  return "text-[var(--color-success)]";
}

export function deltaColor(delta: number | null | undefined): string {
  if (delta == null || delta === 0) return "text-[var(--color-text-tertiary)]";
  return delta > 0 ? "text-[var(--color-success)]" : "text-[var(--color-error)]";
}

export function formatDelta(delta: number | null | undefined): string {
  if (delta == null) return "—";
  // Rounding decides the sign here, not the raw value: a delta of -0.001 is
  // "0.00", and printing it as "-0.00" claims a direction the figure on screen
  // does not have.
  const rounded = Number(delta.toFixed(2));
  if (rounded === 0) return "0.00";
  return `${rounded > 0 ? "+" : ""}${rounded.toFixed(2)}`;
}

/**
 * A finding's deduction as a signed string, or `null` when it has none.
 *
 * Two different zeros, kept apart. Performance findings carry an impact of
 * exactly zero by construction, so there is no deduction to print and `null`
 * tells the caller to print a word instead. A real deduction too small to show
 * at two places is still a measured cost, so it prints as a bound rather than
 * as "−0.00", which would claim the finding costs nothing.
 */
export function formatHealthImpact(impact: number | null | undefined): string | null {
  if (impact == null || impact === 0) return null;
  const rounded = Number(Math.abs(impact).toFixed(2));
  if (rounded === 0) return "−<0.01";
  return `−${rounded.toFixed(2)}`;
}