Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 3x 663x 203x 203x 375x 126x 126x 126x 78x 78x 81x 2x 78x 77x 78x 80x 107x 107x 78x 78x 1x 1x 78x 77x 78x 9x 9x 9x 78x 78x 120x 120x 720x 720x | import type { Appointment } from '@/features/appointments/api/appointments.schemas'
import type { Baby } from '@/features/babies/api/babies.schemas'
import type { Medication } from '@/features/medications/api/medications.schemas'
import type { Milestone } from '@/features/milestones/api/milestones.schemas'
import type { Specialist } from '@/features/specialists/api/specialists.schemas'
import { formatDateDisplay, formatDateTimeDisplay } from '@/lib/date'
import type { AdhocVaccineRecord, VaccineItem } from '@/features/vaccines/api/vaccines.schemas'
export type SearchDomain =
'babies' | 'vaccines' | 'appointments' | 'medications' | 'milestones' | 'specialists'
export interface SearchResult {
/** Unique across domains — two features can hold the same row id. */
key: string
domain: SearchDomain
title: string
/** Who and when, when there is a who and a when. */
subtitle: string | null
to: string
babyId?: string
detail?: string
}
/**
* Two characters. One letter matches most of the database and answers nothing,
* and the list flashing on every keystroke of a word being typed is worse than
* no list.
*/
export const MIN_QUERY_LENGTH = 2
/**
* Casefolded and stripped of accents.
*
* Not a nicety in pt-BR: "varicela" has to find "Varicela", "sarampo" has to
* find "Sarampão", and a parent typing on a phone keyboard in a hurry does not
* hold the accent key down. Both sides go through here, so the comparison is
* symmetric — normalising only the query is the version that silently fails on
* the data's own accents.
*/
export function normalise(text: string): string {
return text
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.toLowerCase()
.trim()
.replace(/\s+/g, ' ')
}
/** True when any of the fields contains the query. Nulls are fields nobody filled in. */
export function matches(fields: readonly (string | null | undefined)[], query: string): boolean {
const needle = normalise(query)
if (needle.length < MIN_QUERY_LENGTH) return false
return fields.some((field) => !!field && normalise(field).includes(needle))
}
interface SearchInput {
query: string
locale?: string
doseLabel?: string
adhoc?: readonly AdhocVaccineRecord[]
babies: readonly Baby[]
vaccines: readonly { baby: Baby; items: readonly VaccineItem[] }[]
appointments: readonly { baby: Baby; items: readonly Appointment[] }[]
medications: readonly { baby: Baby; items: readonly Medication[] }[]
milestones: readonly { baby: Baby; items: readonly Milestone[] }[]
specialists: readonly Specialist[]
}
/**
* Everything this account can see that matches, grouped by where it lives.
*
* **It searches what is already loaded and nothing else** — there is no search
* endpoint, and adding one would be a second implementation of every domain's
* visibility rules, which is the class of bug that leaks another family's data.
* The dialog mounts the aggregate hooks the pages already use, so a result can
* only be something the reader could have reached by walking the app.
*
* The order of the domains is the order of the menu, not relevance ranking. A
* parent searching "amoxicilina" knows whether they are after a prescription or
* an appointment note; what they cannot do is guess which of six lists a
* scoring function decided to put first today.
*/
export function search(input: SearchInput): SearchResult[] {
const { query } = input
const date = (value: string) => formatDateDisplay(value, input.locale ?? 'pt-BR')
if (normalise(query).length < MIN_QUERY_LENGTH) return []
const results: SearchResult[] = []
for (const baby of input.babies) {
if (
matches([baby.name, baby.healthPlanName, baby.healthPlanNumber, ...baby.allergies], query)
) {
results.push({
key: `baby:${baby.id}`,
domain: 'babies',
title: baby.name,
subtitle: null,
to: '/dashboard',
babyId: baby.id,
})
}
}
for (const { baby, items } of input.vaccines) {
for (const item of items) {
// `description` and `guidance` are the catalogue's own prose — long, and
// the reason "hepatite" finds a dose whose name says only "Penta".
if (
matches(
[
item.name,
item.description,
item.guidance,
item.location,
item.professional,
item.notes,
],
query,
)
) {
results.push({
key: `vaccine:${baby.id}:${item.vaccineId}:${item.doseNumber}`,
domain: 'vaccines',
title: item.name,
detail: [
`${input.doseLabel ?? 'Dose'} ${item.doseNumber}`,
item.applicationDate && date(item.applicationDate),
]
.filter(Boolean)
.join(' · '),
subtitle: baby.name,
to: '/vaccines',
babyId: baby.id,
})
}
}
}
for (const { baby, items } of input.appointments) {
for (const item of items) {
Eif (
matches([item.doctorName, item.specialty, item.location, item.reason, item.notes], query)
) {
results.push({
key: `appointment:${item.id}`,
domain: 'appointments',
title: item.doctorName,
detail: formatDateTimeDisplay(item.scheduledAt, input.locale ?? 'pt-BR'),
subtitle: [baby.name, item.specialty].filter(Boolean).join(' · '),
to: '/appointments',
babyId: baby.id,
})
}
}
}
for (const { baby, items } of input.medications) {
for (const item of items) {
Eif (
matches(
[item.name, item.dosage, item.frequency, item.reason, item.prescriberName, item.notes],
query,
)
) {
results.push({
key: `medication:${item.id}`,
domain: 'medications',
title: item.name,
detail: [date(item.startedOn), item.endedOn && date(item.endedOn)]
.filter(Boolean)
.join(' → '),
subtitle: [baby.name, item.dosage].filter(Boolean).join(' · '),
to: '/medications',
babyId: baby.id,
})
}
}
}
for (const { baby, items } of input.milestones) {
for (const item of items) {
if (matches([item.title, item.description], query)) {
results.push({
key: `milestone:${item.id}`,
domain: 'milestones',
title: item.title,
detail: date(item.achievedAt),
subtitle: baby.name,
to: '/milestones',
babyId: baby.id,
})
}
}
}
for (const item of input.adhoc ?? []) {
Eif (
matches(
[
item.customName,
item.customDose,
item.notes,
item.professional,
item.location,
item.batchNumber,
],
query,
)
)
results.push({
key: `adhoc:${item.id}`,
domain: 'vaccines',
title: item.customName,
subtitle: input.babies.find((baby) => baby.id === item.babyId)?.name ?? null,
detail: [item.customDose, item.applicationDate && date(item.applicationDate)]
.filter(Boolean)
.join(' · '),
to: '/vaccines',
babyId: item.babyId,
})
}
for (const specialist of input.specialists) {
if (matches([specialist.name, specialist.specialty, specialist.phone], query)) {
results.push({
key: `specialist:${specialist.id}`,
domain: 'specialists',
title: specialist.name,
subtitle: specialist.specialty,
to: '/profissionais',
})
}
}
return results
}
/** The results of one domain, in the menu's order, for a list with headings. */
export function groupByDomain(
results: readonly SearchResult[],
): { domain: SearchDomain; results: SearchResult[] }[] {
const order: SearchDomain[] = [
'babies',
'vaccines',
'appointments',
'specialists',
'medications',
'milestones',
]
return order
.map((domain) => ({ domain, results: results.filter((result) => result.domain === domain) }))
.filter((group) => group.results.length > 0)
}
|