All files / src/pages/flowsheet-page/flowsheet/Diagnostics DiagnosticFindingCard.tsx

84.09% Statements 37/44
54.28% Branches 38/70
20% Functions 1/5
90.9% Lines 30/33

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                                                                      76x                                                     10x     16x         16x 16x   16x               10x     26x   10x   36x 10x 26x 26x   10x       26x     36x 10x       26x 36x   46x   10x   26x           26x                                             36x                         56x 16x                                 48x 48x 80x 64x      
import {
  AlertCircle,
  AlertTriangle,
  CheckCircle2,
  Info,
  Lightbulb,
  LocateFixed,
} from "lucide-react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  Card,
  CardContent,
  CardHeader,
  CardTitle,
} from "@/ahuora-design-system/ui/card";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import {
  type Finding,
  FindingFixActionKindEnum as KindEnum,
  SeverityEnum,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
 
export type DiagnosticFinding = Finding;
 
interface DiagnosticFindingCardProps {
  finding: DiagnosticFinding;
  onApplyFix?: (findingId: string) => void;
  onFocusComponent?: (componentName: string) => void;
}
 
const severityConfig: Record<
  Finding["severity"],
  { icon: typeof AlertCircle; color: string; bgColor: string }
> = {
  [SeverityEnum.Error]: {
    icon: AlertCircle,
    color: "text-rose-500",
    bgColor: "bg-destructive/5",
  },
  [SeverityEnum.Warning]: {
    icon: AlertTriangle,
    color: "text-amber-500",
    bgColor: "bg-warning/5",
  },
  [SeverityEnum.Info]: {
    icon: Info,
    color: "text-sky-500",
    bgColor: "bg-blue-500/5",
  },
  [SeverityEnum.Suggestion]: {
    icon: Lightbulb,
    color: "text-violet-500",
    bgColor: "bg-violet-500/5",
  },
};
 
function formatTitle(title: string): string {
  if (title.match(/^Blocked at GATE\d/i)) {
    return "Critical Error";
  }
  return title;
}
 
export function DiagnosticFindingCard({
  finding,
  onApplyFix,
  onFocusComponent,
}: DiagnosticFindingCardProps) {
  const config = severityConfig[finding.severity];
  const Icon = config.icon;
  const canApplyFix =
    !!finding.id &&
    !!onApplyFix &&
    !!finding.fixAction &&
    (finding.fixAction.kind === KindEnum.ScenarioPatch ||
      typeof finding.suggestedValue === "number");
 
  return (
    <Card
      className={cn(
        "border rounded-md p-4 cursor-default hover:shadow-none hover:bg-transparent [&:hover:not(:has([data-locate-button]:hover))]:bg-card",
        config.bgColor,
      )}
    >
      <CardHeader className="px-0 pt-0 pb-0 text-left">
        <div className="flex items-center gap-2 min-w-0 flex-wrap">
          <Icon className={cn("h-4 w-4 shrink-0", config.color)} />
          <CardTitle className="text-sm font-semibold leading-snug truncate shrink-0">
            {formatTitle(finding.title)}
          </CardTitle>
          <div className="flex items-center gap-2 shrink-0">
            <Badge
              variant="outline"
              size="xs"
              borderRadius="round"
              className={cn("uppercase tracking-wide", config.color)}
            >
              {finding.severity}
            </Badge>
            {finding.componentName && (
              <Badge variant="secondary" size="xs" borderRadius="round">
                {finding.componentName}
              </Badge>
            )}
          </div>
        </div>
      </CardHeader>
      <CardContent className="space-y-3 pt-3 px-0 pb-0">
        <p className="text-sm text-muted-foreground leading-relaxed">
          {finding.description}
        </p>
 
        {finding.ruleReference && !finding.ruleReference.match(/^GATE\d/) && (
          <div className="text-xs text-muted-foreground">
            <span className="font-medium">Rule:</span> {finding.ruleReference}
          </div>
        )}
 
        {finding.suggestedFix && (
          <div className="space-y-2 p-3 rounded-lg border bg-muted/40">
            <div className="flex items-center gap-2 text-xs font-medium text-foreground">
              <span>Suggested Fix</span>
              <ToolTipCover
                content={
                  finding.severity === SeverityEnum.Error
                    ? "Recommended next step for an error. Validate before applying."
                    : "Initial guess / suggestion. Use as a starting point, not a definitive answer."
                }
                variant={
                  finding.severity === SeverityEnum.Error ? "error" : "warning"
                }
              >
                <span className="text-muted-foreground cursor-help">(?)</span>
              </ToolTipCover>
            </div>
            <p className="text-sm text-muted-foreground">
              {finding.suggestedFix}
            </p>
          </div>
        )}
 
        <div className="flex flex-wrap items-center gap-2 pt-1">
          {canApplyFix && finding.suggestedFix && (
            <Button
              size="sm"
              variant="default"
              onClick={() => finding.id && onApplyFix?.(finding.id)}
              className="flex-1 min-w-[140px]"
            >
              <CheckCircle2 className="h-4 w-4 mr-1" />
              Apply Fix
            </Button>
          )}
          {finding.componentName && onFocusComponent && (
            <ToolTipCover
              asChild
              content={`Locate ${finding.componentName} on the flowsheet`}
            >
              <Button
                size="sm"
                variant="outline"
                onClick={() => onFocusComponent(finding.componentName!)}
                className="min-w-[110px]"
                data-locate-button
                aria-label={`Locate ${finding.componentName}`}
              >
                <LocateFixed className="h-4 w-4 mr-1" />
                Locate
              </Button>
            </ToolTipCover>
          )}
        </div>
      </CardContent>
    </Card>
  );
}