"use client"; /** * Quick-entry sheets for the dashboard: measurement, mood, and * medication-intake forms behind the shared ResponsiveSheet primitive * (bottom-sheet on `( '[data-slot="responsive-sheet-body"]', ); if (!body) return false; const fields = body.querySelectorAll( "input, textarea", ); for (const field of fields) { if (field.disabled || field.type === "hidden") continue; // Skip pickers the form prefills with "now" — an untouched default // is not user input (v1.11.3 QA H1). if (PREFILLED_PICKER_TYPES.has(field.type)) continue; if (field.type === "checkbox" || field.type === "radio") { const box = field as HTMLInputElement; if (box.checked !== box.defaultChecked) return true; continue; } if (field.value.trim() !== "") return true; } // v1.11.5 — the input/textarea walk misses two non-text controls a user can // change before dismissing: the mood `role="radio"` selector and a Radix // Select (measurement type / medication). Losing either silently on a // backdrop-dismiss is the same data-loss class the H1 confirm-on-dirty guard // was meant to close. // // Mood radios start with nothing selected (`mood = ""`), so ANY // `aria-checked="true"` is a deliberate user choice — flag it. if (body.querySelector('[role="radio"][aria-checked="true"]') !== null) { return true; } // Radix Select trigger renders `role="combobox"`. A trigger with // `data-state="open"` is mid-interaction (the dropdown is up) — the user is // actively choosing, so a dismiss should confirm rather than drop the pick. // We deliberately do NOT treat a merely non-placeholder value as dirty: the // type / medication selects mount with a non-placeholder DEFAULT, and // flagging that would over-trigger the confirm on a pristine sheet. if (body.querySelector('[role="combobox"][data-state="open"]') !== null) { return true; } return false; } export function QuickEntrySheets({ open, onClose, }: { open: QuickEntryDialog; onClose: () => void; }) { const { t } = useTranslations(); // v1.4.27 R4 RC2 — DOM handles for the form action-row portal target // on each quick-entry sheet. The Sheet branch sticky-pins this slot. const [measurementFooterEl, setMeasurementFooterEl] = useState(null); const [moodFooterEl, setMoodFooterEl] = useState(null); // v1.4.37 W7b — medication-intake quick-add lives on the same Sheet // primitive as the other two quick-entries; the form's action row is // portalled into this slot so Save / Cancel stay reachable above the // mobile soft keyboard. const [medicationIntakeFooterEl, setMedicationIntakeFooterEl] = useState(null); // v1.11.3 F3 — when an open quick-entry sheet is dismissed with // unsaved input, hold the close in this flag and surface a confirm // instead of nulling the dialog outright. Cleared once the user // confirms (close) or keeps editing (dismiss the confirm). const [confirmDiscardOpen, setConfirmDiscardOpen] = useState(false); // Intercept a sheet dismiss: close immediately when the form is // clean, otherwise keep the sheet open and ask before discarding. function handleQuickEntryOpenChange(nextOpen: boolean) { if (nextOpen) return; if (sheetBodyHasUnsavedInput()) { setConfirmDiscardOpen(true); return; } onClose(); } return ( <> } > } > {/* v1.4.37 W7b — third Sheet: medication intake. Same ResponsiveSheet contract as the two above; the form's footer (Cancel + Save) portals into the sticky-pinned slot so it stays reachable above the soft keyboard on mobile. */} } > {/* v1.11.3 F3 — confirm before discarding a partly-filled quick-entry form when the sheet is dismissed by an overlay tap, Escape, or a mobile swipe-down. Keeps the sheet open until the user decides. */} {t("dashboard.quickEntryDiscard.title")} {t("dashboard.quickEntryDiscard.description")} {t("dashboard.quickEntryDiscard.cancel")} { setConfirmDiscardOpen(false); onClose(); }} > {t("dashboard.quickEntryDiscard.confirm")} ); }