All files / src/pages/flowsheet-page/economics/shared/ui GroupedEconomicsStudySelect.tsx

87.8% Statements 36/41
78.94% Branches 30/38
57.14% Functions 4/7
93.1% Lines 27/29

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                                            107x           107x       107x 107x       107x 40x 147x 187x 107x 107x 79x     118x               1498x 321x 749x     90x               51x         163x   182x   163x                         155x       155x           107x                 118x         118x 118x   107x    
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue,
} from "@/ahuora-design-system/ui/select";
import type { EconomicsStudyRead } from "@/api/apiStore.gen";
 
type GroupedEconomicsStudySelectProps = {
  label?: string;
  ariaLabel: string;
  value: string;
  studies: EconomicsStudyRead[];
  disabled: boolean;
  placeholder?: string;
  routeFlowsheetId?: number;
  onChange: (studyId: string) => void;
};
 
export function GroupedEconomicsStudySelect({
  label,
  ariaLabel,
  value,
  studies,
  disabled,
  placeholder = "Select study",
  routeFlowsheetId,
  onChange,
}: GroupedEconomicsStudySelectProps) {
  const duplicateNames = duplicateEconomicsStudyNames(studies);
  const selectedValue = studies.some((study) => String(study.id) === value)
    ? value
    : undefined;
  const select = (
    <Select value={selectedValue} onValueChange={onChange} disabled={disabled}>
      <SelectTrigger aria-label={ariaLabel}>
        <SelectValue placeholder={placeholder} />
      </SelectTrigger>
      <SelectContent>
        {groupEconomicsStudiesByFlowsheet(studies).map((group) => (
          <SelectGroup key={group.flowsheetId}>
            <SelectLabel>{group.flowsheetName}</SelectLabel>
            {group.studies.map((study) => (
              <SelectItem key={study.id} value={String(study.id)}>
                {economicsStudyDisplayLabel(study, {
                  duplicateNames,
                  routeFlowsheetId,
                })}
              </SelectItem>
            ))}
          </SelectGroup>
        ))}
      </SelectContent>
    </Select>
  );
 
  if (!label) {
    return select;
  }
 
  return (
    <label className="grid gap-1 text-xs font-medium text-muted-foreground">
      {label}
      {select}
    </label>
  );
}
 
export function duplicateEconomicsStudyNames(studies: EconomicsStudyRead[]) {
  const counts = new Map<string, number>();
  for (const study of studies) {
    counts.set(study.name, (counts.get(study.name) ?? 0) + 1);
  }
  return counts;
}
 
export function economicsStudyDisplayLabel(
  study: EconomicsStudyRead,
  {
    duplicateNames,
    routeFlowsheetId,
  }: {
    duplicateNames: Map<string, number>;
    routeFlowsheetId?: number;
  },
) {
  const shouldIncludeFlowsheet =
    (duplicateNames.get(study.name) ?? 0) > 1 ||
    (routeFlowsheetId !== undefined && study.flowsheet !== routeFlowsheetId);
  if (!shouldIncludeFlowsheet) {
    return study.name;
  }
  return `${study.name} - ${study.flowsheet_name}`;
}
 
function groupEconomicsStudiesByFlowsheet(studies: EconomicsStudyRead[]) {
  const groupedStudies = new Map<
    number,
    {
      flowsheetId: number;
      flowsheetName: string;
      studies: EconomicsStudyRead[];
    }
  >();
  for (const study of studies) {
    const group = groupedStudies.get(study.flowsheet) ?? {
      flowsheetId: study.flowsheet,
      flowsheetName: study.flowsheet_name || `Flowsheet ${study.flowsheet}`,
      studies: [],
    };
    group.studies.push(study);
    groupedStudies.set(study.flowsheet, group);
  }
  return [...groupedStudies.values()];
}