"use client" import { useState, useEffect } from "react" import { RatingInput, RatingBadge } from "@/components/ui/rating-input" import { Card } from "@/components/ui/card" import { getApiUrl } from "@/lib/api-config" import { Sparkles, TrendingUp } from "lucide-react" interface EntityRatingProps { entityType: "show" | "song" | "venue" | "tour" | "performance" entityId: number compact?: boolean ratingCount?: number rank?: number // Rank of this performance vs others isHeady?: boolean // Is this a top-rated "heady" version } export function EntityRating({ entityType, entityId, compact = false, ratingCount, rank, isHeady = false }: EntityRatingProps) { // const { user, token } = useAuth() // Unused, keeping hook for context if needed but removing vars to fix lint const [userRating, setUserRating] = useState(null) const [averageRating, setAverageRating] = useState(0) const [loading, setLoading] = useState(false) const [hasRated, setHasRated] = useState(false) useEffect(() => { // Fetch average rating fetch(`${getApiUrl()}/social/ratings/average?${entityType}_id=${entityId}`) .then(res => res.ok ? res.json() : 0) .then(data => setAverageRating(data || 0)) .catch(() => setAverageRating(0)) // Fetch user's rating if logged in const storedToken = localStorage.getItem("token") if (storedToken) { fetch(`${getApiUrl()}/social/ratings/me?${entityType}_id=${entityId}`, { headers: { Authorization: `Bearer ${storedToken}` } }) .then(res => res.ok ? res.json() : null) .then(data => { if (data?.score) { setUserRating(data.score) setHasRated(true) } }) .catch(() => { }) } }, [entityType, entityId]) const handleRate = async (score: number) => { const storedToken = localStorage.getItem("token") if (!storedToken) { alert("Please log in to rate.") return } setLoading(true) try { const body: Record = { score } body[`${entityType}_id`] = entityId const res = await fetch(`${getApiUrl()}/social/ratings`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${storedToken}` }, body: JSON.stringify(body) }) if (!res.ok) throw new Error("Failed to submit rating") const data = await res.json() setUserRating(data.score) setHasRated(true) // Re-fetch average fetch(`${getApiUrl()}/social/ratings/average?${entityType}_id=${entityId}`) .then(res => res.ok ? res.json() : averageRating) .then(setAverageRating) } catch (err) { console.error(err) alert("Error submitting rating") } finally { setLoading(false) } } if (compact) { return (
{isHeady && ( Heady )} {averageRating > 0 && ( )} {rank && ( #{rank} )}
) } return (
Your Rating {isHeady && ( Heady Version )}
{rank && ( Ranked #{rank} )} {averageRating > 0 && ( Community: {averageRating.toFixed(1)} {ratingCount && ` (${ratingCount})`} )}
{loading && (

Submitting...

)} {hasRated && !loading && userRating && (

Your rating: {userRating.toFixed(1)}/10

)}
) }