import { clsx } from 'clsx'; import { Star } from 'lucide-react'; export type RatingSize = 'sm' | 'md' | 'lg'; type StarState = 'filled' | 'half' | 'empty'; export interface RatingProps { value: number; showCount?: boolean; count?: number; size?: RatingSize; className?: string; onCountClick?: () => void; } const sizeStyles: Record = { sm: 'w-4 h-4', md: 'w-5 h-5', lg: 'w-6 h-6', }; const textSizeStyles: Record = { sm: 'text-xs', md: 'text-sm', lg: 'text-base', }; function getStarStates(value: number): StarState[] { const floor = Math.floor(value); const decimal = value - floor; return [...Array(5)].map((_, i) => { if (i < floor) return 'filled'; if (i === floor && decimal >= 0.5) return 'half'; return 'empty'; }); } function HalfStar({ className }: { className: string }) { return ( ); } export function Rating({ value, showCount = false, count, size = 'md', className, onCountClick, }: RatingProps) { const starStates = getStarStates(value); return (
{starStates.map((state, i) => { if (state === 'half') { return ; } return ( ); })}
{showCount && count !== undefined && ( {value}{' '} {onCountClick ? ( ) : ( ({count} reviews) )} )}
); } export default Rating;