Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"use client";
/**
* The plan inspector, as a right-hand drawer.
*
* It was a centred `max-w-4xl` dialog with six sections, a generated diff and a
* sticky footer — a reading surface wearing a modal. A drawer keeps the list on
* screen behind it, and lets the host put the open plan in the URL so a plan
* can be linked rather than only navigated to.
*
* The before-and-after itself is unchanged: `PlanComparison`, the evidence row
* and the blast list are the strongest work on this surface and the rework does
* not touch what they show. What changed is the frame. The header used to paint
* a gradient wash, an icon tile and the "After" panel's border from a per-type
* accent, which meant six hues separating categories where two types are 96% of
* the data — and Extract Helper's green was the same green as the health badge
* next to it. The type is a word now. Effort, confidence, blast radius and
* recovered health were three chip shapes across two sections; they are one
* line of prose under the title.
*/
import type { ReactNode } from "react";
import { Layers, TrendingUp, Wand2 } from "lucide-react";
import { AiPromptButton } from "../health/ai-prompt-button";
import { CollapsibleSection } from "../shared/collapsible-section";
import { Sheet, SheetContent, SheetTitle } from "../ui/sheet";
import { Skeleton, SkeletonRegion } from "../ui/skeleton";
import { disambiguateBasenames, formatNumber } from "../lib/format";
import { FilePath } from "../shared/file-path";
import { PlanComparison } from "./plan-comparison";
import { GenerateCodePanel } from "./generate-code-panel";
import { RefactoringModelToggle, type RefactoringModelToggleProps } from "./refactoring-settings-card";
import { PriorityExplanation } from "./priority-explanation";
import { ValidationSummary } from "./validation-summary";
import { CONFIDENCE_LABEL, EFFORT_LABEL, planBlurb, typeMeta } from "./meta";
import { blastCount, blastFiles, evidenceRows, planWins } from "./types";
import type {
Confidence,
EffortBucket,
GeneratedCode,
RefactoringPlan,
} from "@repowise-dev/types/refactoring";
export interface RefactoringDrawerProps {
plan: RefactoringPlan | null;
open: boolean;
onOpenChange: (open: boolean) => void;
loading?: boolean | undefined;
error?: string | undefined;
/** Optional causal context supplied by another shared product surface. */
contextSlot?: ReactNode;
onAiPrompt?: ((plan: RefactoringPlan) => void) | undefined;
/** Opt-in LLM code generation. Omit to hide the section entirely. */
onGenerateCode?: ((plan: RefactoringPlan) => Promise<GeneratedCode>) | undefined;
settingsHref?: string | undefined;
/** The code-generation switch, offered in place of the action while it is off. */
modelSetting?: RefactoringModelToggleProps | undefined;
fileHref?: ((path: string, line?: number | null) => string | undefined) | undefined;
}
export function RefactoringDrawer({
plan,
open,
onOpenChange,
loading = false,
error,
contextSlot,
onAiPrompt,
onGenerateCode,
settingsHref,
modelSetting,
fileHref,
}: RefactoringDrawerProps) {
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent
side="right"
closeLabel="Close plan"
// Wider than the nav sheet this primitive was written for: the
// before-and-after is two columns at rest, and the reading floor for the
// code block is what sets the minimum.
className="w-full max-w-[680px] sm:w-[92vw]"
>
{plan ? (
<DrawerBody
plan={plan}
onAiPrompt={onAiPrompt}
onGenerateCode={onGenerateCode}
settingsHref={settingsHref}
modelSetting={modelSetting}
fileHref={fileHref}
contextSlot={contextSlot}
/>
) : loading ? (
<>
<SheetTitle className="border-b border-[var(--color-border-default)] px-5 py-4 pr-12 text-[15px]">
Loading structured plan
</SheetTitle>
<SkeletonRegion className="space-y-4 px-5 py-5" label="Loading structured plan">
<Skeleton className="h-16 rounded-none" />
<Skeleton className="h-40 rounded-none" />
</SkeletonRegion>
</>
) : error ? (
<>
<SheetTitle className="border-b border-[var(--color-border-default)] px-5 py-4 pr-12 text-[15px]">
Structured plan unavailable
</SheetTitle>
<p className="px-5 py-5 text-sm text-[var(--color-text-secondary)]">{error}</p>
</>
) : (
<SheetTitle className="sr-only">Structured plan</SheetTitle>
)}
</SheetContent>
</Sheet>
);
}
function DrawerBody({
plan,
onAiPrompt,
onGenerateCode,
settingsHref,
modelSetting,
fileHref,
contextSlot,
}: {
plan: RefactoringPlan;
onAiPrompt?: ((plan: RefactoringPlan) => void) | undefined;
onGenerateCode?: ((plan: RefactoringPlan) => Promise<GeneratedCode>) | undefined;
settingsHref?: string | undefined;
modelSetting?: RefactoringModelToggleProps | undefined;
fileHref?: ((path: string, line?: number | null) => string | undefined) | undefined;
contextSlot?: ReactNode;
}) {
const meta = typeMeta(plan.refactoring_type);
const blurb = planBlurb(plan);
const effort = (plan.effort_bucket || "M") as EffortBucket;
const confidence = (plan.confidence || "medium") as Confidence;
const evidence = evidenceRows(plan);
const wins = planWins(plan);
const blast = blastFiles(plan).filter((f) => f !== plan.file_path);
const blastN = blastCount(plan);
const blastLabels = disambiguateBasenames(blast);
const lines =
plan.line_start && plan.line_end
? `${plan.line_start}–${plan.line_end}`
: plan.line_start || null;
const name = plan.file_path.split("/").pop() ?? plan.file_path;
// One line of words, in the order a reader asks: how big, how sure, how far,
// what it buys. Rule 4 — every figure gets a unit or a sentence.
const facts: string[] = [
`${EFFORT_LABEL[effort]} effort`,
`${CONFIDENCE_LABEL[confidence]} confidence`,
];
if (blastN > 0) facts.push(`${formatNumber(blastN)} file${blastN === 1 ? "" : "s"} affected`);
return (
<>
<div className="border-b border-[var(--color-border-default)] px-5 py-4 pr-12">
<div className="text-[11px] text-[var(--color-text-secondary)]">{meta.label}</div>
<SheetTitle className="mt-0.5 break-words font-mono text-[15px] font-semibold text-[var(--color-text-primary)]">
{plan.target_symbol || name}
</SheetTitle>
<FilePath
path={plan.file_path}
line={lines}
mode="full"
copyable
className="mt-1 block text-[11.5px] text-[var(--color-text-tertiary)]"
/>
</div>
<div className="flex flex-wrap items-center gap-x-4 gap-y-1 border-b border-[var(--color-border-default)] px-5 py-2.5 text-[12.5px] text-[var(--color-text-secondary)]">
{facts.map((f) => (
<span key={f}>{f}</span>
))}
{plan.impact_delta > 0 ? (
<span className="font-medium tabular-nums text-[var(--color-success)]">
+{plan.impact_delta.toFixed(1)} health
</span>
) : (
<span className="text-[var(--color-text-tertiary)]">No score change</span>
)}
</div>
<div className="flex-1 space-y-6 overflow-y-auto px-5 py-5">
{contextSlot}
<p className="text-sm leading-relaxed text-[var(--color-text-secondary)]">{blurb}</p>
{plan.risks && plan.risks.length > 0 ? (
<section>
<h4 className="mb-2.5 font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
Before you start
</h4>
<ul className="space-y-1">
{plan.risks.map((risk) => (
<li
key={`${risk.kind}:${risk.ref ?? risk.text}`}
className="flex items-start gap-2 text-sm text-[var(--color-text-secondary)]"
>
<span
className="mt-[7px] h-1.5 w-1.5 shrink-0 rounded-full bg-[var(--color-warning)]"
aria-hidden
/>
{risk.text}
</li>
))}
</ul>
</section>
) : null}
<section>
<h4 className="mb-3 font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
The change
</h4>
<PlanComparison plan={plan} fileHref={fileHref} />
</section>
{onGenerateCode ? (
<section>
<div className="mb-3 flex items-center justify-between gap-3">
<h4 className="flex items-center gap-1.5 font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
<Wand2 className="h-3.5 w-3.5" />
Generate the code
</h4>
{settingsHref ? (
<a
href={settingsHref}
className="text-[11px] text-[var(--color-text-tertiary)] underline-offset-2 transition-colors hover:text-[var(--color-text-secondary)] hover:underline"
>
Change model
</a>
) : null}
</div>
<GenerateCodePanel plan={plan} onGenerate={onGenerateCode} />
</section>
) : modelSetting ? (
<section className="border-t border-[var(--color-border-default)] pt-4">
<RefactoringModelToggle {...modelSetting} />
</section>
) : null}
{wins.length > 0 ? (
<section>
<h4 className="mb-2.5 flex items-center gap-1.5 font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
<TrendingUp className="h-3.5 w-3.5" />
What you gain
</h4>
<ul className="space-y-1">
{wins.map((w) => (
<li
key={w.label}
className={`flex items-start gap-2 text-sm ${
w.hero
? "font-medium text-[var(--color-text-primary)]"
: "text-[var(--color-text-secondary)]"
}`}
>
<span
className="mt-[7px] h-1 w-1 shrink-0 rounded-full bg-[var(--color-text-tertiary)]"
aria-hidden
/>
{w.label}
</li>
))}
</ul>
</section>
) : null}
{evidence.length > 0 ? (
<CollapsibleSection title="Why this was flagged">
<dl className="grid grid-cols-2 sm:grid-cols-4">
{evidence.map((row) => (
<div
key={row.label}
className="border-t border-[var(--color-border-default)] py-2 pr-3"
>
<dt className="font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
{row.label}
</dt>
<dd className="mt-0.5 text-sm font-semibold tabular-nums text-[var(--color-text-primary)]">
{row.value}
</dd>
</div>
))}
</dl>
</CollapsibleSection>
) : null}
{blast.length > 0 ? (
<CollapsibleSection
title={
<span className="flex items-center gap-1.5">
<Layers className="h-3.5 w-3.5" aria-hidden="true" />
Also affected
</span>
}
hint={formatNumber(blast.length)}
>
<ul className="space-y-1">
{blast.map((f) => {
const href = fileHref?.(f, null);
return (
<li key={f}>
{href ? (
<a
href={href}
className="flex min-w-0 text-[var(--color-text-secondary)] underline-offset-2 hover:text-[var(--color-accent-primary)] hover:underline"
>
<FilePath path={f} mode="distinguishing" labels={blastLabels} />
</a>
) : (
<span className="flex min-w-0 text-[var(--color-text-secondary)]">
<FilePath path={f} mode="distinguishing" labels={blastLabels} />
</span>
)}
</li>
);
})}
</ul>
</CollapsibleSection>
) : null}
<CollapsibleSection
title="Validation"
hint={
plan.validation
? plan.validation.total > 0
? `${formatNumber(plan.validation.total)} tests`
: "none guarding"
: undefined
}
defaultOpen={plan.validation?.total === 0}
>
<ValidationSummary
validation={plan.validation}
fileHref={(path) => fileHref?.(path, null)}
/>
</CollapsibleSection>
<CollapsibleSection title="Priority">
<PriorityExplanation plan={plan} />
</CollapsibleSection>
</div>
{onAiPrompt ? (
<div className="flex items-center gap-3 border-t border-[var(--color-border-default)] bg-[var(--color-bg-elevated)] px-5 py-3.5">
<AiPromptButton onClick={() => onAiPrompt(plan)} />
<span className="hidden text-xs text-[var(--color-text-tertiary)] sm:block">
The steps, the blast radius, and a completion contract.
</span>
</div>
) : null}
</>
);
}