import { useEffect, useRef, useState } from 'react' import { Check, ChevronDown, Users } from 'lucide-react' import type { ReservationTraveler } from '@trek/shared' import { SPLIT_COLORS } from '../Budget/BudgetPanel.constants' import type { TripMember } from '../Budget/BudgetPanelMemberChips' import GuestBadge from '../shared/GuestBadge' import { useTranslation } from '../../i18n' // Deterministic colour per user so the same person keeps their gradient across the // picker, the card and the cost-split chips (mirrors BudgetPanel.helpers). function colorFor(userId: number) { const n = SPLIT_COLORS.length return SPLIT_COLORS[(((Math.trunc(userId) - 1) % n) + n) % n] } function Avatar({ userId, username, avatarUrl, size = 22, dim = false }: { userId: number username?: string avatarUrl?: string | null size?: number dim?: boolean }) { const base = { width: size, height: size, borderRadius: '50%', flexShrink: 0, opacity: dim ? 0.5 : 1 } as const if (avatarUrl) return return ( {(username || '?').charAt(0).toUpperCase()} ) } /** * Who travels on a booking or transport, for its edit and create dialogs * (#1517): a field shaped like the TREK dropdown beside it, showing the chosen * people as avatars and names, that opens a list to tick them on and off. * Guests ride along on tripMembers, so no separate list is needed. */ export function TravelerPicker({ tripMembers, selectedIds, onToggle }: { tripMembers: TripMember[] selectedIds: Set onToggle: (id: number) => void }) { const { t } = useTranslation() const [open, setOpen] = useState(false) const rootRef = useRef(null) useEffect(() => { if (!open) return const closeOnOutsidePointer = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) setOpen(false) } document.addEventListener('pointerdown', closeOnOutsidePointer) return () => document.removeEventListener('pointerdown', closeOnOutsidePointer) }, [open]) const field = { width: '100%', minHeight: 38, display: 'flex', alignItems: 'center', gap: 8, padding: '5px 12px 5px 8px', borderRadius: 10, border: '1px solid var(--border-primary)', background: 'var(--bg-input)', fontSize: 'calc(13px * var(--fs-scale-body, 1))', fontWeight: 500, fontFamily: 'inherit', textAlign: 'start', } as const if (tripMembers.length === 0) { return
{t('reservations.travelers.none')}
} const chosen = tripMembers.filter(m => selectedIds.has(m.id)) return (
{open && (
{tripMembers.map(m => { const on = selectedIds.has(m.id) return ( ) })}
)}
) } /** * Read-only traveler pills for a reservation card. Each person is a rounded badge * (avatar + name) on bg-surface-card so they stand out inside the card's grey field * box. Renders nothing when nobody is assigned, so a card without travelers stays * clean (#1517). */ export function TravelerAvatarRow({ travelers }: { travelers?: ReservationTraveler[] }) { if (!travelers || travelers.length === 0) return null return (
{travelers.map(tv => ( {tv.username} {!!tv.is_guest && } ))}
) } /** * Avatar filter row for the reservations toolbar — toggle to show only bookings * assigned to the picked members/guests (#1517/#1557). Active avatars get an accent * ring + check badge; the rest dim once any filter is on. Gradient avatars keep it * consistent with the traveler pills and the cost-split chips. */ export function TravelerFilterAvatars({ members, active, onToggle, label }: { members: TripMember[] active: Set onToggle: (id: number) => void label?: string }) { const hasFilter = active.size > 0 return (
{members.map(m => { const on = active.has(m.id) const dim = hasFilter && !on return ( ) })}
) }