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.

refactor(ui): share one median helper and one effort label map (#3561)

Pragalva058f14c3e127
diff --git a/packages/ui/__tests__/health/findings-view-queue.test.tsx b/packages/ui/__tests__/health/findings-view-queue.test.tsx
index 2a18c1d68..3fd000b51 100644
--- a/packages/ui/__tests__/health/findings-view-queue.test.tsx
+++ b/packages/ui/__tests__/health/findings-view-queue.test.tsx
@@ -111,6 +111,19 @@ describe("FindingsView history signals", () => {
const watchSection = screen.getAllByRole("heading", { level: 3 })[watchAt]!.closest("section")!;
expect(within(watchSection).getByText("w.py")).toBeInTheDocument();
});
+
+ it("groups by effort under the shared label, with no size threshold", async () => {
+ const load = vi.fn(async () => page([row("a.py"), row("b.py", { effort_bucket: "XL" })]));
+ render(<FindingsView adapter={adapter({ getHealthWorkQueue: load })} />);
+ await screen.findByText("a.py");
+
+ fireEvent.change(screen.getByLabelText("Group"), { target: { value: "effort" } });
+
+ const headings = screen.getAllByRole("heading", { level: 3 }).map((h) => h.textContent ?? "");
+ expect(headings.some((h) => h.startsWith("Medium"))).toBe(true);
+ expect(headings.some((h) => h.startsWith("Extra large"))).toBe(true);
+ expect(headings.join(" ")).not.toMatch(/NLOC/);
+ });
});
describe("FindingsView graph", () => {
diff --git a/packages/ui/__tests__/health/refactoring-card.test.tsx b/packages/ui/__tests__/health/refactoring-card.test.tsx
index 6854a08c8..42c1a850a 100644
--- a/packages/ui/__tests__/health/refactoring-card.test.tsx
+++ b/packages/ui/__tests__/health/refactoring-card.test.tsx
@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { EFFORT_LABEL } from "../../src/health/labels";
import {
RefactoringCard,
type RefactoringTarget,
@@ -148,6 +149,18 @@ describe("a file led by a history marker", () => {
});
describe("the row's labels and action", () => {
+ it("uses the shared effort wording in the badge and summary", () => {
+ const original = EFFORT_LABEL.M;
+ EFFORT_LABEL.M = "Moderate";
+ try {
+ render(<RefactoringCard target={target({ effort_bucket: "M" })} />);
+ expect(screen.getByTitle("Effort: Moderate (NLOC 900)")).toBeInTheDocument();
+ expect(screen.getByText("· Moderate effort")).toBeInTheDocument();
+ } finally {
+ EFFORT_LABEL.M = original;
+ }
+ });
+
it("labels a test file, and only a test file", () => {
const { rerender } = render(<RefactoringCard target={target({ is_test: true })} />);
expect(screen.getByText("test")).toBeInTheDocument();
diff --git a/packages/ui/__tests__/refactoring/structural-field.test.tsx b/packages/ui/__tests__/refactoring/structural-field.test.tsx
index 557161eff..2a311fa6c 100644
--- a/packages/ui/__tests__/refactoring/structural-field.test.tsx
+++ b/packages/ui/__tests__/refactoring/structural-field.test.tsx
@@ -20,6 +20,8 @@ import {
type StructuralMark,
} from "../../src/refactoring/opportunity";
import { StructuralMap } from "../../src/refactoring/structural-map";
+import { StartHere } from "../../src/refactoring/start-here";
+import { EFFORT_LABEL } from "../../src/health/labels";
function opportunity(
overrides: Partial<RefactoringOpportunity> = {},
@@ -145,6 +147,19 @@ describe("salience", () => {
});
});
+describe("StartHere", () => {
+ it("uses the shared effort wording in ranked rows", () => {
+ const original = EFFORT_LABEL.M;
+ EFFORT_LABEL.M = "Moderate";
+ try {
+ render(<StartHere opportunities={[opportunity()]} />);
+ expect(screen.getByText(/Moderate effort/)).toBeInTheDocument();
+ } finally {
+ EFFORT_LABEL.M = original;
+ }
+ });
+});
+
describe("StructuralMap", () => {
// The field sizes itself from its container, and jsdom reports every element
// as zero-width, so without this it renders its axes and legend and no marks
diff --git a/packages/ui/src/health/churn-complexity-quadrant.tsx b/packages/ui/src/health/churn-complexity-quadrant.tsx
index 388e537c7..455741611 100644
--- a/packages/ui/src/health/churn-complexity-quadrant.tsx
+++ b/packages/ui/src/health/churn-complexity-quadrant.tsx
@@ -6,6 +6,7 @@ import { useMemo, useState } from "react";
// subpath imports are fine (erased before resolution).
import { bandForScore, formatScore } from "@repowise-dev/types";
import type { ChurnComplexityPoint } from "@repowise-dev/types/health";
+import { median } from "../lib/math";
import { ordinal } from "../lib/format";
import { HEALTH_BAND_FILL } from "./tokens";
@@ -15,14 +16,6 @@ export interface ChurnComplexityQuadrantProps {
height?: number;
}
-/** Median of a numeric list (the data-driven quadrant split). */
-function median(values: number[]): number {
- if (values.length === 0) return 0;
- const sorted = [...values].sort((a, b) => a - b);
- const mid = Math.floor(sorted.length / 2);
- return sorted.length % 2 === 0 ? (sorted[mid - 1]! + sorted[mid]!) / 2 : sorted[mid]!;
-}
-
/**
* Churn x complexity quadrant -- the "hotspot anatomy" view. X = 90-day commit
* count (how often the file changes), Y = max cyclomatic complexity (how
diff --git a/packages/ui/src/health/findings-view.tsx b/packages/ui/src/health/findings-view.tsx
index 29957735e..735e896c2 100644
--- a/packages/ui/src/health/findings-view.tsx
+++ b/packages/ui/src/health/findings-view.tsx
@@ -50,6 +50,7 @@ import {
isWatchOnlyBiomarker,
} from "./biomarker-glossary";
import { buildAiPrompt } from "./ai-prompt-builder";
+import { EFFORT_LABEL } from "./labels";
import { SEVERITY_LABEL, type Severity } from "./tokens";
import type { CodeHealthAdapter } from "./code-health-adapter";
@@ -72,13 +73,6 @@ function lowestSeverity(picked: Severity[]): Severity {
return SEVERITIES.filter((s) => picked.includes(s)).pop() ?? "low";
}
-const EFFORT_LABEL: Record<string, string> = {
- S: "Small (≤40 NLOC)",
- M: "Medium (≤150 NLOC)",
- L: "Large (≤400 NLOC)",
- XL: "Extra large (>400 NLOC)",
-};
-
export function FindingsView({ adapter }: { adapter: CodeHealthAdapter }) {
const { cacheKey } = adapter;
diff --git a/packages/ui/src/health/refactoring-card.tsx b/packages/ui/src/health/refactoring-card.tsx
index 4816d0cfd..0ee1ce5b8 100644
--- a/packages/ui/src/health/refactoring-card.tsx
+++ b/packages/ui/src/health/refactoring-card.tsx
@@ -12,6 +12,7 @@ import {
} from "./biomarker-glossary";
import type { BiomarkerDetailsRecord } from "./biomarker-details";
import { EFFORT_TINT, type Severity } from "./tokens";
+import { EFFORT_LABEL } from "./labels";
import { ImpactFigure } from "./impact-figure";
import { FindingOpportunityLink } from "./file-opportunity";
import type { RefactoringOpportunity } from "@repowise-dev/types/refactoring";
@@ -109,13 +110,6 @@ export interface HealthWorkItemCardProps {
onToggleSelect?: ((target: HealthWorkItem) => void) | undefined;
}
-const effortLabel: Record<EffortBucket, string> = {
- S: "Small",
- M: "Medium",
- L: "Large",
- XL: "Extra large",
-};
-
export function HealthWorkItemCard({
target,
onSelect,
@@ -215,7 +209,7 @@ export function HealthWorkItemCard({
<PreviewBadge note={target.preview} />
<span
className={`inline-block rounded px-1.5 py-0.5 text-[10px] uppercase font-semibold ${EFFORT_TINT[target.effort_bucket]}`}
- title={`Effort: ${effortLabel[target.effort_bucket]} (NLOC ${target.nloc})`}
+ title={`Effort: ${EFFORT_LABEL[target.effort_bucket]} (NLOC ${target.nloc})`}
>
{target.effort_bucket}
</span>
@@ -272,7 +266,7 @@ export function HealthWorkItemCard({
<div className="flex items-center gap-3 pt-1 text-xs text-[var(--color-text-tertiary)] flex-wrap">
<span>Score {target.score.toFixed(1)}/10</span>
<span>· {target.nloc} NLOC</span>
- <span>· {effortLabel[target.effort_bucket]} effort</span>
+ <span>· {EFFORT_LABEL[target.effort_bucket]} effort</span>
<span>
{"· "}
{target.open_finding_count != null &&
diff --git a/packages/ui/src/lib/math.test.ts b/packages/ui/src/lib/math.test.ts
new file mode 100644
index 000000000..e055f9613
--- /dev/null
+++ b/packages/ui/src/lib/math.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from "vitest";
+
+import { median } from "./math";
+
+describe("median", () => {
+ it("averages the two middle values of an even count", () => {
+ expect(median([1, 2, 3, 10])).toBe(2.5);
+ expect(median([4, 8])).toBe(6);
+ });
+
+ it("takes the middle value of an odd count", () => {
+ expect(median([9, 1, 5])).toBe(5);
+ });
+
+ it("sorts numerically and leaves the input alone", () => {
+ const values = [10, 9, 100, 1];
+ expect(median(values)).toBe(9.5);
+ expect(values).toEqual([10, 9, 100, 1]);
+ });
+
+ it("is 0 for an empty list", () => {
+ expect(median([])).toBe(0);
+ });
+});
diff --git a/packages/ui/src/lib/math.ts b/packages/ui/src/lib/math.ts
new file mode 100644
index 000000000..2436ffbec
--- /dev/null
+++ b/packages/ui/src/lib/math.ts
@@ -0,0 +1,14 @@
+/**
+ * Small numeric helpers shared across repowise UI surfaces.
+ */
+
+/**
+ * Median of a numeric list, 0 for an empty one. An even count averages the two
+ * middle values, so two charts splitting the same data draw the same line.
+ */
+export function median(values: number[]): number {
+ if (values.length === 0) return 0;
+ const sorted = [...values].sort((a, b) => a - b);
+ const mid = Math.floor(sorted.length / 2);
+ return sorted.length % 2 === 0 ? (sorted[mid - 1]! + sorted[mid]!) / 2 : sorted[mid]!;
+}
diff --git a/packages/ui/src/refactoring/start-here.tsx b/packages/ui/src/refactoring/start-here.tsx
index 71dedf913..1d93bb753 100644
--- a/packages/ui/src/refactoring/start-here.tsx
+++ b/packages/ui/src/refactoring/start-here.tsx
@@ -24,6 +24,7 @@
import * as React from "react";
import { formatNumber } from "../lib/format";
+import { EFFORT_LABEL } from "../health/labels";
import { StructuralMap } from "./structural-map";
import { typeMeta } from "./meta";
import { MAP_MIN_POINTS } from "./types";
@@ -47,13 +48,6 @@ export interface StartHereProps {
onHighlight?: ((id: string | null) => void) | undefined;
}
-const EFFORT_WORD: Record<string, string> = {
- S: "Small",
- M: "Medium",
- L: "Large",
- XL: "Extra large",
-};
-
export function StartHere({
opportunities,
onOpen,
@@ -122,7 +116,7 @@ export function StartHere({
<div>
{lead.map((item, i) => {
const meta = typeMeta(item.lead_refactoring_type || "");
- const effort = EFFORT_WORD[item.effort_bucket] ?? item.effort_bucket;
+ const effort = EFFORT_LABEL[item.effort_bucket] ?? item.effort_bucket;
const lit = item.opportunity_id === highlighted;
return (
<button
diff --git a/packages/ui/src/refactoring/structural-map.tsx b/packages/ui/src/refactoring/structural-map.tsx
index 012f47d49..9d55a7418 100644
--- a/packages/ui/src/refactoring/structural-map.tsx
+++ b/packages/ui/src/refactoring/structural-map.tsx
@@ -47,6 +47,7 @@
import * as React from "react";
import { formatNumber } from "../lib/format";
+import { median } from "../lib/math";
import { typeMeta } from "./meta";
import {
MARK_STROKE,
@@ -111,12 +112,6 @@ function axisCeiling(max: number): number {
return Math.max(step, Math.ceil(max / step) * step);
}
-function median(values: number[]): number {
- if (values.length === 0) return 0;
- const sorted = [...values].sort((a, b) => a - b);
- return sorted[Math.floor(sorted.length / 2)] ?? 0;
-}
-
export function StructuralMap({
marks,
dropped = 0,