fix(measurements): remember the last-used type on the add form

The add form always defaulted to BLOOD_PRESSURE regardless of what a
user actually logs day to day, forcing a re-pick from a 15-entry
select on every entry unless arriving via a ?add= deep link. Seed the
default from the last manually-saved type (localStorage, client-only)
when no deep link overrides it, and record the type on every
successful save.
This commit is contained in:
Marc-André Bombeck committed 2026-07-18 03:52:32 +02:00
1 parent 3c468b5e00
commit 9531bbdcec
2 files changed
+61 -1

No files matched your search

@@ -31,6 +31,10 @@ import {
} from "@/lib/query-keys";
import { ApiError, apiPost } from "@/lib/api/api-fetch";
import { MEASUREMENT_NOTES_MAX_LENGTH } from "@/lib/validations/measurement";
import {
getLastUsedMeasurementType,
setLastUsedMeasurementType,
} from "@/lib/measurements/last-used-type";
const MAX_COMMENT_LENGTH = MEASUREMENT_NOTES_MAX_LENGTH;
@@ -234,7 +238,16 @@ export function MeasurementForm({
? "BLOOD_PRESSURE"
: defaultType;
const [type, setType] = useState(normalizedDefault || "BLOOD_PRESSURE");
// v1.30.1 M3 — an explicit deep-link default always wins; absent one,
// seed from the last type the user actually saved rather than always
// landing on BLOOD_PRESSURE. Lazy initializer so the localStorage read
// happens once, at mount, not on every render.
const [type, setType] = useState(
() =>
normalizedDefault ||
getLastUsedMeasurementType(MEASUREMENT_FORM_TYPE_VALUES) ||
"BLOOD_PRESSURE",
);
const [value, setValue] = useState("");
const [sysBp, setSysBp] = useState("");
const [diaBp, setDiaBp] = useState("");
@@ -323,6 +336,10 @@ export function MeasurementForm({
});
}
// v1.30.1 M3 — remember this type as the next mount's smart
// default (deep links still override it).
setLastUsedMeasurementType(type);
// Reset form
setValue("");
setSysBp("");