import { useEffect, useRef, useState, type MouseEvent as ReactMouseEvent, type ReactNode } from 'react' import { Search, Plus, X, Upload, Loader2, 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 { SidebarState } from './usePlacesSidebar' import { placesFilterTabs, usePlacesFilterTags } from './usePlacesPool' import { PLACES_SORTS, type PlacesSort } from './placesSort' import { RATING_FLOORS, UNCATEGORIZED, tagFilterKey } from '../../utils/placesFilter' import type { TagChoice } from '../../utils/placeTags' /** * 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 ( ) } /** A file dragged over the column, and while the Tours import reads one dropped or picked here. */ export function PlacesDropOverlay({ t, tourFileImport }: SidebarState) { const reading = tourFileImport.importing return (
{reading ? : } {t(reading ? 'tours.import.importing' : '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, toggleSelectMode } = S return (
{canEditPlaces && }
{canEditPlaces && ( {selectMode ? : } )}
{/* The filters' lists open across this whole row (see FilterDropdown). */}
) } function SearchField({ t, search, setSearch, updateSearch }: 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 ( ) } /** 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 = placesFilterTabs(t, S.hasTracks, S.hasVisited) 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 list under the row; closes on a * click outside or Escape. A badge marks it while it is narrowing the list, and the * tooltip says to what. The list spans the whole row rather than hanging off its * trigger: hung off an icon near the end, it ran out of the sidebar and was cut off. */ 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}
)}
) } /** * What the category filter says is in force: nothing, the one name, or how many * categories. Once a tag is ticked too it is no count of categories any more, so * the names say it. */ function categoryFilterLabel({ t, categories, categoryFilters }: SidebarState, tags: TagChoice[]): string { if (categoryFilters.size === 0) return t('places.allCategories') const tagNames = tags.filter(tag => categoryFilters.has(tagFilterKey(tag.id))).map(tag => tag.name) if (tagNames.length === 0 && categoryFilters.size > 1) { return `${categoryFilters.size} ${t('places.categoriesSelected', { count: categoryFilters.size })}` } const names = categories.filter(c => categoryFilters.has(String(c.id))).map(c => c.name) if (categoryFilters.has(UNCATEGORIZED)) names.push(t('places.noCategory')) return [...names, ...tagNames].join(', ') || t('places.allCategories') } /** Which categories and which tags 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) const tags = usePlacesFilterTags(places) if (categories.length === 0 && tags.length === 0) return null 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')} /> )} {/* The tags, in the same list: a place has one category and any number of these. */} {tags.length > 0 && {t('places.tags')}} {tags.map(tag => ( toggleCategoryFilter(tagFilterKey(tag.id))} tile={( )} label={tag.name} /> ))} {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, pickImportFile, setListImportOpen, hasMultipleListImportProviders } = 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 = [ // With Tours on this opens the Tours file picker: a track is a tour now, and points still reach the places import. { label: t('places.importFile'), icon: FileDown, onClick: pickImportFile }, { 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 (
} />
) }