import { useState } from 'react' import { X, ThumbsUp, ThumbsDown } from 'lucide-react' import { StarRating } from '@/components/common/StarRating' import { cn } from '@/lib/utils' import type { Step } from '@/types' interface StepRatingData { rating: number helpful: boolean | null review: string } interface StepRatingModalProps { isOpen: boolean onClose: () => void onSubmit: (ratings: Map) => Promise librarySteps: Step[] isSaving?: boolean } export function StepRatingModal({ isOpen, onClose, onSubmit, librarySteps, isSaving = false }: StepRatingModalProps) { // Store ratings for each step const [ratings, setRatings] = useState>(new Map()) if (!isOpen) return null const handleRatingChange = (stepId: string, rating: number) => { setRatings(prev => { const updated = new Map(prev) const existing = updated.get(stepId) || { rating: 0, helpful: null, review: '' } updated.set(stepId, { ...existing, rating }) return updated }) } const handleHelpfulChange = (stepId: string, helpful: boolean) => { setRatings(prev => { const updated = new Map(prev) const existing = updated.get(stepId) || { rating: 0, helpful: null, review: '' } // Toggle: if clicking same button, set to null const newHelpful = existing.helpful === helpful ? null : helpful updated.set(stepId, { ...existing, helpful: newHelpful }) return updated }) } const handleReviewChange = (stepId: string, review: string) => { setRatings(prev => { const updated = new Map(prev) const existing = updated.get(stepId) || { rating: 0, helpful: null, review: '' } updated.set(stepId, { ...existing, review }) return updated }) } const handleSubmit = async () => { // Filter out steps with no rating const ratingsToSubmit = new Map( Array.from(ratings.entries()).filter(([, data]) => data.rating > 0) ) if (ratingsToSubmit.size === 0) { // No ratings to submit, just close onClose() return } await onSubmit(ratingsToSubmit) } const getRating = (stepId: string) => ratings.get(stepId) return (
{/* Header */}

Rate Your Experience

Help others by rating the steps you used ({librarySteps.length} step{librarySteps.length !== 1 ? 's' : ''})

{/* Steps List */}
{librarySteps.map((step) => { const rating = getRating(step.id) return (
{/* Step Title */}

{step.title}

{step.step_type}

{/* Star Rating */}
handleRatingChange(step.id, value)} size="lg" />
{/* Was this helpful? */}
{/* Optional Review */}