import React, { useState } from 'react' import { createPortal } from 'react-dom' import { Star } from 'lucide-react' import { avatarSrc } from '../../utils/avatarSrc' import { useAuthStore } from '../../store/authStore' import { useTranslation } from '../../i18n' import type { PlaceRatingVote } from '@trek/shared' const STAR_FILL = '#facc15' interface StarsProps { /** Value the stars visualize (average for display, own vote while hovering). */ value: number size?: number /** Interactive mode: hover previews, click casts/clears the own vote. */ onRate?: (rating: number | null) => void myRating?: number | null ariaLabel?: string } /** Five stars with fractional fill; interactive when onRate is set (#1435). * Clicking the star matching the own vote clears it. */ export function Stars({ value, size = 15, onRate, myRating = null, ariaLabel }: StarsProps) { const [hover, setHover] = useState(null) const shown = hover ?? value return (
setHover(null)} > {[1, 2, 3, 4, 5].map(i => { // Fractional fill for averages: full below, empty above, clipped between. const fill = Math.max(0, Math.min(1, shown - (i - 1))) const star = ( {fill > 0 && ( )} ) if (!onRate) return star return ( ) })}
) } interface PlaceRatingProps { ratings: PlaceRatingVote[] ratingAvg: number | null | undefined onRate?: (rating: number | null) => void size?: number /** Hide the voter avatar strip (compact mobile rows). */ compact?: boolean } /** * The collaborative rating row (#1435): interactive stars (hover previews the * own vote, the average fill shows otherwise), the numeric average, and the * voter avatars. Hovering reveals a custom tooltip listing every voter with * their stars. */ export default function PlaceRating({ ratings, ratingAvg, onRate, size = 16, compact = false }: PlaceRatingProps) { const { t } = useTranslation() const currentUserId = useAuthStore(s => s.user?.id) const [tip, setTip] = useState<{ top: number; left: number } | null>(null) const myRating = ratings.find(r => r.user_id === currentUserId)?.rating ?? null const avg = ratingAvg ?? null const showTip = (e: React.MouseEvent) => { if (ratings.length === 0) return const r = e.currentTarget.getBoundingClientRect() setTip({ top: r.top, left: r.left + r.width / 2 }) } return (
setTip(null)} > {avg !== null ? ( {(Math.round(avg * 10) / 10).toLocaleString()} ({ratings.length}) ) : ( {t('places.notRated')} )} {/* Who voted — avatar strip. */} {!compact && ratings.length > 0 && ( {ratings.slice(0, 6).map((r, i) => ( {r.avatar ? {r.username : r.username?.[0]?.toUpperCase()} ))} )} {/* Custom voter tooltip — fixed at the root so nothing clips it. */} {tip && ratings.length > 0 && createPortal(
{ratings.map(r => (
{r.username}{r.user_id === currentUserId ? ` (${t('vacay.you')})` : ''}
))}
, document.body, )}
) }