import { AlertCircle, AlertTriangle, CheckCircle2 } from "lucide-react"; import type { ClaimDetail } from "@/types"; type ValidationPanelProps = { validation: ClaimDetail["validation"]; }; type IssueList = ClaimDetail["validation"]["errors"]; /** * Group issues by their `rule` field, preserving insertion order so the * backend's surface order is respected on screen. Returns an array of * `[rule, issues]` pairs — Maps aren't a great fit for JSX iteration here. */ function groupByRule(issues: IssueList): Array<[string, IssueList]> { const groups = new Map(); for (const issue of issues) { const bucket = groups.get(issue.rule); if (bucket) { bucket.push(issue); } else { groups.set(issue.rule, [issue]); } } return Array.from(groups.entries()); } /** * One rule-group block: header with rule code + count chip, followed by * the list of messages underneath. */ function IssueGroup({ rule, issues, testId, }: { rule: string; issues: IssueList; testId: string; }) { const Icon = testId === "validation-errors" ? AlertCircle : AlertTriangle; const iconColor = testId === "validation-errors" ? "text-[color:var(--m-error)]" : "text-[color:var(--m-warning)]"; return (
{rule} {issues.length}
); } /** * Validation section of the claim detail drawer (SP4). * * Three states: * - passed && warnings.length === 0 → "All checks passed" pill * - errors and/or warnings present → grouped sub-sections * * Issues are grouped by their `rule` field (e.g. R050_diagnosis_present) * with a per-group count, so repeated violations of the same rule don't * drown out other rules. Errors render before warnings so the user sees * blocking issues first. */ export function ValidationPanel({ validation }: ValidationPanelProps) { const allPassed = validation.passed && validation.warnings.length === 0; if (allPassed) { return (
All checks passed Valid
); } const errorGroups = groupByRule(validation.errors); const warningGroups = groupByRule(validation.warnings); return (
Validation {validation.errors.length > 0 ? (
Errors ({validation.errors.length})
{errorGroups.map(([rule, issues]) => ( ))}
) : null} {validation.warnings.length > 0 ? (
Warnings ({validation.warnings.length})
{warningGroups.map(([rule, issues]) => ( ))}
) : null}
); }