import { useState } from 'react'
interface Props {
value: number
/** Scale length, from capabilities.tracks.rating_max. */
max?: number
/** When provided, the stars become clickable to set the rating. */
onChange?: (value: number) => void
}
function Star({ className }: { className?: string }) {
return (
)
}
// Compact star rating. Read-only by default (shows a dash when unrated). When
// `onChange` is given it's interactive: hollow (☆) above the rating, solid gold
// (★) up to it, with hover preview. Clicking a star sets that rating; clicking
// the current top star again clears it to 0.
export function RatingStars({ value, max = 5, onChange }: Props) {
const [hover, setHover] = useState(0)
const stars = Array.from({ length: max }, (_, i) => i + 1)
if (!onChange) {
if (!value) return —
return (
{stars.map((i) => (
))}
)
}
const shown = hover || value
return (
{stars.map((i) => (
))}
)
}