All files / src/pages/flowsheet-page/economics/operating-lines-panel/ui fields.tsx

71.11% Statements 64/90
56.94% Branches 41/72
100% Functions 8/8
71.42% Lines 40/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 224 225 226 227 228 229 230                              103x   233x                     460x 233x       466x 460x 932x       216x                             240x 288x       251x   251x           502x       212x 212x                       166x 31x   135x           26x 135x 135x     213x       2356x   219x   2575x       68x             68x                           80x 68x         68x 68x 72x   612x 76x 272x       72x                 4x             29x                                                                 4x             47x                                                    
import { Badge } from "@/ahuora-design-system/ui/badge";
import type { EconomicsDefaultRateRead } from "@/api/apiStore.gen";
import {
  RateSourceModeEnum,
  ReviewStatusEnum,
  ValueKindEnum,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
import {
  FormattedNumericInput,
  type NumericInputFormat,
} from "../../shared/ui/EconomicsFormFields";
import type { SaveState } from "../model/types";
 
const WARNING_INVALID_CLASS =
  "aria-[invalid=true]:border-orange-500 aria-[invalid=true]:focus-visible:border-b-orange-500";
 
export function SectionHeading({
  title,
  count,
  help,
}: {
  title: string;
  count?: number;
  help: string;
}) {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <h4 className="text-sm font-semibold">{title}</h4>
      {typeof count === "number" && (
        <Badge variant="secondary" size="xs" borderRadius="round">
          {count}
        </Badge>
      )}
      <HelpBadge help={help} />
    </div>
  );
}
 
export function FieldLabel({
  htmlFor,
  label,
  help,
}: {
  htmlFor: string;
  label: string;
  help: string;
}) {
  return (
    <label
      htmlFor={htmlFor}
      className="flex items-center gap-1 text-xs font-medium"
    >
      {label}
      <HelpBadge help={help} />
    </label>
  );
}
 
export function HelpBadge({ help }: { help: string }) {
  return (
    <span
      className="inline-flex size-4 items-center justify-center rounded-full border text-[10px] text-muted-foreground"
      title={help}
      aria-label={help}
    >
      ?
    </span>
  );
}
 
export function FieldError({ id, message }: { id: string; message?: string }) {
  Iif (!message) return null;
  return (
    <div
      id={id}
      className="text-xs text-orange-700 dark:text-orange-300"
      role="status"
    >
      {message}
    </div>
  );
}
 
export function FormState({ state }: { state: SaveState }) {
  Iif (state.kind === "idle" || !state.message) return null;
  const className =
    state.kind === "error"
      ? "text-destructive"
      : state.kind === "saving"
        ? "text-muted-foreground"
        : "text-emerald-700";
  return (
    <div
      className={`mt-2 text-xs ${className}`}
      role={state.kind === "error" ? "alert" : "status"}
    >
      {state.message}
    </div>
  );
}
 
export function EmptyState({ text }: { text: string }) {
  return (
    <div className="rounded-md border border-dashed p-3 text-sm text-muted-foreground">
      {text}
    </div>
  );
}
 
export function LineInput({
  id,
  label,
  help,
  value,
  disabled,
  error,
  numericFormat = "plain",
  onChange,
}: {
  id: string;
  label: string;
  help: string;
  value: string;
  disabled?: boolean;
  error?: string;
  numericFormat?: NumericInputFormat;
  onChange: (value: string) => void;
}) {
  return (
    <div className="grid gap-1">
      <FieldLabel htmlFor={id} label={label} help={help} />
      <FormattedNumericInput
        id={id}
        value={value}
        numericFormat={numericFormat}
        disabled={disabled}
        aria-invalid={Boolean(error)}
        aria-describedby={error ? `${id}-error` : undefined}
        className={cn(error ? WARNING_INVALID_CLASS : undefined)}
        onValueChange={onChange}
      />
      <FieldError id={`${id}-error`} message={error} />
    </div>
  );
}
 
export function SourceDefaultBadge({
  rate,
  mode,
}: {
  rate?: EconomicsDefaultRateRead;
  mode?: RateSourceModeEnum | "" | null;
}) {
  if (mode === RateSourceModeEnum.ProjectDefault) {
    return (
      <Badge
        variant="secondary"
        size="xs"
        borderRadius="round"
        className="max-w-full truncate"
      >
        Project default
      </Badge>
    );
  }
  if (mode === RateSourceModeEnum.Custom) {
    return (
      <Badge
        variant="secondary"
        size="xs"
        borderRadius="round"
        className="max-w-full truncate"
      >
        Custom rate
      </Badge>
    );
  }
  if (!rate) {
    return (
      <Badge
        variant="secondary"
        size="xs"
        borderRadius="round"
        className="max-w-full truncate"
      >
        No default selected
      </Badge>
    );
  }
  if (
    rate.review_status === ReviewStatusEnum.Reviewed &&
    rate.value_kind === ValueKindEnum.ReviewedDefault &&
    rate.is_numeric_default_available
  ) {
    return (
      <Badge
        variant="secondary"
        size="xs"
        borderRadius="round"
        className="max-w-full truncate"
      >
        {rate.label}
      </Badge>
    );
  }
  if (rate.is_derived_template) {
    return (
      <Badge
        variant="secondary"
        size="xs"
        borderRadius="round"
        className="max-w-full truncate"
      >
        Formula: {rate.label}
      </Badge>
    );
  }
  return (
    <Badge
      variant="secondary"
      size="xs"
      borderRadius="round"
      className="max-w-full truncate"
    >
      No numeric default: {rate.label}
    </Badge>
  );
}