import React, { useEffect, useMemo, useRef } from 'react' import { EyeOff, FileDown } from 'lucide-react' import type { TourListItem } from '@trek/shared' import type { Day } from '../../types' import { useTranslation } from '../../i18n' import TourListRow from './TourListRow' import { useTourPermissions, type TourPermissionProps } from './useTourPermissions' import { NEUTRAL_TINT, fs } from '../shared/DialogShell' import { Tooltip } from '../shared/Tooltip' import EmptyState from '../shared/EmptyState' import { TourDayMenu } from './tourParts' import TourListFilterMenu, { TourFilterEmpty } from './TourListFilterMenu' import { useTourListFilters } from './useTourListFilters' import TourGpxPreviewDialog from './TourGpxPreviewDialog' import { useTourGpxImport } from './useTourGpxImport' import { type AssignTourToDay, useTourDayAssignment } from './useTourDayAssignment' import { useTourMapVisibility } from './useTourMapVisibility' import { useTripStore } from '../../store/tripStore' import { resolveTrackColor } from '../Map/trackColors' interface ToursSidebarProps extends TourPermissionProps { tripId: number days: Day[] tours: TourListItem[] loading?: boolean selectedPlaceId?: number | null onAssignToDay: AssignTourToDay pushUndo?: (label: string, undo: () => void | Promise) => void /** Imported place ids are excluded immediately; no ids means refresh derived Tour metadata. */ onToursChanged?: (placeIds?: number[]) => void | Promise /** Tour row was clicked — caller will render the detail modal. */ onSelectTour?: (tour: TourListItem | null, opener?: HTMLElement) => void } /** * Tours mode of the right add-panel: a selection list of existing tours * with GPX import and day assignment. * * Reuses the existing day-assignment mechanism as-is: `onAssignToDay` is the * same `handleAssignToDay` PlacesSidebar calls, so a tour lands on a day * exactly like a place does, and its route line renders on the day map * without any Tours-specific rendering code. */ export default function ToursSidebar({ tripId, days, tours, loading = false, selectedPlaceId = null, onAssignToDay, onToursChanged, onSelectTour, canEdit: editPermission, canAssign: assignPermission }: ToursSidebarProps): React.ReactElement { const { t } = useTranslation() const { canEdit, canAssign } = useTourPermissions({ tripId, canEdit: editPermission, canAssign: assignPermission }) const gpxImport = useTourGpxImport({ tripId, canEdit, onToursChanged }) const filters = useTourListFilters(tours) const dayAssignment = useTourDayAssignment({ canAssign, onAssignToDay }) const tourIds = useMemo(() => new Set(tours.map(tour => tour.place_id)), [tours]) const visibility = useTourMapVisibility(tripId, tourIds) // The colour each line has on the map, so a row and its line read as one (PR1-08). const places = useTripStore(state => state.places) const trackColors = useMemo( () => new Map(places.filter(place => tourIds.has(place.id)).map(place => [place.id, resolveTrackColor(place)])), [places, tourIds], ) const listRef = useRef(null) // A tour picked on the map shows in the list too, scrolled into view. useEffect(() => { if (selectedPlaceId == null) return const row = listRef.current?.querySelector(`[data-place-id="${selectedPlaceId}"]`) row?.scrollIntoView?.({ block: 'nearest' }) }, [selectedPlaceId]) return (
{/* GPX import stays — the tour-appropriate import path. List Import (place/route-of-driving oriented) is deliberately absent here. */}
{visibility.hiddenCount > 0 && (
{t('tours.visibility.hidden')} {visibility.hiddenCount}
)}
{!loading && filters.emptyByFilter ? ( ) : !loading && filters.filtered.length === 0 ? ( {t('tours.empty.body')}

} /> ) : (
    {filters.filtered.map(tour => ( 0} selected={tour.place_id === selectedPlaceId} trackColor={trackColors.get(tour.place_id)} mapToggle={{ hidden: visibility.isHidden(tour.place_id), onToggle: () => visibility.toggle(tour.place_id), showLabel: t('tours.visibility.show'), hideLabel: t('tours.visibility.hide'), }} onSelect={(selectedTour, opener) => onSelectTour?.(selectedTour, opener)} action={} /> ))}
)}
) }