"use client"; /** * Settings → Export → the health-record export panel. * * One surface driving `POST /api/export/health-record`: a format radio, the * reporting window, the practice line, the scope picker, and the generate * action that streams the artefact as a download. * * Three states, by design: * * 1. First run (no saved selection): NOTHING is selected. The picker opens * expanded so the standard-report button and the empty fenced tier are * both in view, and Generate is disabled until something is ticked. One * click on that button still produces a complete doctor's report, so the * fast path costs one interaction, not a scope nobody chose. * 2. Repeat run: the picker is collapsed behind its disclosure, the saved * selection is loaded, and the scope line under the button says what will * be in the document. Two interactions after navigation, and the reading * is the consent check. * 3. Editing: a group row is one control for up to sixteen leaves; the leaf * grid opens only when the scope actually changes, which is rare. * * The saved selection seeds per-user during render with a stored-id guard — * `useAuth` resolves asynchronously, so a `useState` initialiser would read * `undefined` and stick — and the per-user gate means a late re-resolve never * clobbers edits the user is in the middle of. */ import Link from "next/link"; import { useId, useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { ChevronDown, Download, FileText, FolderOpen, Loader2, } from "lucide-react"; import { SettingsCard } from "@/components/settings/settings-card"; import { SettingsCardHeader } from "@/components/settings/_card-header"; import { Button } from "@/components/ui/button"; import { DateField } from "@/components/ui/date-field"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { NativeSelect } from "@/components/ui/native-select"; import { ReportVisitOffer } from "./report-visit-offer"; import { Switch } from "@/components/ui/switch"; import { useRovingRadioGroup } from "@/hooks/use-roving-radio-group"; import { apiFetchRaw } from "@/lib/api/api-fetch"; import { useAuth } from "@/hooks/use-auth"; import { useTranslations } from "@/lib/i18n/context"; import { queryKeys } from "@/lib/query-keys"; import { cn } from "@/lib/utils"; import type { ReportLeafId } from "@/lib/report-selection/catalogue"; import { parseSavedProfile, SAVED_PROFILE_FALLBACK, } from "@/lib/report-selection/profile-shape"; import { orderLeaves } from "@/lib/report-selection/selection"; import { ReportScopePicker } from "./report-scope-picker"; import { ScopeSummary } from "./scope-summary"; type ExportFormat = "pdf" | "fhir" | "package"; const EXPORT_FORMATS: readonly ExportFormat[] = ["pdf", "fhir", "package"]; const PRESET_RANGES = [30, 90, 180, 365] as const; export function HealthRecordExportPanel() { const { t, locale } = useTranslations(); const { user } = useAuth(); const queryClient = useQueryClient(); const [format, setFormat] = useState( SAVED_PROFILE_FALLBACK.format, ); const [days, setDays] = useState(SAVED_PROFILE_FALLBACK.rangeDays); const [customRange, setCustomRange] = useState(false); const [startDate, setStartDate] = useState(""); const [endDate, setEndDate] = useState(""); const [practiceName, setPracticeName] = useState(""); const [includeCharts, setIncludeCharts] = useState( SAVED_PROFILE_FALLBACK.includeCharts, ); // Empty until a human ticks something. There is no default selection to get // wrong, because there is no default selection. const [selected, setSelected] = useState>( () => new Set(), ); const [seededUserId, setSeededUserId] = useState(null); // A first run opens the picker: with nothing selected, the standard-report // button and the empty groups are the two things the person needs to see. const [pickerOpen, setPickerOpen] = useState(true); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const pickerPanelId = useId(); const scopeSummaryId = useId(); if (user && user.id !== seededUserId) { setSeededUserId(user.id); setPracticeName(user.lastReportPracticeName ?? ""); const saved = parseSavedProfile(user.reportSelection); if (saved) { setSelected(new Set(saved.leaves as ReportLeafId[])); setFormat(saved.format); setDays(saved.rangeDays); setIncludeCharts(saved.includeCharts); setPickerOpen(false); } } const isPdfLike = format === "pdf" || format === "package"; const isFhirLike = format === "fhir" || format === "package"; const { getRadioProps: getFormatRadioProps } = useRovingRadioGroup({ count: EXPORT_FORMATS.length, selectedIndex: EXPORT_FORMATS.indexOf(format), onSelect: (index) => setFormat(EXPORT_FORMATS[index]!), }); async function handleGenerate() { setBusy(true); setError(null); try { const range = customRange && startDate && endDate ? { startDate: new Date(`${startDate}T00:00:00Z`).toISOString(), endDate: new Date(`${endDate}T23:59:59Z`).toISOString(), } : { days }; const res = await apiFetchRaw("/api/export/health-record", { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify({ format, // Carry the active in-app locale so the artefact matches the UI // language instead of falling back to Accept-Language on the server. locale, range, practiceName: practiceName.trim() || undefined, includeCharts, selection: { v: 2, leaves: orderLeaves(selected) }, }), }); if (!res.ok) { setError( res.status === 429 ? t("settings.healthRecord.errorRateLimit") : res.status === 403 ? t("settings.healthRecord.errorModuleDisabled") : t("settings.healthRecord.errorGeneric"), ); return; } const blob = await res.blob(); const ext = format === "pdf" ? "pdf" : format === "fhir" ? "json" : "zip"; const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `healthlog-health-record-${new Date() .toISOString() .slice(0, 10)}.${ext}`; document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url); // The route just persisted the practice name and the selection, so the // cached `/me` payload is stale. Refresh it, or a remount would seed from // the previous values. void queryClient.invalidateQueries({ queryKey: queryKeys.authMe() }); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setBusy(false); } } return (
{/* Format + range share a row on desktop; on mobile they stack. */}
{t("settings.healthRecord.format")}
{EXPORT_FORMATS.map((f, index) => ( ))}
{ if (e.target.value === "custom") { setCustomRange(true); return; } setCustomRange(false); setDays(Number(e.target.value)); }} > {PRESET_RANGES.map((preset) => ( ))} {/* Entry point into the document vault for the report period (navigation only). Only rendered when the module is enabled. */} {user?.modules?.inboundDocuments ? ( {t("settings.healthRecord.documentsLink")} ) : null}
{/* Never auto-applies: pressing it is what opens the custom window and sets its end date. */} { setCustomRange(true); setEndDate(endDay); }} /> {customRange ? (
) : null} {/* Practice name — PDF only */} {isPdfLike && (
setPracticeName(e.target.value)} maxLength={120} placeholder={t("settings.healthRecord.practiceNamePlaceholder")} />
)}
{t("settings.healthRecord.includedData")} {pickerOpen && (
{isPdfLike ? ( ) : null}
)}
{/* The FHIR note and the generate action share the footer row. */}
{isFhirLike ? (

{t("settings.healthRecord.fhirNote")}

) : null}
{/* An empty scope cannot be generated: there is no document to make. The scope line beside the button carries the reason and the way out of it, and describes the button so the disabled state is not silent for a screen reader. */}
{error && (

{error}

)}
); }