Files
HealthLog/src/components/medications/wizard/steps/Step3Dose.tsx
T
Marc-André Bombeck f4dec8c28f feat(medications): fractional dosing via decimal units per dose (#316)
A dose can now consume a fraction of a unit — a half, third or quarter
tablet for a split pill. The wizard offers a curated fraction set beside
the whole numbers (the most error-resistant input); units-per-dose and the
per-container counts widen from Int to Decimal, so a 30-tablet pack dosed
at a half reads 29.5 after one dose and the days-left projection follows.
The consumption ledger stamps and refunds the exact fraction, and existing
whole-number doses are untouched. OpenAPI regenerated — the three unit
fields move from integer to number — and the units-per-dose hint is
updated across the six locales.

Also anchors a first-use date relative to now in an inventory route test
that had aged past its 30-day in-use window.
2026-06-13 16:07:50 +02:00

293 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useState } from "react";
import { ChevronDown, ChevronUp } from "lucide-react";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Button } from "@/components/ui/button";
import { Switch } from "@/components/ui/switch";
import { Checkbox } from "@/components/ui/checkbox";
import {
INJECTION_SITE_KEYS,
describeInjectionSite,
type InjectionSiteKey,
} from "@/lib/medications/injection-sites";
import type { MedicationDeliveryForm } from "@/lib/validations/medication";
import { unitsPerDoseOptionsFor } from "@/components/medications/units-per-dose";
import { useTranslations } from "@/lib/i18n/context";
import type { StepProps } from "./Step1Name";
/**
* Dose unit choices, ordered so mg / ml / IE / µg lead and the
* device-form units (Tablette, Hub, Sprühstoß, …) follow. Mirrors the
* v1.5.3 list with the maintainer-requested order tightening.
*/
const DOSE_UNIT_KEYS = [
"mg",
"ml",
"iu",
"mcg",
"g",
"tablets",
"capsules",
"drops",
"puffs",
"sprays",
"pieces",
"other",
] as const;
const DELIVERY_FORM_KEYS: readonly MedicationDeliveryForm[] = [
"ORAL",
"INJECTION",
"OTHER",
];
export function Step3Dose({ payload, applyPartial }: StepProps) {
const { t } = useTranslations();
// v1.8.5 — injection-site tracking is a per-medication opt-in surfaced
// only for an INJECTION delivery form. When enabled, the user can
// restrict the allowed sites; an empty selection means "no
// restriction" (every site offered, minus the global exclusion).
const [allowedOpen, setAllowedOpen] = useState(false);
const isInjection = payload.deliveryForm === "INJECTION";
function toggleAllowedSite(site: InjectionSiteKey, checked: boolean) {
const current = new Set(payload.allowedInjectionSites);
if (checked) current.add(site);
else current.delete(site);
applyPartial({
allowedInjectionSites: INJECTION_SITE_KEYS.filter((s) => current.has(s)),
});
}
return (
<div className="space-y-6" data-slot="wizard-step3">
<div className="grid grid-cols-2 gap-3">
<div className="space-y-2">
<Label htmlFor="wizard-dose-amount" className="text-sm">
{t("medications.wizard.steps.step3.amountLabel")}
</Label>
<Input
id="wizard-dose-amount"
type="text"
inputMode="decimal"
value={payload.doseAmount}
onChange={(e) => applyPartial({ doseAmount: e.target.value })}
placeholder={t("medications.wizard.steps.step3.amountPlaceholder")}
maxLength={20}
autoComplete="off"
enterKeyHint="next"
/>
</div>
<div className="space-y-2">
<Label htmlFor="wizard-dose-unit" className="text-sm">
{t("medications.wizard.steps.step3.unitLabel")}
</Label>
<Select
value={payload.doseUnit}
onValueChange={(v) => applyPartial({ doseUnit: v })}
>
<SelectTrigger id="wizard-dose-unit" className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{DOSE_UNIT_KEYS.map((u) => (
<SelectItem key={u} value={u}>
{t(`medications.wizard.steps.step3.unit.${u}`)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
{/* v1.6.0 — route of administration + inventory. Decoupled from
the Step 2 treatment row so an INJECTION delivery surfaces the
rotation map for any class, not only GLP-1. */}
<div className="grid grid-cols-2 gap-3">
<div className="space-y-2">
<Label htmlFor="wizard-delivery-form" className="text-sm">
{t("medications.wizard.steps.step3.deliveryFormLabel")}
</Label>
<Select
value={payload.deliveryForm}
onValueChange={(v) =>
applyPartial({ deliveryForm: v as MedicationDeliveryForm })
}
>
<SelectTrigger id="wizard-delivery-form" className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{DELIVERY_FORM_KEYS.map((d) => (
<SelectItem key={d} value={d}>
{t(`medications.wizard.steps.step3.deliveryForm.${d}`)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="wizard-doses-per-unit" className="text-sm">
{t("medications.wizard.steps.step3.dosesPerUnitLabel")}
</Label>
<Input
id="wizard-doses-per-unit"
type="text"
inputMode="numeric"
value={payload.dosesPerUnit}
onChange={(e) =>
applyPartial({
// v1.16.10 — the cap rose to 1000, so four digits.
dosesPerUnit: e.target.value.replace(/[^0-9]/g, "").slice(0, 4),
})
}
placeholder={t(
"medications.wizard.steps.step3.dosesPerUnitPlaceholder",
)}
maxLength={4}
autoComplete="off"
/>
<p className="text-muted-foreground text-xs">
{t("medications.wizard.steps.step3.dosesPerUnitHint")}
</p>
</div>
</div>
{/* v1.16.10 — units consumed per dose. The inventory counts units
(tablets / ampoules); a multi-unit dose (2 × 2 mg for 4 mg)
decrements several per take and every dose-level readout
divides by this factor. */}
<div className="space-y-2">
<Label id="wizard-units-per-dose-label" className="text-sm">
{t("medications.wizard.steps.step3.unitsPerDoseLabel")}
</Label>
{/* v1.16.12 (#316) — curated fraction / whole-number selector
instead of a free-text field: split-pill doses (½ tablet) are
now expressible, and a button set is the most error-resistant
input (no ambiguous decimal separators, no out-of-set values
the server would reject). The decimal value is what the
payload + API carry; the button shows the glyph. */}
<div
role="group"
aria-labelledby="wizard-units-per-dose-label"
data-slot="wizard-units-per-dose"
className="flex flex-wrap gap-1.5"
>
{unitsPerDoseOptionsFor(payload.unitsPerDose).map((opt) => {
const selected = payload.unitsPerDose === opt.raw;
return (
<Button
key={opt.raw}
type="button"
size="sm"
variant={selected ? "default" : "outline"}
aria-pressed={selected}
className="min-w-10 tabular-nums"
onClick={() => applyPartial({ unitsPerDose: opt.raw })}
>
{opt.label}
</Button>
);
})}
</div>
<p className="text-muted-foreground text-xs">
{t("medications.wizard.steps.step3.unitsPerDoseHint")}
</p>
</div>
{/* v1.8.5 — injection-site tracking opt-in + allowed-sites editor.
Shown only for an INJECTION delivery form. Default off; enabling
it surfaces the post-dose site prompt and the rotation
suggestion. The allowed-sites checklist restricts the picker
(empty = no restriction). */}
{isInjection && (
<div
className="border-border/60 space-y-3 rounded-lg border p-3"
data-slot="wizard-injection-site"
>
<label className="flex items-start justify-between gap-3">
<span className="space-y-0.5">
<span className="block text-sm font-medium">
{t("medications.trackInjectionSitesToggle")}
</span>
<span className="text-muted-foreground block text-xs">
{t("medications.trackInjectionSitesHint")}
</span>
</span>
<Switch
checked={payload.trackInjectionSites}
onCheckedChange={(checked) =>
applyPartial({
trackInjectionSites: checked,
// Clear the per-med restriction when tracking is turned off.
...(checked ? {} : { allowedInjectionSites: [] }),
})
}
aria-label={t("medications.trackInjectionSitesToggle")}
/>
</label>
{payload.trackInjectionSites && (
<div className="space-y-2">
<Button
type="button"
variant="ghost"
className="h-9 w-full justify-between px-2"
aria-expanded={allowedOpen}
onClick={() => setAllowedOpen((prev) => !prev)}
>
<span className="text-sm font-medium">
{t("medications.allowedSitesLabel")}
</span>
{allowedOpen ? (
<ChevronUp aria-hidden="true" className="h-4 w-4" />
) : (
<ChevronDown aria-hidden="true" className="h-4 w-4" />
)}
</Button>
{allowedOpen && (
<div className="space-y-2 px-1">
<p className="text-muted-foreground text-xs">
{t("medications.allowedSitesHint")}
</p>
<div className="grid grid-cols-2 gap-2">
{INJECTION_SITE_KEYS.map((site) => {
const checked =
payload.allowedInjectionSites.includes(site);
return (
<label
key={site}
className="flex items-center gap-2 text-sm"
>
<Checkbox
checked={checked}
onCheckedChange={(c) =>
toggleAllowedSite(site, c === true)
}
/>
{t(describeInjectionSite(site))}
</label>
);
})}
</div>
</div>
)}
</div>
)}
</div>
)}
</div>
);
}