import { useEffect, useRef, useState, type MouseEvent as ReactMouseEvent, type ReactNode } from 'react' import { Search, Plus, X, Upload, FileDown, ListPlus, Check, Star, CalendarPlus, CalendarDays, ChevronDown, Tag, Globe2, ArrowDownUp } from 'lucide-react' import { Tooltip } from '../shared/Tooltip' import { ContextMenu, useContextMenu } from '../shared/ContextMenu' import { NEUTRAL_TINT, fs } from '../shared/DialogShell' import { useElementSize } from '../../hooks/useElementSize' import { Eyebrow } from './bookings/bookingParts' import { tintOf } from './planParts' import { CategoryTile } from './PlacesBulkCategoryModal' import type { PlacesFilter, SidebarState } from './usePlacesSidebar' import { PLACES_SORTS, type PlacesSort } from './placesSort' /** * Below this the two labels stop fitting side by side and both buttons fall back * to their icon. Measured on the add row itself, the full width of the head band, * rather than derived from the sidebar width: the labels are translated, so how * much room they need differs per locale, and the row is what runs out of space. * The rail is draggable down to 200px, which leaves the row 172. */ const COMPACT_BUTTONS_WIDTH = 232 /** The controls of the head band: one height, one radius, the card colour of the other trip bars. */ const CONTROL = 'h-8 rounded-[10px] shadow-sm' /** A 32px icon control on the head band, named by its tooltip. `on` fills it in the accent (a mode that is switched on). */ function BandButton({ label, onClick, on = false, lit = false, ariaPressed, ariaExpanded, ariaHaspopup, children }: { label: string onClick: (e: ReactMouseEvent) => void on?: boolean /** Brighter while its panel or menu is open, or while it is filtering. */ lit?: boolean ariaPressed?: boolean ariaExpanded?: boolean ariaHaspopup?: 'dialog' | 'menu' children: ReactNode }) { let tone = 'bg-surface-card text-content-secondary hover:text-content' if (on) tone = 'bg-accent text-accent-text' else if (lit) tone = 'bg-surface-card text-content' return ( ) } const RATING_FLOORS = ['all', 5, 4, 3, 2, 1] as const export function PlacesDropOverlay({ t }: SidebarState) { return (
{t('places.sidebarDrop')}
) } /** * The head of the places column, one tinted band like every panel head in the * planner, in three rows: adding and importing first, as the column's main job; * search with the category and rating filters and the select switch; what to * show as tabs with their counts. While a day is open on the planned tab, a chip * says so. */ export function PlacesHeader(S: SidebarState) { const { canEditPlaces, t, selectMode, setSelectMode, setSelectedIds } = S return (
{canEditPlaces && }
{canEditPlaces && ( { setSelectMode(v => !v); setSelectedIds(new Set()) }} > {selectMode ? : } )}
) } function SearchField({ t, search, setSearch, selectMode, setSelectedIds }: SidebarState) { return ( ) } /** A category (or the no-category bucket) in the category list, ticked while it filters. */ function CategoryChoice({ on, onClick, tile, label }: { on: boolean; onClick: () => void; tile: ReactNode; label: string }) { return ( ) } /** The "show" choices; tracks only once a place has one. */ function filterTabs({ t, hasTracks }: Pick) { const tabs: Array<{ id: PlacesFilter; label: string }> = [ { id: 'all', label: t('places.all') }, { id: 'unplanned', label: t('places.unplanned') }, { id: 'planned', label: t('places.planned') }, ] if (hasTracks) tabs.push({ id: 'tracks', label: t('places.filterTracks') }) return tabs } /** What the list shows (every place, the unplanned, the planned, the tracks) as one dropdown with their counts. */ function ShowDropdown(S: SidebarState) { const { t, filter, pickFilter, filterCounts } = S const [open, setOpen] = useState(false) const tabs = filterTabs(S) const current = tabs.find(tab => tab.id === filter) ?? tabs[0] return (
{tabs.map(tab => { const on = filter === tab.id return ( ) })}
) } /** * A filter as one icon on the tabs row, opening a small list under it; closes on a * click outside or Escape. A badge marks it while it is narrowing the list, and the * tooltip says to what. */ function FilterDropdown({ label, active, badge, icon, open, setOpen, name, children, wide = false, count }: { /** What is in force: on the trigger when `wide`, else in the tooltip ("All categories", "4+"). */ label: string active: boolean badge?: string | number icon?: ReactNode /** Takes the row's free width and shows its label (and `count`) on the trigger. */ wide?: boolean count?: number open: boolean setOpen: (v: boolean | ((v: boolean) => boolean)) => void /** The trigger's accessible name: what it filters by. */ name: string children: ReactNode }) { const wrapRef = useRef(null) useEffect(() => { if (!open) return const onDown = (e: MouseEvent) => { if (!wrapRef.current?.contains(e.target as Node)) setOpen(false) } // Captured and stopped, so the key takes back the list and nothing under it. const onKey = (e: KeyboardEvent) => { if (e.key !== 'Escape') return e.stopPropagation() setOpen(false) wrapRef.current?.querySelector('button')?.focus() } document.addEventListener('mousedown', onDown) document.addEventListener('keydown', onKey, true) return () => { document.removeEventListener('mousedown', onDown) document.removeEventListener('keydown', onKey, true) } }, [open, setOpen]) return (
{wide ? ( ) : ( )} {open && (
{children}
)}
) } /** Which categories to show, many at once; the label says what is in force. */ function CategoryFilter(S: SidebarState) { const { t, places, categories, categoryFilters, setCategoryFilters, toggleCategoryFilter } = S const [open, setOpen] = useState(false) if (categories.length === 0) return null let label = t('places.allCategories') if (categoryFilters.size === 1) { const only = categories.find(c => categoryFilters.has(String(c.id))) if (categoryFilters.has('uncategorized')) label = t('places.noCategory') else if (only) label = only.name } else if (categoryFilters.size > 1) { label = `${categoryFilters.size} ${t('places.categoriesSelected')}` } return ( 0} badge={categoryFilters.size} icon={} open={open} setOpen={setOpen}> {categories.map(c => ( toggleCategoryFilter(String(c.id))} tile={} label={c.name} /> ))} {places.some(p => p.category_id == null) && ( toggleCategoryFilter('uncategorized')} tile={} label={t('places.noCategory')} /> )} {categoryFilters.size > 0 && ( )} ) } /** A minimum of stars (#1435), the same floors as the collections bar. */ function RatingFilter(S: SidebarState) { const { t, ratingFilter, setRatingFilter } = S const [open, setOpen] = useState(false) const star = return ( } open={open} setOpen={setOpen} > {RATING_FLOORS.map(opt => { const on = ratingFilter === opt return ( ) })} ) } const SORT_LABEL_KEYS: Record = { newest: 'places.sortNewest', oldest: 'places.sortOldest', name: 'places.sortName', rating: 'places.sortRating', updated: 'places.sortUpdated', } /** The list's order (#2093); marked while it is anything but newest first. */ function SortDropdown(S: SidebarState) { const { t, placesSort, setPlacesSort } = S const [open, setOpen] = useState(false) const active = placesSort !== 'newest' return ( } open={open} setOpen={setOpen} >
{t('places.sortBy')}
{PLACES_SORTS.map(opt => { const on = placesSort === opt return ( ) })}
) } /** * Country, or one region in it, from each place's resolved position (#2537). Shown once * the trip spans more than one country or region; a single one has nothing to narrow. */ function LocalityFilterDropdown(S: SidebarState) { const { t, localities, localityFilter, setLocalityFilter } = S const [open, setOpen] = useState(false) const regionCount = localities.reduce((n, g) => n + g.regions.length, 0) if (localities.length < 2 && regionCount < 2) return null const pick = (country: string, region: string | null) => { setLocalityFilter({ country, region }); setOpen(false) } const row = (key: string, label: string, count: number, on: boolean, onClick: () => void, indent: boolean) => ( ) const label = localityFilter ? (localityFilter.region ?? localityFilter.country) : t('places.allLocations') return ( } open={open} setOpen={setOpen}> {localities.map(g => (
{row(`c:${g.country}`, g.country, g.count, localityFilter?.country === g.country && localityFilter.region == null, () => pick(g.country, null), false)} {g.regions.map(r => row(`c:${g.country}:${r.region}`, r.region, r.count, localityFilter?.country === g.country && localityFilter.region === r.region, () => pick(g.country, r.region), true))}
))} {localityFilter && ( )}
) } function AddRow(S: SidebarState) { const { onAddPlace, onAddPlaceToSelectedDay, selectedDayId, t, setFileImportOpen, setListImportOpen, hasMultipleListImportProviders, toursEnabled } = S const dayOpen = selectedDayId != null const split = dayOpen && !!onAddPlaceToSelectedDay const { ref: rowRef, width: rowWidth } = useElementSize() // Zero is the first paint, before the observer has measured anything — treat // that as roomy so the labels do not flash away and back on every mount. const compact = rowWidth > 0 && rowWidth < COMPACT_BUTTONS_WIDTH const addLabel = t(dayOpen ? 'places.addPlaceShort' : 'places.addPlace') const importMenu = useContextMenu() const importItems = [ ...(!toursEnabled ? [{ label: t('places.importFile'), icon: FileDown, onClick: () => setFileImportOpen(true) }] : []), { label: t(hasMultipleListImportProviders ? 'places.importList' : 'places.importGoogleList'), icon: ListPlus, onClick: () => setListImportOpen(true) }, ] return (
{/* The label shortens while the second button is out, so both fit side by side in a default rail without either one truncating; a squeezed rail drops the labels entirely and the aria-labels carry the names instead. */}
{/* Kept mounted and collapsed rather than unmounted, so it has something to animate out of. Hidden from the tab order and from screen readers in the same breath, because a zero-width button is still focusable otherwise. */} {onAddPlaceToSelectedDay && ( )}
{ if (importMenu.menu) importMenu.close(); else importMenu.open(e, importItems, true) }} >
) } /** A filter in force, said out loud, with the X that lifts it. */ function FilterChip({ label, clearLabel, onClear, icon, tone = 'neutral' }: { label: string /** The X's name; its tooltip, too, unless the X says more than the tooltip. */ clearLabel: string onClear?: () => void icon?: ReactNode tone?: 'neutral' | 'info' }) { const look = tone === 'info' ? 'bg-info-soft text-info' : 'border border-edge-faint bg-surface-card text-content-secondary' return ( {icon} {label} {onClear && ( )} ) } function ActiveFilterChips(S: SidebarState) { const { t, dayScoped, onClearSelectedDay } = S // Says out loud what the count below already narrowed to. The map has followed // the open day on this filter since #2024, and until this said so the pool read 55 // beside five pins and the honest conclusion was that the map was broken. The X // closes the day rather than changing the filter. if (!dayScoped) return null return (
} />
) }