import { Link } from '@inertiajs/react' import type { DrugSearchResult } from '../../../types/drug_reference' import { PRODUCT_TYPES } from '../../../types/drug_reference' interface Props { result: DrugSearchResult /** * Inside an ingredient group the active ingredient is already the group header, * so lead with the brand (product identity) instead of repeating the ingredient. * Ungrouped (default) leads with the active ingredient — the thing users think in. */ brandFirst?: boolean } /** Title-case an UPPERCASE ingredient string (IBUPROFEN → Ibuprofen). */ function titleCase(s: string): string { return s .toLowerCase() .replace(/\b([a-z])/g, (m) => m.toUpperCase()) } /** * A single collapsed search result row. Leads with the active ingredient by * default (drug-first), or the brand when rendered inside an ingredient group. */ export default function DrugResultRow({ result, brandFirst = false }: Props) { const isRx = result.product_type === PRODUCT_TYPES.RX const isOtc = result.product_type === PRODUCT_TYPES.OTC const ingredient = result.generic_name ? titleCase(result.generic_name) : null const brand = result.brand_name ?? null // Headline vs sub-line depending on context. const headline = brandFirst ? (brand ?? ingredient ?? 'Unknown') : (ingredient ?? brand ?? 'Unknown') const subParts: string[] = [] if (brandFirst) { if (result.manufacturer) subParts.push(result.manufacturer) } else { // Ingredient-first: show the brand (if it differs from the ingredient) + maker. if (brand && brand.toLowerCase() !== (ingredient ?? '').toLowerCase()) subParts.push(brand) if (result.manufacturer) subParts.push(result.manufacturer) } if (result.route) subParts.push(titleCase(result.route)) return (
{headline} {isRx && ( Rx )} {isOtc && ( OTC )} {result.labelCount > 1 && ( {result.labelCount} labels )}
{subParts.length > 0 && (
{subParts.map((p, i) => ( {p} ))}
)}
) }