All files / src/pages/flowsheet-page/economics/settings-panel/ui ConfigurationHeader.tsx

84.12% Statements 106/126
51.29% Branches 79/154
66.66% Functions 6/9
86.17% Lines 81/94

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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                                                                                      382x           382x 382x                   382x 382x     401x               382x     382x   29x 29x   382x 411x   411x   401x 25x       407x 432x 557x 513x 676x 382x             1910x 1274x       126x     14x         75x                     41x           10x   20x                   71x                             71x   5x   71x   71x         81x 5x   71x 76x 5x     71x         71x           81x 5x   71x   71x           81x 5x   71x 76x 5x     71x       76x 5x   5x 71x 76x   71x         81x 5x   5x   71x 81x 76x 5x   5x   71x 81x 76x   116x       218x               10x 228x 228x 238x       45x                   45x 90x   90x 90x 135x 135x      
import { CheckCircle2, ChevronDown } from "lucide-react";
import { type ReactNode, useId, useState } from "react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Spinner } from "@/ahuora-design-system/ui/spinner";
import type {
  CostIndexSeriesRead,
  EconomicsSettingsProfileRead,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
import { DefaultBadge, HelpBadge } from "../../shared/ui/EconomicsFormFields";
import {
  DEFAULT_CURRENCY,
  DEFAULT_DISCOUNT_RATE,
  DEFAULT_INDEX_METHOD,
  DEFAULT_PROJECT_LIFETIME,
  indexMethodLabel,
  indexSeriesLabel,
} from "../model/settingsOptions";
import type { SaveState } from "../model/settingsTypes";
import { normalizeDecimal } from "../model/settingsValueUtils";
 
export function ConfigurationHeader({
  title,
  status,
  saveState,
}: {
  title: string;
  status: string;
  saveState: SaveState;
}) {
  return (
    <div className="flex flex-wrap items-center justify-between gap-3">
      <div className="flex items-center gap-2">
        <h3 className="text-sm font-semibold">{title}</h3>
        <Badge variant="secondary" size="xs" borderRadius="round">
          {status}
        </Badge>
      </div>
      <AutosaveStatus state={saveState} />
    </div>
  );
}
 
export function SettingsDisclosureSection({
  title,
  status,
  saveState,
  ariaLabel,
  children,
  nested = false,
  defaultOpen = false,
}: {
  title: string;
  status?: string;
  saveState?: SaveState;
  ariaLabel: string;
  children: ReactNode;
  nested?: boolean;
  defaultOpen?: boolean;
}) {
  const [open, setOpen] = useState(defaultOpen);
  const contentId = useId();
  return (
    <section
      className={cn("rounded-md border bg-card", nested ? "bg-muted/10" : null)}
      role="region"
      aria-label={ariaLabel}
    >
      <div className="flex flex-wrap items-center justify-between gap-3 p-3">
        <button
          type="button"
          className="-m-1 flex min-w-0 flex-1 items-center gap-2 rounded-md p-1 text-left hover:bg-muted/30"
          aria-label={`Toggle ${title}`}
          aria-expanded={open}
          aria-controls={contentId}
          onClick={() => setOpen((current) => !current)}
        >
          <ChevronDown
            className={cn(
              "size-4 shrink-0 text-muted-foreground transition-transform",
              open ? "rotate-180" : null,
            )}
            aria-hidden="true"
          />
          <div className="flex min-w-0 flex-wrap items-center gap-2">
            <h3 className="text-sm font-semibold">{title}</h3>
            {status ? (
              <Badge variant="secondary" size="xs" borderRadius="round">
                {status}
              </Badge>
            ) : null}
          </div>
        </button>
        {saveState ? <AutosaveStatus state={saveState} /> : null}
      </div>
      {open ? (
        <div
          id={contentId}
          className={cn("border-t p-3", nested ? "pt-3" : "pt-4")}
        >
          {children}
        </div>
      ) : null}
    </section>
  );
}
 
export function AutosaveStatus({ state }: { state: SaveState }) {
  if (state.kind === "idle") {
    return (
      <span
        className="text-xs text-muted-foreground"
        aria-label="Autosave idle"
      >
        Autosave
      </span>
    );
  }
  if (state.kind === "saving") {
    return (
      <span
        className="inline-flex items-center gap-1 text-xs text-muted-foreground"
        role="status"
      >
        <Spinner size="small" />
        Saving
      </span>
    );
  }
  if (state.kind === "saved") {
    return (
      <span className="inline-flex items-center gap-1 text-xs text-primary">
        <CheckCircle2 className="size-4" />
        {state.message}
      </span>
    );
  }
  return (
    <span className="text-xs text-destructive" role="alert">
      {state.message}
    </span>
  );
}
 
export function InheritedAssumptionsSummary({
  assumptions,
  indexSeries,
}: {
  assumptions?: EconomicsSettingsProfileRead;
  indexSeries: CostIndexSeriesRead[];
}) {
  return (
    <section
      className="rounded-md border bg-muted/20 p-3"
      aria-label="Shared profile defaults"
    >
      <div className="mb-3 flex items-center gap-2">
        <h4 className="text-sm font-semibold">Shared profile defaults</h4>
        <HelpBadge help="Reference baseline calculations inherit these profile fields. They are read-only here." />
      </div>
      <dl className="grid gap-3 text-sm md:grid-cols-2 xl:grid-cols-3">
        <ReadOnlyDefinition
          label="Currency"
          value={assumptions?.currency || DEFAULT_CURRENCY}
          badge={
            (assumptions?.currency || DEFAULT_CURRENCY).toUpperCase() ===
            DEFAULT_CURRENCY
              ? "NZ default"
              : undefined
          }
        />
        <ReadOnlyDefinition
          label="Basis date"
          value={assumptions?.basis_date || "Not set"}
        />
        <ReadOnlyDefinition
          label="Discount rate"
          value={
            assumptions?.discount_rate_percent
              ? `${assumptions.discount_rate_percent}%`
              : `${DEFAULT_DISCOUNT_RATE}%`
          }
          badge={
            normalizeDecimal(
              assumptions?.discount_rate_percent || DEFAULT_DISCOUNT_RATE,
            ) === normalizeDecimal(DEFAULT_DISCOUNT_RATE)
              ? "NZ default"
              : undefined
          }
        />
        <ReadOnlyDefinition
          label="Project lifetime"
          value={`${assumptions?.project_lifetime_years ?? DEFAULT_PROJECT_LIFETIME} years`}
          badge={
            String(
              assumptions?.project_lifetime_years ?? DEFAULT_PROJECT_LIFETIME,
            ) === DEFAULT_PROJECT_LIFETIME
              ? "NZ default"
              : undefined
          }
        />
        <ReadOnlyDefinition
          label="Corporate tax rate"
          value={`${assumptions?.tax_rate_percent ?? "0.0000"}%`}
        />
        <ReadOnlyDefinition
          label="Depreciation default"
          value={
            assumptions?.depreciation_enabled
              ? `${assumptions.default_depreciation_life_years ?? 10} years, ${assumptions.default_depreciation_salvage_percent ?? "0.0000"}% residual`
              : "Not applied"
          }
        />
        <ReadOnlyDefinition
          label="Index method"
          value={indexMethodLabel(
            assumptions?.inflation_method || DEFAULT_INDEX_METHOD,
          )}
          badge={
            (assumptions?.inflation_method || DEFAULT_INDEX_METHOD) ===
            DEFAULT_INDEX_METHOD
              ? "NZ default"
              : undefined
          }
        />
        <ReadOnlyDefinition
          label="Capital index"
          value={indexSeriesLabel(
            indexSeries,
            assumptions?.capital_index_series,
          )}
        />
        <ReadOnlyDefinition
          label="Operating index"
          value={indexSeriesLabel(
            indexSeries,
            assumptions?.operating_index_series,
          )}
        />
      </dl>
    </section>
  );
}
 
export function ReadOnlyField({
  label,
  value,
}: {
  label: string;
  value: ReactNode;
}) {
  return (
    <div>
      <div className="text-xs font-medium text-muted-foreground">{label}</div>
      <div className="mt-1 text-sm">{value}</div>
    </div>
  );
}
 
export function ReadOnlyDefinition({
  label,
  value,
  badge,
}: {
  label: string;
  value: ReactNode;
  badge?: string;
}) {
  return (
    <div>
      <dt className="text-xs font-medium text-muted-foreground">{label}</dt>
      <dd className="mt-1 flex flex-wrap items-center gap-2">
        <span>{value}</span>
        {badge && <DefaultBadge label={badge} />}
      </dd>
    </div>
  );
}