import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { getComments, createComment } from '../services/commentService'; import { useAuthStore } from '@/stores/auth'; import { useToast } from '@/hooks/useToast'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { MessageCircle, Send, Loader2 } from 'lucide-react'; import { LoadingSpinner } from '@/components/ui/loading-spinner'; import { CommentThread } from './CommentThread'; import type { TrackComment } from '../services/commentService'; // FE-PAGE-007: Complete Track Detail page implementation - Comments Section interface CommentSectionProps { trackId: string; } export function CommentSection({ trackId }: CommentSectionProps) { const { user } = useAuthStore(); const toast = useToast(); const queryClient = useQueryClient(); const [newComment, setNewComment] = useState(''); const [page, setPage] = useState(1); const limit = 20; const { data: commentsData, isLoading, error, } = useQuery({ queryKey: ['trackComments', trackId, page], queryFn: () => getComments(trackId, page, limit), enabled: !!trackId, }); const createCommentMutation = useMutation({ mutationFn: (content: string) => createComment(trackId, content), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['trackComments', trackId] }); setNewComment(''); toast.success('Commentaire publié'); }, onError: (error: any) => { toast.error(error.message || 'Erreur lors de la publication'); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!newComment.trim() || !user) return; createCommentMutation.mutate(newComment.trim()); }; // Filter to show only top-level comments (no parent_id) const topLevelComments = commentsData?.comments?.filter((c: TrackComment) => !c.parent_id) || []; const total = commentsData?.total || 0; const totalPages = Math.ceil(total / limit); return ( Commentaires ({commentsData?.total || 0}) {/* Comment Form */} {user ? (
setNewComment(e.target.value)} placeholder="Écrire un commentaire..." maxLength={500} />
) : (

Connectez-vous pour commenter

)} {/* Comments List */} {isLoading ? (
) : error ? (
Failed to load comments
) : topLevelComments.length === 0 ? (
Aucun commentaire pour le moment. Soyez le premier à commenter !
) : (
{topLevelComments.map((comment: TrackComment) => ( ))} {/* Pagination */} {totalPages > 1 && (
Page {page} sur {totalPages}
)}
)}
); }