|
| 1 | +import { useMemo, useState } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | + |
| 4 | +import { cn } from "@/lib/utils"; |
| 5 | + |
| 6 | +import Autocomplete from "@/components/ui/autocomplete"; |
| 7 | + |
| 8 | +import { |
| 9 | + fhirDosageToFrequencyValue, |
| 10 | + generateManSuggestions, |
| 11 | + MAN_FREQUENCY_PRESETS, |
| 12 | + manToFhirTiming, |
| 13 | + MEDICATION_REQUEST_TIMING_OPTIONS, |
| 14 | + MedicationRequestDosageInstruction, |
| 15 | +} from "@/types/emr/medicationRequest/medicationRequest"; |
| 16 | + |
| 17 | +interface DosageFrequencyInputProps { |
| 18 | + dosageInstruction: MedicationRequestDosageInstruction; |
| 19 | + onDosageInstructionChange: ( |
| 20 | + updates: Partial<MedicationRequestDosageInstruction>, |
| 21 | + ) => void; |
| 22 | + disabled?: boolean; |
| 23 | + hasError?: boolean; |
| 24 | + className?: string; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Smart single-field frequency autocomplete. |
| 29 | + * |
| 30 | + * Doctors type naturally -- "1-0-1", "1/2-0-1", "SOS", "Q6H" -- and the |
| 31 | + * system dynamically generates suggestions. Under the hood it maps to FHIR |
| 32 | + * Timing structures where possible, and falls back to dosageInstruction.text |
| 33 | + * for freeform patterns. |
| 34 | + */ |
| 35 | +export function DosageFrequencyInput({ |
| 36 | + dosageInstruction, |
| 37 | + onDosageInstructionChange, |
| 38 | + disabled = false, |
| 39 | + hasError = false, |
| 40 | + className, |
| 41 | +}: DosageFrequencyInputProps) { |
| 42 | + const { t } = useTranslation(); |
| 43 | + const [searchQuery, setSearchQuery] = useState(""); |
| 44 | + |
| 45 | + // Derive current value from existing dosage instruction (reverse mapping) |
| 46 | + // Only the fields read by fhirDosageToFrequencyValue are listed as deps |
| 47 | + // to avoid recomputing when unrelated dosageInstruction fields change. |
| 48 | + const currentValue = useMemo( |
| 49 | + () => fhirDosageToFrequencyValue(dosageInstruction), |
| 50 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 51 | + [ |
| 52 | + dosageInstruction?.text, |
| 53 | + dosageInstruction?.timing, |
| 54 | + dosageInstruction?.as_needed_boolean, |
| 55 | + ], |
| 56 | + ); |
| 57 | + |
| 58 | + // Dynamically generate options based on what the user is typing |
| 59 | + const options = useMemo(() => { |
| 60 | + const query = searchQuery.trim(); |
| 61 | + const results: { value: string; label: string }[] = []; |
| 62 | + const seen = new Set<string>(); |
| 63 | + |
| 64 | + const add = (value: string, label: string) => { |
| 65 | + if (seen.has(value)) return; |
| 66 | + seen.add(value); |
| 67 | + results.push({ value, label }); |
| 68 | + }; |
| 69 | + |
| 70 | + // 1. Generate M-A-N pattern suggestions |
| 71 | + const manSuggestions = generateManSuggestions(query); |
| 72 | + for (const s of manSuggestions) { |
| 73 | + add(s.value, s.label); |
| 74 | + } |
| 75 | + |
| 76 | + // 2. Add SOS option |
| 77 | + if (!query || "sos".startsWith(query.toLowerCase())) { |
| 78 | + add("SOS", "SOS (As needed)"); |
| 79 | + } |
| 80 | + |
| 81 | + // 3. Add STAT option |
| 82 | + if (!query || "stat".startsWith(query.toLowerCase())) { |
| 83 | + add("STAT", "STAT (Immediately)"); |
| 84 | + } |
| 85 | + |
| 86 | + // 4. Filter FHIR timing options by code or display text |
| 87 | + if (query) { |
| 88 | + const lowerQuery = query.toLowerCase(); |
| 89 | + for (const [key, opt] of Object.entries( |
| 90 | + MEDICATION_REQUEST_TIMING_OPTIONS, |
| 91 | + )) { |
| 92 | + // Skip options that are already represented as M-A-N presets |
| 93 | + const isManPreset = MAN_FREQUENCY_PRESETS.some( |
| 94 | + (p) => p.timingKey === key, |
| 95 | + ); |
| 96 | + if (isManPreset) continue; |
| 97 | + |
| 98 | + if ( |
| 99 | + key.toLowerCase().startsWith(lowerQuery) || |
| 100 | + opt.display.toLowerCase().includes(lowerQuery) || |
| 101 | + opt.timing.code?.display.toLowerCase().includes(lowerQuery) |
| 102 | + ) { |
| 103 | + add(key, opt.display); |
| 104 | + } |
| 105 | + } |
| 106 | + } else { |
| 107 | + // When empty, show a few common FHIR codes after M-A-N presets |
| 108 | + for (const key of ["QD", "QOD", "Q6H", "Q8H", "Q12H", "BED", "WK"]) { |
| 109 | + const opt = MEDICATION_REQUEST_TIMING_OPTIONS[key]; |
| 110 | + if (opt) add(key, opt.display); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // 5. Always include the currently selected value so the button displays correctly |
| 115 | + if (currentValue && !seen.has(currentValue)) { |
| 116 | + const preset = MAN_FREQUENCY_PRESETS.find((p) => p.man === currentValue); |
| 117 | + add( |
| 118 | + currentValue, |
| 119 | + preset ? `${currentValue} (${preset.label})` : currentValue, |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + // 6. If query looks like a valid M-A-N but isn't in our generated list, |
| 124 | + // add it as a "custom" entry so freeform is always allowed. |
| 125 | + const manFullRe = /^[\d]+(?:\/[\d]+)?(-[\d]+(?:\/[\d]+)?){1,3}$/; |
| 126 | + if (query && manFullRe.test(query) && !seen.has(query)) { |
| 127 | + add(query, query); |
| 128 | + } |
| 129 | + |
| 130 | + return results; |
| 131 | + }, [searchQuery, currentValue]); |
| 132 | + |
| 133 | + const handleChange = (value: string) => { |
| 134 | + if (!value) { |
| 135 | + // Cleared |
| 136 | + onDosageInstructionChange({ |
| 137 | + timing: undefined, |
| 138 | + as_needed_boolean: false, |
| 139 | + as_needed_for: undefined, |
| 140 | + text: undefined, |
| 141 | + }); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + // Try to map to FHIR timing |
| 146 | + const fhirMapping = manToFhirTiming(value); |
| 147 | + |
| 148 | + if (fhirMapping) { |
| 149 | + if (fhirMapping.asNeeded) { |
| 150 | + // SOS / PRN |
| 151 | + onDosageInstructionChange({ |
| 152 | + timing: undefined, |
| 153 | + as_needed_boolean: true, |
| 154 | + text: "SOS", |
| 155 | + }); |
| 156 | + } else { |
| 157 | + // Standard FHIR timing (from M-A-N preset or direct FHIR code) |
| 158 | + const preset = MAN_FREQUENCY_PRESETS.find((p) => p.man === value); |
| 159 | + onDosageInstructionChange({ |
| 160 | + timing: fhirMapping.timing, |
| 161 | + as_needed_boolean: false, |
| 162 | + as_needed_for: undefined, |
| 163 | + text: preset ? preset.man : undefined, |
| 164 | + }); |
| 165 | + } |
| 166 | + } else { |
| 167 | + // Non-standard M-A-N or freeform -- store as text |
| 168 | + onDosageInstructionChange({ |
| 169 | + text: value, |
| 170 | + as_needed_boolean: false, |
| 171 | + as_needed_for: undefined, |
| 172 | + timing: undefined, |
| 173 | + }); |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + return ( |
| 178 | + <Autocomplete |
| 179 | + options={options} |
| 180 | + value={currentValue} |
| 181 | + onChange={handleChange} |
| 182 | + onSearch={setSearchQuery} |
| 183 | + placeholder={t("frequency_placeholder")} |
| 184 | + inputPlaceholder={t("frequency_input_placeholder")} |
| 185 | + noOptionsMessage={t("no_frequency_found")} |
| 186 | + disabled={disabled} |
| 187 | + className={cn("h-9 text-sm", hasError && "border-red-500", className)} |
| 188 | + popoverContentClassName="w-80" |
| 189 | + showClearButton={false} |
| 190 | + /> |
| 191 | + ); |
| 192 | +} |
0 commit comments