import React, { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { StarIcon } from '@phosphor-icons/react'; import { useAuth } from '../../context/AuthContext'; import { useToast } from '../../context/ToastContext'; import { CourseReviewSummary, MyCourseReview, deleteMyCourseReview, getCourseReviews, getMyCourseReview, saveCourseReview } from '../../services/db'; import { ConfirmDialog } from '../ui/ConfirmDialog'; import { shortDate } from '../../lib/format'; import { paths } from '../../lib/routes'; const Stars: React.FC<{ value: number; label?: string }> = ({ value, label }) => ( {[1, 2, 3, 4, 5].map((star) => )} ); export const CourseReviews: React.FC<{ courseId: string }> = ({ courseId }) => { const { user } = useAuth(); const { showToast } = useToast(); const [summary, setSummary] = useState(null); const [mine, setMine] = useState(null); const [rating, setRating] = useState(0); const [comment, setComment] = useState(''); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [deleteOpen, setDeleteOpen] = useState(false); const load = async () => { setLoading(true); try { const [publicReviews, myReview] = await Promise.all([ getCourseReviews(courseId), user ? getMyCourseReview(courseId).catch(() => null) : Promise.resolve(null), ]); setSummary(publicReviews); setMine(myReview); setRating(myReview?.rating ?? 0); setComment(myReview?.comment ?? ''); } catch { setSummary(null); } finally { setLoading(false); } }; useEffect(() => { void load(); }, [courseId, user?.id]); const submit = async (event: React.FormEvent) => { event.preventDefault(); if (!rating) { showToast('Escolha uma nota de 1 a 5 estrelas.', 'error'); return; } setSaving(true); try { const result = await saveCourseReview(courseId, rating, comment.trim()); setMine(result.review); setSummary(result.summary); setRating(result.review.rating); setComment(result.review.comment); showToast('Avaliação salva.', 'success'); } catch { showToast('Não foi possível salvar sua avaliação.', 'error'); } finally { setSaving(false); } }; const remove = async () => { setDeleteOpen(false); try { await deleteMyCourseReview(courseId); setMine(null); setRating(0); setComment(''); setSummary(await getCourseReviews(courseId)); showToast('Avaliação removida.', 'success'); } catch { showToast('Não foi possível remover sua avaliação.', 'error'); } }; const average = summary?.average ?? 0; const count = summary?.count ?? 0; return

Avaliações

{count > 0 && {count} {count === 1 ? 'avaliação' : 'avaliações'}}
{loading ?

Carregando avaliações…

: <>
{count ? average.toFixed(1) : '—'} {count ? `${count} ${count === 1 ? 'pessoa avaliou' : 'pessoas avaliaram'}` : 'Seja a primeira pessoa a avaliar'}
{user ?
{mine ? 'Sua avaliação' : 'Avalie este curso'}
{[1, 2, 3, 4, 5].map((star) => )}