All files / src/flowsheet-cloning FlowsheetCloneProblemDialog.tsx

23.07% Statements 18/78
15.9% Branches 14/88
8.33% Functions 1/12
26.78% Lines 15/56

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                                                      103x                                                                                                                                                                                                                                                                                                 3036x                 3036x   3036x     9108x               3036x   3036x       3696x 660x     3036x 3696x   3036x                 18216x 5064x   12144x      
import { AlertTriangle } from "lucide-react";
import { useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/ahuora-design-system/ui/dialog";
import { Tabs, TabsList, TabsTrigger } from "@/ahuora-design-system/ui/tabs";
import { ProjectPagination } from "@/pages/main-page/components/ProjectPagination";
import {
  type CloneValidationProblem,
  cloneDeficiencyCopy,
} from "./cloneValidation";
 
type FlowsheetCloneProblemDialogProps = {
  problem: CloneValidationProblem | null;
  scope: "flowsheet" | "project";
  isRepairingAutomatically: boolean;
  onDismiss: () => void;
  onRepairManually: (flowsheetId: number) => void;
  onRepairAutomatically: () => Promise<void> | void;
};
 
const PROBLEMS_PER_PAGE = 5;
 
function ProblemDetails({
  problem,
  scope,
  isRepairingAutomatically,
  onRepairManually,
  onRepairAutomatically,
}: {
  problem: CloneValidationProblem;
  scope: "flowsheet" | "project";
  isRepairingAutomatically: boolean;
  onRepairManually: (flowsheetId: number) => void;
  onRepairAutomatically: () => Promise<void> | void;
}) {
  const [categoryCode, setCategoryCode] = useState(
    problem.categories[0]?.code ?? "",
  );
  const [page, setPage] = useState(1);
  const category =
    problem.categories.find(({ code }) => code === categoryCode) ??
    problem.categories[0];
  const pageCount = Math.max(
    1,
    Math.ceil(category.items.length / PROBLEMS_PER_PAGE),
  );
  const firstIndex = (page - 1) * PROBLEMS_PER_PAGE;
  const visibleItems = category.items.slice(
    firstIndex,
    firstIndex + PROBLEMS_PER_PAGE,
  );
  const repairFlowsheetId = visibleItems[0]?.flowsheetId ?? problem.flowsheetId;
  const copy = cloneDeficiencyCopy(category.code);
 
  return (
    <div className="flex min-h-0 flex-1 flex-col gap-4">
      <div
        className="min-h-0 flex-1 space-y-3 overflow-y-auto pr-1"
        aria-label="Clone problem details"
      >
        <Tabs
          value={category.code}
          onValueChange={(code) => {
            setCategoryCode(code);
            setPage(1);
          }}
        >
          <TabsList
            type="hFull"
            bg="default"
            rounded="yes"
            className="grid h-auto w-full grid-cols-1 gap-1 sm:grid-cols-3"
          >
            {problem.categories.map((entry) => (
              <TabsTrigger key={entry.code} value={entry.code} size="sm">
                {cloneDeficiencyCopy(entry.code).label} ({entry.totalCount})
              </TabsTrigger>
            ))}
          </TabsList>
        </Tabs>
 
        <div>
          <p className="text-sm font-medium">{copy.title}</p>
          <p className="mt-1 text-sm text-muted-foreground">
            {copy.description}
          </p>
        </div>
 
        {visibleItems.length > 0 ? (
          <div className="space-y-2" aria-label="Flowsheet problems">
            {visibleItems.map((item, index) => (
              <div
                key={`${item.flowsheetId}-${item.objectName}-${item.fieldName}-${firstIndex + index}`}
                className="rounded-md border p-3"
              >
                <p className="text-sm font-medium">
                  {item.fieldName} on {item.objectName}
                </p>
                <p className="mt-1 text-xs text-muted-foreground">
                  {item.objectType}
                  {scope === "project" ? ` ยท ${item.flowsheetName}` : ""}
                </p>
                {item.referenceName && (
                  <p className="mt-2 text-sm">
                    Unavailable reference: {item.referenceName}
                  </p>
                )}
              </div>
            ))}
          </div>
        ) : (
          <div className="rounded-md border p-3 text-sm text-muted-foreground">
            {scope === "project" && (
              <p className="mb-2 text-foreground">
                Flowsheet to repair: <strong>{problem.flowsheetName}</strong>
              </p>
            )}
            <p>
              A specific location is not available. Review the flowsheet for
              this type of problem, then retry.
            </p>
          </div>
        )}
 
        {category.truncated && (
          <p className="text-sm text-muted-foreground">
            Showing the first {category.items.length} of {category.totalCount}{" "}
            problems. Repair these, then retry to check the remainder.
          </p>
        )}
 
        <ProjectPagination
          currentPage={page}
          pageCount={pageCount}
          totalProjects={category.items.length}
          currentPageSize={visibleItems.length}
          pageSize={PROBLEMS_PER_PAGE}
          itemLabel="problem"
          ariaLabel="Problem pages"
          onPageChange={setPage}
        />
      </div>
 
      <DialogFooter className="shrink-0">
        <Button
          type="button"
          variant="outline"
          disabled={isRepairingAutomatically}
          onClick={() => onRepairManually(repairFlowsheetId)}
        >
          Repair manually
        </Button>
        <Button
          type="button"
          disabled={isRepairingAutomatically}
          onClick={() => void onRepairAutomatically()}
        >
          {isRepairingAutomatically ? "Repairing..." : "Repair automatically"}
        </Button>
      </DialogFooter>
    </div>
  );
}
 
/** Present repairable clone failures without exposing backend implementation data. */
export function FlowsheetCloneProblemDialog({
  problem,
  scope,
  isRepairingAutomatically,
  onDismiss,
  onRepairManually,
  onRepairAutomatically,
}: FlowsheetCloneProblemDialogProps) {
  return (
    <div onClick={(event) => event.stopPropagation()}>
      <Dialog
        open={problem !== null}
        onOpenChange={(open) => {
          Iif (!open && !isRepairingAutomatically) onDismiss();
        }}
      >
        <DialogContent
          className="flex max-h-[calc(100dvh-2rem)] w-[min(34rem,calc(100vw-2rem))] flex-col overflow-hidden"
          preventDefault={isRepairingAutomatically}
        >
          <DialogHeader className="shrink-0">
            <div className="flex items-center gap-2 text-amber-700">
              <AlertTriangle className="h-5 w-5" aria-hidden="true" />
              <DialogTitle>
                {scope === "project"
                  ? "This project needs attention"
                  : "This flowsheet needs attention"}
              </DialogTitle>
            </div>
            <DialogDescription className="pt-2">
              Ahuora stopped this action because it found problems in the saved
              flowsheet data. No copy or version was created.
            </DialogDescription>
          </DialogHeader>
 
          {problem && (
            <ProblemDetails
              key={JSON.stringify(problem)}
              problem={problem}
              scope={scope}
              isRepairingAutomatically={isRepairingAutomatically}
              onRepairManually={onRepairManually}
              onRepairAutomatically={onRepairAutomatically}
            />
          )}
        </DialogContent>
      </Dialog>
    </div>
  );
}