mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
Merge branch 'v1423/labs' into release/v1.42.3
This commit is contained in:
commit
42b66b2228
72 files changed
+4115
-146
No files matched your search
@@ -28,6 +28,7 @@ const MARKER: BiomarkerDto = {
|
||||
lowerBound: 70,
|
||||
upperBound: 99,
|
||||
panel: null,
|
||||
analyteKey: null,
|
||||
hasContext: false,
|
||||
context: null,
|
||||
hidden: false,
|
||||
|
||||
@@ -51,6 +51,7 @@ const MARKER: BiomarkerDto = {
|
||||
lowerBound: 0,
|
||||
upperBound: 100,
|
||||
panel: null,
|
||||
analyteKey: null,
|
||||
hasContext: false,
|
||||
context: null,
|
||||
hidden: false,
|
||||
@@ -67,6 +68,9 @@ function reading(id: string, value: number, takenAt: string): LabResultDto {
|
||||
value,
|
||||
valueText: null,
|
||||
unit: "mg/dL",
|
||||
converted: false,
|
||||
sourceValue: null,
|
||||
sourceUnit: null,
|
||||
referenceLow: 0,
|
||||
referenceHigh: 100,
|
||||
catalogReferenceLow: 0,
|
||||
@@ -74,6 +78,7 @@ function reading(id: string, value: number, takenAt: string): LabResultDto {
|
||||
sourceReferenceLow: null,
|
||||
sourceReferenceHigh: null,
|
||||
sourceReferenceText: null,
|
||||
printedReferenceText: null,
|
||||
referenceOrigin: "catalog",
|
||||
referenceDivergesFromCatalog: false,
|
||||
takenAt,
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
|
||||
import { LabConvertedNote } from "../lab-converted-note";
|
||||
|
||||
function render(
|
||||
reading: Parameters<typeof LabConvertedNote>[0]["reading"],
|
||||
locale: "en" | "de" = "en",
|
||||
) {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale={locale}>
|
||||
<LabConvertedNote reading={reading} />
|
||||
</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("<LabConvertedNote>", () => {
|
||||
it("says what the report printed beside a converted reading", () => {
|
||||
const html = render({
|
||||
converted: true,
|
||||
sourceValue: 5.6,
|
||||
sourceUnit: "mmol/L",
|
||||
});
|
||||
expect(html).toContain('data-slot="lab-converted-note"');
|
||||
expect(html).toContain("converted from 5.6 mmol/L");
|
||||
});
|
||||
|
||||
it("formats the number for the reader's language", () => {
|
||||
expect(
|
||||
render({ converted: true, sourceValue: 5.6, sourceUnit: "mmol/L" }, "de"),
|
||||
).toContain("umgerechnet aus 5,6 mmol/L");
|
||||
});
|
||||
|
||||
it("trims a long fraction the way the reading itself is trimmed", () => {
|
||||
expect(
|
||||
render({ converted: true, sourceValue: 5.6123456, sourceUnit: "mmol/L" }),
|
||||
).toContain("converted from 5.61 mmol/L");
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ converted: false, sourceValue: null, sourceUnit: null }],
|
||||
[{ converted: true, sourceValue: null, sourceUnit: "mmol/L" }],
|
||||
[{ converted: true, sourceValue: 5.6, sourceUnit: null }],
|
||||
])("renders nothing for %j", (reading) => {
|
||||
expect(render(reading)).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -29,6 +29,9 @@ function reading(over: Partial<LabResultDto>): LabResultDto {
|
||||
value: 80,
|
||||
valueText: null,
|
||||
unit: "ng/mL",
|
||||
converted: false,
|
||||
sourceValue: null,
|
||||
sourceUnit: null,
|
||||
referenceLow: 30,
|
||||
referenceHigh: 400,
|
||||
catalogReferenceLow: 30,
|
||||
@@ -165,4 +168,21 @@ describe("<LabList> columns", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("notes where a converted reading came from, and only for that reading", () => {
|
||||
const html = renderList([
|
||||
reading({
|
||||
id: "c",
|
||||
biomarkerId: "b1",
|
||||
analyte: "Glucose",
|
||||
unit: "mg/dL",
|
||||
converted: true,
|
||||
sourceValue: 5.6,
|
||||
sourceUnit: "mmol/L",
|
||||
}),
|
||||
reading({ id: "d", biomarkerId: "b2", analyte: "Ferritin" }),
|
||||
]);
|
||||
expect(count(html, 'data-slot="lab-converted-note"')).toBe(1);
|
||||
expect(html).toContain("converted from 5.6 mmol/L");
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,8 @@ import { seedReviewRows } from "../ocr-review-types";
|
||||
import {
|
||||
collectRowErrors,
|
||||
planSave,
|
||||
readingConversion,
|
||||
readingUnitBlocked,
|
||||
readingUnitDiffers,
|
||||
toCommitRow,
|
||||
validateReviewRow,
|
||||
@@ -24,6 +26,7 @@ function row(overrides: Partial<OcrReviewRow> = {}): OcrReviewRow {
|
||||
confidence: { analyte: 1, value: 1, unit: 1, range: 1 },
|
||||
biomarkerMatch: "existing",
|
||||
markerUnit: "mmol/L",
|
||||
markerAnalyteKey: null,
|
||||
duplicateOf: null,
|
||||
confirmed: true,
|
||||
...overrides,
|
||||
@@ -200,6 +203,7 @@ describe("a reading in another unit than its marker's", () => {
|
||||
confidence: { analyte: 1, value: 1, unit: 1, range: 1 },
|
||||
biomarkerMatch: "existing" as const,
|
||||
markerUnit,
|
||||
markerAnalyteKey: null,
|
||||
duplicateOf: null,
|
||||
});
|
||||
const [same, other, fresh] = seedReviewRows(
|
||||
@@ -212,3 +216,85 @@ describe("a reading in another unit than its marker's", () => {
|
||||
expect(fresh.confirmed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("a reading in another unit that can be converted", () => {
|
||||
// Glucose stated in mmol/L against a marker kept in mg/dL, and the analyte is
|
||||
// known: Save converts it, so nothing blocks and the row starts selected.
|
||||
const glucose = (overrides: Partial<OcrReviewRow> = {}) =>
|
||||
row({
|
||||
analyte: "Glucose",
|
||||
value: 5.6,
|
||||
unit: "mmol/L",
|
||||
markerUnit: "mg/dL",
|
||||
markerAnalyteKey: "fasting-glucose",
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it("previews what Save will store, beside what the report stated", () => {
|
||||
const conversion = readingConversion(glucose());
|
||||
expect(conversion).toMatchObject({
|
||||
unit: "mg/dL",
|
||||
statedValue: 5.6,
|
||||
statedUnit: "mmol/L",
|
||||
});
|
||||
expect(conversion!.value).toBeCloseTo(5.6 * 18.0182, 9);
|
||||
});
|
||||
|
||||
it("is not blocked, and not a validation error", () => {
|
||||
expect(readingUnitDiffers(glucose())).toBe(true);
|
||||
expect(readingUnitBlocked(glucose())).toBe(false);
|
||||
expect(validateReviewRow(glucose())).toEqual({});
|
||||
});
|
||||
|
||||
it("sends what the report printed, for the server to convert", () => {
|
||||
const payload = toCommitRow(glucose());
|
||||
expect(payload).toMatchObject({
|
||||
analyte: "Glucose",
|
||||
value: 5.6,
|
||||
unit: "mmol/L",
|
||||
});
|
||||
expect(planSave([glucose()]).kind).toBe("ready");
|
||||
});
|
||||
|
||||
it("starts selected on the review screen", () => {
|
||||
const dto = {
|
||||
analyte: "Glucose",
|
||||
value: 5.6,
|
||||
valueText: null,
|
||||
unit: "mmol/L",
|
||||
referenceLow: null,
|
||||
referenceHigh: null,
|
||||
referenceText: null,
|
||||
takenAt: "2026-06-10",
|
||||
confidence: { analyte: 1, value: 1, unit: 1, range: 1 },
|
||||
biomarkerMatch: "existing" as const,
|
||||
markerUnit: "mg/dL",
|
||||
markerAnalyteKey: "fasting-glucose",
|
||||
duplicateOf: null,
|
||||
};
|
||||
const [converts, blocked] = seedReviewRows(
|
||||
[dto, { ...dto, markerAnalyteKey: null }],
|
||||
null,
|
||||
);
|
||||
expect(converts.confirmed).toBe(true);
|
||||
expect(blocked.confirmed).toBe(false);
|
||||
});
|
||||
|
||||
it("stays blocked when the marker has no known analyte, or the pair is not in the registry", () => {
|
||||
expect(readingUnitBlocked(glucose({ markerAnalyteKey: null }))).toBe(true);
|
||||
expect(readingConversion(glucose({ markerAnalyteKey: null }))).toBeNull();
|
||||
expect(validateReviewRow(glucose({ markerAnalyteKey: null }))).toEqual({
|
||||
unitMismatch: true,
|
||||
});
|
||||
// The analyte is known but the unit is not one it reaches.
|
||||
expect(readingUnitBlocked(glucose({ unit: "%" }))).toBe(true);
|
||||
});
|
||||
|
||||
it("has nothing to preview when the units agree or the row is qualitative", () => {
|
||||
expect(readingConversion(glucose({ unit: "mg/dL" }))).toBeNull();
|
||||
expect(
|
||||
readingConversion(glucose({ value: null, valueText: "negative" })),
|
||||
).toBeNull();
|
||||
expect(readingConversion(glucose({ value: null }))).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -19,6 +19,7 @@ const baseRow: OcrReviewRow = {
|
||||
confidence: { analyte: 1, value: 1, unit: 1, range: 1 },
|
||||
biomarkerMatch: "existing",
|
||||
markerUnit: "mmol/L",
|
||||
markerAnalyteKey: null,
|
||||
duplicateOf: null,
|
||||
confirmed: true,
|
||||
};
|
||||
@@ -155,3 +156,54 @@ describe("<OcrRowEditor> unit differs from the marker's", () => {
|
||||
expect(html).toContain("Geführt in mmol/L; dieser Befund nennt mg/dL");
|
||||
});
|
||||
});
|
||||
|
||||
describe("<OcrRowEditor> unit differs but converts", () => {
|
||||
const converts: OcrReviewRow = {
|
||||
...baseRow,
|
||||
analyte: "Glucose",
|
||||
value: 5.6,
|
||||
takenAt: "2026-06-10",
|
||||
unit: "mmol/L",
|
||||
markerUnit: "mg/dL",
|
||||
markerAnalyteKey: "fasting-glucose",
|
||||
};
|
||||
|
||||
it("says what Save will store and what the report stated, not that it is blocked", () => {
|
||||
const html = render(undefined, "en", converts);
|
||||
expect(html).toContain('data-slot="ocr-row-converted-badge"');
|
||||
expect(html).toContain("Saved as 100.9 mg/dL, converted from 5.6 mmol/L");
|
||||
expect(html).not.toContain("Tracked in");
|
||||
expect(html).not.toContain('aria-invalid="true"');
|
||||
});
|
||||
|
||||
it("wraps the sentence instead of cutting off the printed value", () => {
|
||||
const html = render(undefined, "en", converts);
|
||||
const badge =
|
||||
/<span[^>]*data-slot="ocr-row-converted-badge"[^>]*>([\s\S]*?)<\/span><\/span>/.exec(
|
||||
html,
|
||||
);
|
||||
expect(badge).not.toBeNull();
|
||||
const tag = badge![0].slice(0, badge![0].indexOf(">"));
|
||||
expect(tag).toContain("whitespace-normal");
|
||||
expect(badge![0]).not.toContain("truncate");
|
||||
});
|
||||
|
||||
it("speaks German too", () => {
|
||||
const html = render(undefined, "de", converts);
|
||||
expect(html).toContain("umgerechnet aus 5,6 mmol/L");
|
||||
});
|
||||
|
||||
it("shows nothing when the units agree", () => {
|
||||
const html = render(undefined, "en", { ...converts, unit: "mg/dL" });
|
||||
expect(html).not.toContain("ocr-row-converted-badge");
|
||||
});
|
||||
|
||||
it("still says it is blocked when the analyte is not known", () => {
|
||||
const html = render(undefined, "en", {
|
||||
...converts,
|
||||
markerAnalyteKey: null,
|
||||
});
|
||||
expect(html).toContain("Tracked in mg/dL; this report says mmol/L");
|
||||
expect(html).not.toContain("ocr-row-converted-badge");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
|
||||
import { SourceRangeNote } from "../source-range-note";
|
||||
|
||||
type Reading = Parameters<typeof SourceRangeNote>[0]["reading"];
|
||||
|
||||
function render(reading: Reading) {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale="en">
|
||||
<SourceRangeNote reading={reading} />
|
||||
</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
const STATED: Reading = {
|
||||
unit: "mg/dL",
|
||||
referenceLow: 70,
|
||||
referenceHigh: 99,
|
||||
catalogReferenceLow: 70,
|
||||
catalogReferenceHigh: 99,
|
||||
sourceReferenceText: "70 - 99",
|
||||
printedReferenceText: null,
|
||||
sourceUnit: null,
|
||||
referenceOrigin: "source",
|
||||
referenceDivergesFromCatalog: false,
|
||||
};
|
||||
|
||||
// 5.6 mmol/L stored as 100.9 mg/dL, with the report's 3.9 to 5.5 mmol/L window
|
||||
// converted alongside it.
|
||||
const CONVERTED: Reading = {
|
||||
unit: "mg/dL",
|
||||
referenceLow: 70.27,
|
||||
referenceHigh: 99.1,
|
||||
catalogReferenceLow: 70,
|
||||
catalogReferenceHigh: 100,
|
||||
sourceReferenceText: null,
|
||||
printedReferenceText: "3.9 - 5.5",
|
||||
sourceUnit: "mmol/L",
|
||||
referenceOrigin: "source",
|
||||
referenceDivergesFromCatalog: true,
|
||||
};
|
||||
|
||||
describe("<SourceRangeNote>", () => {
|
||||
it("names a stated reading's window verbatim", () => {
|
||||
const html = render(STATED);
|
||||
expect(html).toContain("Report range 70 - 99");
|
||||
expect(html).not.toContain("as printed");
|
||||
});
|
||||
|
||||
it("states a converted reading's window in the marker's unit", () => {
|
||||
const html = render(CONVERTED);
|
||||
expect(html).toContain("Report range 70.27–99.1 mg/dL");
|
||||
});
|
||||
|
||||
it("keeps the printed text as evidence, labelled with the unit it is in", () => {
|
||||
const html = render(CONVERTED);
|
||||
expect(html).toContain("as printed in mmol/L: 3.9 - 5.5");
|
||||
});
|
||||
|
||||
it("never puts the printed numbers in the statement about the stored unit", () => {
|
||||
const html = render(CONVERTED);
|
||||
// The window in force is followed by the marker's unit, not the printed text.
|
||||
expect(html).not.toContain("Report range 3.9 - 5.5");
|
||||
expect(html).toContain("your range 70–100 mg/dL");
|
||||
});
|
||||
|
||||
it("renders nothing when the window in force is not the report's", () => {
|
||||
expect(render({ ...CONVERTED, referenceOrigin: "catalog" })).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { formatLabValue } from "@/lib/labs/format-value";
|
||||
|
||||
import { useLabNumber } from "./use-lab-format";
|
||||
|
||||
/**
|
||||
* A quiet "converted from 5.6 mmol/L" beside a reading whose stored number is a
|
||||
* conversion of what the report printed, so a number nobody typed can always be
|
||||
* traced back. Renders nothing for a reading stored as stated.
|
||||
*/
|
||||
export function LabConvertedNote({
|
||||
reading,
|
||||
className,
|
||||
}: {
|
||||
reading: {
|
||||
converted: boolean;
|
||||
sourceValue: number | null;
|
||||
sourceUnit: string | null;
|
||||
};
|
||||
className?: string;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const labNumber = useLabNumber();
|
||||
if (
|
||||
!reading.converted ||
|
||||
reading.sourceValue === null ||
|
||||
reading.sourceUnit === null
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<span
|
||||
data-slot="lab-converted-note"
|
||||
className={className ?? "text-muted-foreground text-xs"}
|
||||
>
|
||||
{t("labs.convertedFrom", {
|
||||
value: formatLabValue(reading.sourceValue, labNumber),
|
||||
unit: reading.sourceUnit,
|
||||
})}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@@ -30,7 +30,9 @@ import { useTableSort } from "@/hooks/use-table-sort";
|
||||
import { apiDelete, apiGet, apiPost, apiPut } from "@/lib/api/api-fetch";
|
||||
import { localizedApiError } from "@/lib/api/localized-error";
|
||||
import { formatLabReading } from "@/lib/labs/format-value";
|
||||
import { LabConvertedNote } from "./lab-converted-note";
|
||||
import { resolveNoteForUpdate } from "@/lib/labs/note-update";
|
||||
import { resolveValueForUpdate, valueEditSeed } from "@/lib/labs/value-update";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
@@ -176,7 +178,8 @@ export function LabHistoryList({ readings }: { readings: LabResultDto[] }) {
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: (body: {
|
||||
id: string;
|
||||
// Exactly one of value / valueText is set, matching the row's type.
|
||||
// At most one of value / valueText is set, matching the row's type;
|
||||
// null leaves the field out of the PUT (an untouched value).
|
||||
value: number | null;
|
||||
valueText: string | null;
|
||||
takenAt: string;
|
||||
@@ -218,7 +221,7 @@ export function LabHistoryList({ readings }: { readings: LabResultDto[] }) {
|
||||
|
||||
async function openEdit(reading: LabResultDto) {
|
||||
const seed = {
|
||||
value: reading.value !== null ? String(reading.value) : "",
|
||||
value: reading.value !== null ? valueEditSeed(reading.value) : "",
|
||||
valueText: reading.valueText ?? "",
|
||||
takenAt: toDateTimeLocal(reading.takenAt),
|
||||
note: "",
|
||||
@@ -274,11 +277,20 @@ export function LabHistoryList({ readings }: { readings: LabResultDto[] }) {
|
||||
}
|
||||
qualitativeValue = trimmed;
|
||||
} else {
|
||||
numericValue = parseDecimal(editValue);
|
||||
if (numericValue === null) {
|
||||
const parsed = parseDecimal(editValue);
|
||||
if (parsed === null) {
|
||||
setEditError(t("labs.form.requiredError"));
|
||||
return;
|
||||
}
|
||||
// An untouched field is left out of the PUT (null here), so the stored
|
||||
// full-precision value and a conversion record survive a date or note
|
||||
// edit.
|
||||
numericValue =
|
||||
resolveValueForUpdate({
|
||||
seed: editSeed.value,
|
||||
editorValue: editValue,
|
||||
parsed,
|
||||
}) ?? null;
|
||||
}
|
||||
const takenDate = new Date(editTakenAt);
|
||||
if (Number.isNaN(takenDate.getTime())) {
|
||||
@@ -381,6 +393,10 @@ export function LabHistoryList({ readings }: { readings: LabResultDto[] }) {
|
||||
<div className="min-w-0 flex-1 space-y-2">
|
||||
<div className="font-semibold tabular-nums">
|
||||
{formatLabReading(r, labNumber)}
|
||||
<LabConvertedNote
|
||||
reading={r}
|
||||
className="text-muted-foreground ml-2 text-xs font-normal"
|
||||
/>
|
||||
</div>
|
||||
<LabReferenceRangeBar
|
||||
value={r.value}
|
||||
@@ -453,6 +469,10 @@ export function LabHistoryList({ readings }: { readings: LabResultDto[] }) {
|
||||
<TableCell className="font-semibold tabular-nums">
|
||||
<div className="space-y-2">
|
||||
<span>{formatLabReading(r, labNumber)}</span>
|
||||
<LabConvertedNote
|
||||
reading={r}
|
||||
className="text-muted-foreground block text-xs font-normal"
|
||||
/>
|
||||
<LabReferenceRangeBar
|
||||
value={r.value}
|
||||
referenceLow={r.referenceLow}
|
||||
|
||||
@@ -15,6 +15,7 @@ import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { apiGet } from "@/lib/api/api-fetch";
|
||||
import { formatReferenceRange } from "@/lib/labs/reference-range";
|
||||
import { formatLabReading } from "@/lib/labs/format-value";
|
||||
import { LabConvertedNote } from "./lab-converted-note";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
@@ -299,6 +300,7 @@ export function LabList({
|
||||
{formatLabReading(group.latest, labNumber)}
|
||||
</span>
|
||||
<span>{labDate(group.latest.takenAt)}</span>
|
||||
<LabConvertedNote reading={group.latest} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@@ -429,6 +431,7 @@ export function LabList({
|
||||
<span className="text-xs">
|
||||
{labDate(group.latest.takenAt)}
|
||||
</span>
|
||||
<LabConvertedNote reading={group.latest} />
|
||||
{group.readings.length > 1 ? (
|
||||
<span className="text-xs">
|
||||
{t("labs.readingsCount", {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
import type { OcrExtractedRowDto } from "@/lib/validations/labs-ocr";
|
||||
|
||||
import { readingUnitDiffers } from "./ocr-review-validation";
|
||||
import { readingUnitBlocked } from "./ocr-review-validation";
|
||||
|
||||
export interface OcrReviewRow {
|
||||
/** Stable client key for the list (the analyte+index at seed time). */
|
||||
@@ -26,9 +26,12 @@ export interface OcrReviewRow {
|
||||
biomarkerMatch: "new" | "existing";
|
||||
/**
|
||||
* The unit the matched marker is tracked in; null for a marker that does not
|
||||
* exist yet. A numeric row whose `unit` differs from it cannot be saved.
|
||||
* exist yet. A numeric row whose `unit` differs from it is converted on
|
||||
* Save when it can be (see `readingConversion`) and cannot be saved when not.
|
||||
*/
|
||||
markerUnit: string | null;
|
||||
/** Which analyte the matched marker is (catalog slug or registry-only key), or null. */
|
||||
markerAnalyteKey: string | null;
|
||||
/** Non-null when the server flagged a likely duplicate of a live reading. */
|
||||
duplicateOf: string | null;
|
||||
/** Whether the row is selected for saving. Defaults false for duplicates. */
|
||||
@@ -53,17 +56,21 @@ export function seedReviewRows(
|
||||
confidence: row.confidence,
|
||||
biomarkerMatch: row.biomarkerMatch,
|
||||
markerUnit: row.markerUnit,
|
||||
markerAnalyteKey: row.markerAnalyteKey,
|
||||
duplicateOf: row.duplicateOf,
|
||||
// A flagged duplicate starts unchecked, and so does a reading in another
|
||||
// unit than its marker's (it carries a badge saying why, and ticking it
|
||||
// marks the unit field until it is fixed); everything else starts
|
||||
// confirmed.
|
||||
// unit than its marker's that cannot be converted (it carries a badge
|
||||
// saying why, and ticking it marks the unit field until it is fixed); a
|
||||
// reading that converts starts confirmed, with a badge saying what Save
|
||||
// will store; everything else starts confirmed.
|
||||
confirmed:
|
||||
row.duplicateOf === null &&
|
||||
!readingUnitDiffers({
|
||||
!readingUnitBlocked({
|
||||
value: row.value,
|
||||
valueText: row.valueText,
|
||||
unit: row.unit,
|
||||
markerUnit: row.markerUnit,
|
||||
markerAnalyteKey: row.markerAnalyteKey,
|
||||
}),
|
||||
}));
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
* build the payload: it returns null exactly when `validateReviewRow` reports
|
||||
* at least one field.
|
||||
*/
|
||||
import { convertLabValue } from "@/lib/labs/unit-conversion/convert";
|
||||
import { sameLabUnit } from "@/lib/labs/unit-normalise";
|
||||
|
||||
import type { OcrReviewRow } from "./ocr-review-types";
|
||||
@@ -46,6 +47,60 @@ export function readingUnitDiffers(row: {
|
||||
return !sameLabUnit(stated, row.markerUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* What Save will store for a numeric row whose unit differs from its marker's
|
||||
* and can be converted: the value in the marker's unit, beside what the report
|
||||
* stated. Null when the units agree, the row is qualitative, or the pair has no
|
||||
* conversion. The commit does the same conversion on the server; this only lets
|
||||
* the screen say so before Save.
|
||||
*/
|
||||
export function readingConversion(row: {
|
||||
value: number | null | undefined;
|
||||
valueText: string | null | undefined;
|
||||
unit: string | null | undefined;
|
||||
markerUnit: string | null | undefined;
|
||||
markerAnalyteKey: string | null | undefined;
|
||||
}): {
|
||||
value: number;
|
||||
unit: string;
|
||||
statedValue: number;
|
||||
statedUnit: string;
|
||||
} | null {
|
||||
if (!readingUnitDiffers(row)) return null;
|
||||
if (row.value === null || row.value === undefined) return null;
|
||||
if (!Number.isFinite(row.value)) return null;
|
||||
const statedUnit = (row.unit ?? "").trim();
|
||||
const converted = convertLabValue({
|
||||
analyteKey: row.markerAnalyteKey,
|
||||
value: row.value,
|
||||
from: statedUnit,
|
||||
to: row.markerUnit as string,
|
||||
});
|
||||
if (!converted.ok) return null;
|
||||
return {
|
||||
value: converted.value,
|
||||
unit: row.markerUnit as string,
|
||||
statedValue: row.value,
|
||||
statedUnit,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A numeric reading in another unit than its marker's that cannot be
|
||||
* converted: the one case that still cannot be saved. A row with no number yet
|
||||
* has nothing to convert, so it is blocked here as well as on `value`.
|
||||
*/
|
||||
export function readingUnitBlocked(row: {
|
||||
value: number | null | undefined;
|
||||
valueText: string | null | undefined;
|
||||
unit: string | null | undefined;
|
||||
markerUnit: string | null | undefined;
|
||||
markerAnalyteKey: string | null | undefined;
|
||||
}): boolean {
|
||||
if (!readingUnitDiffers(row)) return false;
|
||||
return readingConversion(row) === null;
|
||||
}
|
||||
|
||||
export function validateReviewRow(row: OcrReviewRow): OcrRowErrors {
|
||||
const errors: OcrRowErrors = {};
|
||||
if (!row.analyte.trim()) errors.analyte = true;
|
||||
@@ -56,7 +111,7 @@ export function validateReviewRow(row: OcrReviewRow): OcrRowErrors {
|
||||
} else {
|
||||
if (row.value === null || !Number.isFinite(row.value)) errors.value = true;
|
||||
if (!(row.unit ?? "").trim()) errors.unit = true;
|
||||
if (readingUnitDiffers(row)) errors.unitMismatch = true;
|
||||
if (readingUnitBlocked(row)) errors.unitMismatch = true;
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
@@ -80,7 +135,7 @@ export function toCommitRow(row: OcrReviewRow): OcrCommitRowInput | null {
|
||||
if (row.value === null || !Number.isFinite(row.value)) return null;
|
||||
const unit = (row.unit ?? "").trim();
|
||||
if (!unit) return null;
|
||||
if (readingUnitDiffers(row)) return null;
|
||||
if (readingUnitBlocked(row)) return null;
|
||||
return {
|
||||
analyte,
|
||||
value: row.value,
|
||||
|
||||
@@ -32,7 +32,12 @@ import { Label } from "@/components/ui/label";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
import type { OcrReviewRow } from "./ocr-review-types";
|
||||
import { readingUnitDiffers, type OcrRowErrors } from "./ocr-review-validation";
|
||||
import {
|
||||
readingConversion,
|
||||
readingUnitBlocked,
|
||||
type OcrRowErrors,
|
||||
} from "./ocr-review-validation";
|
||||
import { useLabNumber } from "./use-lab-format";
|
||||
|
||||
/** Below this per-field confidence the field is flagged for a second look. */
|
||||
const CONFIDENCE_THRESHOLD = 0.6;
|
||||
@@ -76,10 +81,13 @@ export function OcrRowEditor({
|
||||
message={t(messageKey, params)}
|
||||
/>
|
||||
) : null;
|
||||
// Stated before Save, not only after a failed attempt: a reading in another
|
||||
// unit than its marker's cannot be written, and the person should see why
|
||||
// the row is unticked (or will not save) while they are still reading it.
|
||||
const unitDiffers = readingUnitDiffers(row);
|
||||
// Stated before Save, not only after a failed attempt. A reading in another
|
||||
// unit than its marker's is either converted (the badge says what Save will
|
||||
// store) or cannot be written (the person should see why the row is unticked,
|
||||
// or will not save, while they are still reading it).
|
||||
const unitBlocked = readingUnitBlocked(row);
|
||||
const conversion = readingConversion(row);
|
||||
const labNumber = useLabNumber();
|
||||
|
||||
const isQualitative = row.valueText !== null && row.valueText !== undefined;
|
||||
const lowValueConfidence =
|
||||
@@ -169,7 +177,7 @@ export function OcrRowEditor({
|
||||
{t("labs.ocr.duplicateWarning")}
|
||||
</Badge>
|
||||
) : null}
|
||||
{unitDiffers ? (
|
||||
{unitBlocked ? (
|
||||
<Badge variant="secondary" className="max-w-full">
|
||||
<Ruler aria-hidden />
|
||||
<span className="truncate">
|
||||
@@ -180,6 +188,25 @@ export function OcrRowEditor({
|
||||
</span>
|
||||
</Badge>
|
||||
) : null}
|
||||
{conversion ? (
|
||||
<Badge
|
||||
variant="outline"
|
||||
// Wraps rather than truncates: the printed value is the end
|
||||
// of the sentence, exactly what a cut at 390 px would hide.
|
||||
className="h-auto max-w-full justify-start rounded-md text-left whitespace-normal"
|
||||
data-slot="ocr-row-converted-badge"
|
||||
>
|
||||
<Ruler aria-hidden />
|
||||
<span className="min-w-0 break-words">
|
||||
{t("labs.ocr.unitConvertedBadge", {
|
||||
value: labNumber(conversion.value),
|
||||
markerUnit: conversion.unit,
|
||||
statedValue: labNumber(conversion.statedValue),
|
||||
unit: conversion.statedUnit,
|
||||
})}
|
||||
</span>
|
||||
</Badge>
|
||||
) : null}
|
||||
{lowRowConfidence ? (
|
||||
<Badge variant="secondary" className="text-muted-foreground">
|
||||
<AlertCircle aria-hidden />
|
||||
|
||||
@@ -18,6 +18,11 @@ import { useLabNumber } from "./use-lab-format";
|
||||
* the same number, so a surface that shows one silently is showing a partial
|
||||
* answer.
|
||||
*
|
||||
* A reading converted into its marker's unit carries bounds that were converted
|
||||
* with it, but the report's own text is still in the unit it printed. The note
|
||||
* then states the window in the marker's unit and adds the printed text as
|
||||
* evidence, labelled with its unit, so no single statement mixes two units.
|
||||
*
|
||||
* Neutral by construction: no tint, no icon, muted text. The in/out verdict is
|
||||
* the badge's job — this element states where the window came from, nothing
|
||||
* about whether the value is good news.
|
||||
@@ -34,6 +39,8 @@ export function SourceRangeNote({
|
||||
| "catalogReferenceLow"
|
||||
| "catalogReferenceHigh"
|
||||
| "sourceReferenceText"
|
||||
| "printedReferenceText"
|
||||
| "sourceUnit"
|
||||
| "referenceOrigin"
|
||||
| "referenceDivergesFromCatalog"
|
||||
>;
|
||||
@@ -43,7 +50,9 @@ export function SourceRangeNote({
|
||||
const labNumber = useLabNumber();
|
||||
if (reading.referenceOrigin !== "source") return null;
|
||||
|
||||
const printed =
|
||||
// `sourceReferenceText` is null for a converted reading, so the window is
|
||||
// then stated from the converted bounds, in the marker's unit.
|
||||
const inForce =
|
||||
reading.sourceReferenceText ??
|
||||
`${formatReferenceRange(
|
||||
reading.referenceLow,
|
||||
@@ -59,7 +68,13 @@ export function SourceRangeNote({
|
||||
|
||||
return (
|
||||
<span className={className}>
|
||||
{t("labs.sourceRange.fromReport", { range: printed })}
|
||||
{t("labs.sourceRange.fromReport", { range: inForce })}
|
||||
{reading.printedReferenceText && reading.sourceUnit
|
||||
? ` · ${t("labs.sourceRange.printed", {
|
||||
range: reading.printedReferenceText,
|
||||
unit: reading.sourceUnit,
|
||||
})}`
|
||||
: null}
|
||||
{reading.referenceDivergesFromCatalog && catalog
|
||||
? ` · ${t("labs.sourceRange.catalogDiffers", {
|
||||
range: `${catalog} ${reading.unit}`.trim(),
|
||||
|
||||
@@ -15,6 +15,14 @@ export interface LabResultDto {
|
||||
/** v1.18.9 — qualitative result text ("negativ" / …); null for a numeric row. */
|
||||
valueText: string | null;
|
||||
unit: string;
|
||||
/**
|
||||
* True when the stored number was converted into the marker's unit from
|
||||
* another one; `sourceValue` and `sourceUnit` are then what the report
|
||||
* printed. A reading stored as stated has `converted: false` and two nulls.
|
||||
*/
|
||||
converted: boolean;
|
||||
sourceValue: number | null;
|
||||
sourceUnit: string | null;
|
||||
/**
|
||||
* The window this reading is judged against and the one `rangeStatus` was
|
||||
* computed from: the range its own report printed when it carries one, the
|
||||
@@ -30,6 +38,11 @@ export interface LabResultDto {
|
||||
sourceReferenceLow: number | null;
|
||||
sourceReferenceHigh: number | null;
|
||||
sourceReferenceText: string | null;
|
||||
/**
|
||||
* A converted reading's printed range text, verbatim and in `sourceUnit`;
|
||||
* `sourceReferenceText` is then null. Null for every other reading.
|
||||
*/
|
||||
printedReferenceText: string | null;
|
||||
/** Which window is in force. */
|
||||
referenceOrigin: ReferenceRangeOrigin;
|
||||
/** True when the report's window and the catalog band state different limits. */
|
||||
@@ -60,6 +73,11 @@ export interface BiomarkerDto {
|
||||
lowerBound: number | null;
|
||||
upperBound: number | null;
|
||||
panel: string | null;
|
||||
/**
|
||||
* Which analyte the marker is, for converting a reading in another unit
|
||||
* (the catalog slug, e.g. `ldl`, or a registry-only key such as `urea`), or null when it is none that is known.
|
||||
*/
|
||||
analyteKey: string | null;
|
||||
hasContext: boolean;
|
||||
context: string | null;
|
||||
/** v1.22 — a hidden marker drops from the active list + lab-entry pickers. */
|
||||
|
||||
Reference in new issue
Block a user