All files / src/pages/flowsheet-page/economics/comparison-panel BaselineModePicker.tsx

65.62% Statements 21/32
57.69% Branches 15/26
15.38% Functions 2/13
70.37% Lines 19/27

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                                        103x                     34x   34x 34x                   34x       14x   34x                 1x   68x   14x   34x 14x 14x   28x       2x                                                                         26x                     76x 48x   170x      
import { HelpCircle } from "lucide-react";
import { useState } from "react";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/ahuora-design-system/ui/select";
import {
  Tooltip,
  TooltipContent,
  TooltipPortal,
  TooltipProvider,
  TooltipTrigger,
} from "@/ahuora-design-system/ui/tooltip";
import { EconomicsBaselineMode } from "@/api/apiStore.gen";
 
export type EconomicsBaselineModeValue = EconomicsBaselineMode;
 
const BASELINE_MODE_OPTIONS = [
  {
    value: EconomicsBaselineMode.Manual,
    label: "Manual baseline",
  },
  {
    value: EconomicsBaselineMode.Study,
    label: "Study baseline",
  },
] satisfies { value: EconomicsBaselineModeValue; label: string }[];
 
export function BaselineModePicker({
  value,
  disabled = false,
  studyModeDisabled = false,
  studyModeDisabledReason,
  onChange,
}: {
  value: EconomicsBaselineModeValue;
  disabled?: boolean;
  studyModeDisabled?: boolean;
  studyModeDisabledReason?: string;
  onChange: (value: EconomicsBaselineModeValue) => void;
}) {
  const [studyModeTooltipOpen, setStudyModeTooltipOpen] = useState(false);
 
  return (
    <label className="grid min-w-48 gap-1 text-xs font-medium text-muted-foreground">
      <span className="text-xs font-medium text-muted-foreground">
        Baseline mode
      </span>
      <Select
        value={value}
        disabled={disabled}
        onValueChange={(nextValue) => {
          if (
            nextValue === EconomicsBaselineMode.Manual ||
            nextValue === EconomicsBaselineMode.Study
          ) {
            onChange(nextValue);
          }
        }}
      >
        <SelectTrigger aria-label="Select economics baseline mode">
          <SelectValue placeholder="Select baseline mode" />
        </SelectTrigger>
        <SelectContent>
          {BASELINE_MODE_OPTIONS.map((option) => {
            const isStudyModeUnavailable =
              option.value === EconomicsBaselineMode.Study && studyModeDisabled;
 
            // Keep the help trigger out of the disabled item subtree so it
            // remains focusable/clickable while still appearing on that row.
            if (isStudyModeUnavailable && studyModeDisabledReason) {
              return (
                <div key={option.value} className="relative">
                  <SelectItem value={option.value} disabled className="pr-14">
                    {option.label}
                  </SelectItem>
                  <TooltipProvider delayDuration={0} disableHoverableContent>
                    <Tooltip
                      open={studyModeTooltipOpen}
                      onOpenChange={setStudyModeTooltipOpen}
                    >
                      <TooltipTrigger asChild>
                        <button
                          type="button"
                          className="absolute right-7 top-1/2 z-10 -translate-y-1/2 rounded-sm text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
                          aria-label="Why study baseline mode is unavailable"
                          onBlur={() => setStudyModeTooltipOpen(false)}
                          onClick={(event) => event.stopPropagation()}
                          onFocus={() => setStudyModeTooltipOpen(true)}
                          onMouseEnter={() => setStudyModeTooltipOpen(true)}
                          onMouseLeave={() => setStudyModeTooltipOpen(false)}
                          onPointerDown={(event) => event.stopPropagation()}
                        >
                          <HelpCircle className="size-3.5" />
                        </button>
                      </TooltipTrigger>
                      <TooltipPortal>
                        <TooltipContent side="right" className="max-w-64">
                          <p className="whitespace-pre-line">
                            {studyModeDisabledReason}
                          </p>
                        </TooltipContent>
                      </TooltipPortal>
                    </Tooltip>
                  </TooltipProvider>
                </div>
              );
            }
 
            return (
              <SelectItem
                key={option.value}
                value={option.value}
                disabled={isStudyModeUnavailable}
              >
                {option.label}
              </SelectItem>
            );
          })}
        </SelectContent>
      </Select>
    </label>
  );
}