mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
Replace the separate, capability-gated scan button with a single Add menu: "Scan a document" (OCR) or "Add a value manually". The scan entry appears only when the capability probe reports a usable mode, and routes to native vision or local in-browser OCR (tesseract.js, lazy-loaded, deu+eng, images only) per the resolved mode. Add the local-OCR opt-in toggle to the Labs settings page with honest "less accurate — review every value" copy. Surface range confidence prominently on the OCR review screen for new numeric biomarkers, where the extracted range becomes the catalog range: flag low range-confidence or a missing range so the user verifies it.
130 lines
4.2 KiB
TypeScript
130 lines
4.2 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* v1.18.9 — client hooks for the Lab-OCR ingestion flow.
|
|
*
|
|
* - `useOcrCapability()` — the cheap capability probe that decides whether the
|
|
* "Scan a report" affordance shows.
|
|
* - `useOcrExtract()` — uploads the photo / PDF and returns the proposed rows.
|
|
* A vision call is slow, so it opts out of the default 15 s fetch timeout in
|
|
* favour of a generous 90 s window.
|
|
* - `useOcrCommit()` — writes the user-confirmed rows and invalidates the
|
|
* labs + biomarker query keys.
|
|
*/
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { apiFetch, apiGet, apiPatch, apiPost } from "@/lib/api/api-fetch";
|
|
import { ocrImageToText } from "@/lib/labs/local-ocr";
|
|
import { queryKeys } from "@/lib/query-keys";
|
|
import type {
|
|
OcrCapabilityDto,
|
|
OcrCommitResponseDto,
|
|
OcrExtractResponseDto,
|
|
} from "@/lib/validations/labs-ocr";
|
|
|
|
import type { LabResultDto } from "./types";
|
|
|
|
/** Per-row payload the commit endpoint accepts (the human-confirmed shape). */
|
|
export interface OcrCommitRowInput {
|
|
analyte: string;
|
|
panel?: string;
|
|
value?: number;
|
|
valueText?: string;
|
|
unit?: string;
|
|
referenceLow?: number;
|
|
referenceHigh?: number;
|
|
takenAt: string;
|
|
}
|
|
|
|
export interface OcrCommitResult extends OcrCommitResponseDto {
|
|
inserted: LabResultDto[];
|
|
}
|
|
|
|
/** Capability probe — refetched when the scan dialog opens. */
|
|
export function useOcrCapability(enabled: boolean) {
|
|
return useQuery<OcrCapabilityDto>({
|
|
queryKey: queryKeys.ocrCapability(),
|
|
queryFn: () => apiFetch<OcrCapabilityDto>("/api/labs/ocr/capability"),
|
|
enabled,
|
|
staleTime: 60_000,
|
|
});
|
|
}
|
|
|
|
/** Upload + extract (VISION mode). Resolves with the proposed review rows. */
|
|
export function useOcrExtract() {
|
|
return useMutation<OcrExtractResponseDto, Error, File>({
|
|
mutationFn: (file: File) => {
|
|
const form = new FormData();
|
|
form.append("file", file);
|
|
return apiFetch<OcrExtractResponseDto>("/api/labs/ocr/extract", {
|
|
method: "POST",
|
|
body: form,
|
|
// Vision extraction is slow; a generous ceiling beats the 15 s default.
|
|
signal: AbortSignal.timeout(90_000),
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* TEXT mode (v1.18.10) — OCR the image IN THE BROWSER (tesseract.js), then POST
|
|
* only the extracted text. The raw image never leaves the device. Resolves with
|
|
* the same proposed-rows DTO the vision path returns, so the review/commit flow
|
|
* is shared verbatim.
|
|
*/
|
|
export function useOcrTextExtract() {
|
|
return useMutation<OcrExtractResponseDto, Error, File>({
|
|
mutationFn: async (file: File) => {
|
|
const text = await ocrImageToText(file);
|
|
return apiPost<OcrExtractResponseDto>("/api/labs/ocr/extract", {
|
|
mode: "text",
|
|
text,
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/** The local-OCR opt-in preference (read + toggle). */
|
|
export interface LabsLocalOcrPref {
|
|
labsLocalOcrEnabled: boolean;
|
|
}
|
|
|
|
/** Read the current local-OCR opt-in flag. */
|
|
export function useLabsLocalOcr(enabled = true) {
|
|
return useQuery<LabsLocalOcrPref>({
|
|
queryKey: queryKeys.labsLocalOcr(),
|
|
queryFn: () => apiGet<LabsLocalOcrPref>("/api/auth/me/labs-local-ocr"),
|
|
enabled,
|
|
staleTime: 60_000,
|
|
});
|
|
}
|
|
|
|
/** Toggle the local-OCR opt-in; invalidates the flag + the capability probe. */
|
|
export function useUpdateLabsLocalOcr() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<LabsLocalOcrPref, Error, boolean>({
|
|
mutationFn: (labsLocalOcrEnabled: boolean) =>
|
|
apiPatch<LabsLocalOcrPref>("/api/auth/me/labs-local-ocr", {
|
|
labsLocalOcrEnabled,
|
|
}),
|
|
onSuccess: (data) => {
|
|
queryClient.setQueryData(queryKeys.labsLocalOcr(), data);
|
|
// The toggle changes whether text-mode scanning is available.
|
|
queryClient.invalidateQueries({ queryKey: queryKeys.ocrCapability() });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Commit the confirmed rows and invalidate the dependent reads. */
|
|
export function useOcrCommit() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<OcrCommitResult, Error, OcrCommitRowInput[]>({
|
|
mutationFn: (rows: OcrCommitRowInput[]) =>
|
|
apiPost<OcrCommitResult>("/api/labs/ocr/commit", { rows }),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: queryKeys.labResults() });
|
|
queryClient.invalidateQueries({ queryKey: queryKeys.biomarkers() });
|
|
},
|
|
});
|
|
}
|