import { Loader2, MapPin, Navigation, Plus, Search } from 'lucide-react'; import { useEffect, useRef, useState, type ReactNode } from 'react'; import type { Category, Place } from '../../types'; import { safeHexColor } from '../../utils/safeColor'; import type { StopKind } from '../Roadtrip/stopKinds'; import { getCategoryIcon } from '../shared/categoryIcons'; import { PillSelect } from '../shared/dialogParts'; import { PILL } from '../shared/DialogShell'; import { NavigationMenu } from '../shared/NavigationMenu'; import { Tooltip } from '../shared/Tooltip'; import { getNavigationTargets, openNavigationTarget } from './placeNavigation'; type Translate = (key: string, params?: Record) => string; /** The round pill of the head band: a + or a map pin, an icon and nothing else. */ const ROUND_PILL_BUTTON = 'grid h-[26px] w-[26px] flex-none place-items-center rounded-full bg-surface-card text-content-muted shadow-sm ring-1 ring-edge-faint transition-colors hover:text-content'; interface NewCategoryRow { open: boolean; setOpen: (open: boolean) => void; name: string; setName: (name: string) => void; create: () => Promise; inputId: string; } /** * The head band's pill row. The category to pick, with a round + beside it that * turns the two into a field for a new one; for a stop on a drive, its kind in * place of both. Then where the picked place came from, and a spinner while a * pick is still being looked up. */ export function HeaderPills({ kind, categories, categoryId, onCategory, newCategory, pickedSource, busy, navPlace, refresh, t, }: { kind?: StopKind; categories: Category[] | null; categoryId: string; onCategory: (id: string) => void; newCategory: NewCategoryRow; pickedSource: string | null; busy: boolean; /** The place as the form holds it, once it has a position: opens it in a map app (#2178). */ navPlace: NavigablePlace | null; /** Newer details for a saved place, when a fresh lookup has them (PlaceRefreshPill). */ refresh?: ReactNode; t: Translate; }) { // When the new-category field closes, the field or the button that closed it // goes with it, and the focus would drop out of the dialog. It comes back to // the + that opened the field, as a picker's focus comes back to its pill. const plusRef = useRef(null); const wasOpen = useRef(newCategory.open); useEffect(() => { const closed = wasOpen.current && !newCategory.open; wasOpen.current = newCategory.open; if (!closed) return; const active = document.activeElement; if (!active || active === document.body || !active.isConnected) plusRef.current?.focus(); }, [newCategory.open]); const noCategory = { label: t('places.noCategory'), icon: , }; const options = [ { value: '', ...noCategory }, ...(categories || []).map((c) => { const Icon = getCategoryIcon(c.icon); // A string like form.category_id, so the picked option hands back the same // kind of value the form already keeps. return { value: String(c.id), label: c.name, icon: , }; }), ]; let lead: ReactNode; if (kind) { lead = ( {t(kind.labelKey)} ); } else if (newCategory.open) { lead = ; } else { lead = ( <> ); } return ( <> {lead} {pickedSource && ( )} {navPlace && } {refresh} {busy && ( )} ); } type NavigablePlace = Pick; /** * A look at the picked place in a real map before it is saved (#2178): its reviews, its * photos, whether it is the right branch. The same apps the inspector offers, one tap * straight away when only one applies. */ function OpenInMapsPill({ place, t }: { place: NavigablePlace; t: Translate }) { const [open, setOpen] = useState(false); const anchorRef = useRef(null); const targets = getNavigationTargets(place); if (targets.length === 0) return null; return ( {open && setOpen(false)} />} ); } /** The new category's name, typed where the category pill was, with its OK and Cancel beside it. */ function NewCategoryField({ inputId, name, setName, create, setOpen, t }: NewCategoryRow & { t: Translate }) { return ( // One parent for the field and both of its buttons. setName(e.target.value)} onKeyDown={(e) => { // Enter confirms the name, like OK; it never reaches the place's own save. if (e.key === 'Enter') { e.preventDefault(); void create(); } // Escape closes the field, like Cancel, and only the field: marked // handled, the dialog around it leaves it alone and keeps what is typed. if (e.key === 'Escape') { e.preventDefault(); setOpen(false); } }} aria-label={t('places.newCategory')} placeholder={t('places.categoryNamePlaceholder')} className="w-48 min-w-0 rounded-full bg-surface-card px-3 py-1 text-content shadow-sm outline-none ring-1 ring-edge-faint placeholder:text-content-faint focus:ring-2 focus:ring-[color:var(--text-primary)]" /> ); }