Files
TREK/client/src/components/Collections/CollectionList.tsx
T
Maurice 501e4449ef refactor(places): one label for a rating badge
The list rows and the map pins wrote the average with their own copy of
the whole star or one decimal rule; both now read ratingBadge.
2026-11-30 12:34:50 +01:00

137 lines
5.8 KiB
TypeScript

import React, { useEffect, useMemo, useRef } from 'react'
import { Check, GripVertical, Info, MapPin } from 'lucide-react'
import type { CollectionPlace, CollectionStatus, CollectionLabel } from '@trek/shared'
import type { TranslationFn } from '../../types'
import PlaceAvatar from '../shared/PlaceAvatar'
import StatusBadge from './StatusBadge'
import EntryTagDots from './EntryTagDots'
import { usePlaceReorder } from './usePlaceReorder'
import CategoryBadge from './CategoryBadge'
import { ratingBadge } from './ratingLabel'
interface CollectionListProps {
places: CollectionPlace[]
labels: CollectionLabel[]
selectedPlaceId: number | null
selectMode: boolean
selectedIds: number[]
onOpenPlace: (id: number) => void
onStatusChange?: (placeId: number, status: CollectionStatus) => void
onToggleSelect: (id: number) => void
/** The list's own order may be dragged right now (#1457). */
reorderEnabled?: boolean
/** It could be, but a search or filter hides places, so the list says why not. */
reorderBlocked?: boolean
onReorder?: (orderedIds: number[]) => void
t: TranslationFn
}
/**
* List view — one glass row per saved place with a photo avatar, name +
* category/address, and a one-tap status cycle on the badge. Click the row to
* open the place (or toggle it in select mode).
*/
/** The row's classes: picked, draggable, being dragged, and the side a drop would land on. */
function rowClass(on: boolean, canReorder: boolean, dragging: boolean, dropSide: string | null | undefined): string {
const parts = ['col-lrow', 'col-lrow-place']
if (on) parts.push('sel')
if (canReorder) parts.push('can-reorder')
if (dragging) parts.push('dragging')
if (dropSide) parts.push(`drop-${dropSide}`)
return parts.join(' ')
}
/**
* The place's picture with the list's average vote on its corner, so the row needs no
* line for it; the category stands as an icon before the name instead.
*/
function RowAvatar({ place, t }: { place: CollectionPlace; t: TranslationFn }) {
const category = place.category ? { color: place.category.color ?? undefined, icon: place.category.icon ?? undefined } : null
const rating = ratingBadge(place.rating_avg)
return (
<span className="col-lrow-avatar">
<PlaceAvatar place={place} size={40} category={category} />
{rating && (
<span className="col-lrow-ratebadge" role="img" aria-label={t('collections.ratingBadge', { rating })}>
{rating}
</span>
)}
</span>
)
}
export default function CollectionList({
places, labels, selectedPlaceId, selectMode, selectedIds, onOpenPlace, onStatusChange, onToggleSelect,
reorderEnabled = false, reorderBlocked = false, onReorder, t,
}: CollectionListProps): React.ReactElement {
const labelsById = useMemo(() => new Map(labels.map(l => [l.id, l])), [labels])
// Bring the selected row into view — e.g. when it was picked from the map.
const selectedRef = useRef<HTMLDivElement>(null)
useEffect(() => {
selectedRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
}, [selectedPlaceId])
const order = useMemo(() => places.map(p => p.id), [places])
const reorder = usePlaceReorder(order, reorderEnabled && !!onReorder, ids => onReorder?.(ids))
return (
<div className="col-listview">
{reorderBlocked && (
<p className="col-reorder-note"><Info size={13} /> {t('collections.reorderBlocked')}</p>
)}
{places.map(place => {
const selected = selectedIds.includes(place.id)
const active = selectedPlaceId === place.id
const placeLabels = (place.label_ids ?? []).map(id => labelsById.get(id)).filter(Boolean) as CollectionLabel[]
return (
<div
key={place.id}
ref={active ? selectedRef : undefined}
role="button"
// Opt out of the global :active press-scale: shrinking the row-wide button
// slides the status badge out from under the pointer mid-click, so the click
// retargets onto the row and opens the place instead of cycling (#2158).
data-no-press
tabIndex={0}
onClick={() => (selectMode ? onToggleSelect(place.id) : onOpenPlace(place.id))}
onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); if (selectMode) onToggleSelect(place.id); else onOpenPlace(place.id) } }}
className={rowClass(active || selected, reorder.enabled, reorder.dragId === place.id, reorder.dropSide(place.id))}
{...reorder.rowProps(place.id)}
>
{reorder.enabled && (
<span
className="col-lgrip"
role="button"
tabIndex={0}
aria-label={t('collections.reorderHandle', { name: place.name })}
onClick={e => e.stopPropagation()}
{...reorder.handleProps(place.id)}
>
<GripVertical size={14} />
</span>
)}
{selectMode ? (
<span className={`col-lcheck${selected ? ' on' : ''}`}>{selected && <Check size={14} strokeWidth={3} />}</span>
) : (
<RowAvatar place={place} t={t} />
)}
<div className="li">
<div className="t">{place.category?.name && <CategoryBadge category={place.category} />}<span className="nm">{place.name}</span><EntryTagDots tags={placeLabels} /></div>
{place.address && (
<div className="s">
<MapPin size={11} />
<span>{place.address}</span>
</div>
)}
</div>
<div className="col-lrow-status">
<StatusBadge status={place.status} onChange={selectMode || !onStatusChange ? undefined : next => onStatusChange(place.id, next)} t={t} />
</div>
</div>
)
})}
</div>
)
}