2025-12-24 11:57:49 +00:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
2025-12-25 10:51:52 +00:00
|
|
|
import { getComments, createComment } from '../services/commentService';
|
2025-12-26 08:11:41 +00:00
|
|
|
import { useAuthStore } from '@/features/auth/store/authStore';
|
2025-12-24 11:57:49 +00:00
|
|
|
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';
|
2025-12-25 10:51:52 +00:00
|
|
|
import { MessageCircle, Send, Loader2 } from 'lucide-react';
|
2025-12-24 11:57:49 +00:00
|
|
|
import { LoadingSpinner } from '@/components/ui/loading-spinner';
|
2025-12-25 10:51:52 +00:00
|
|
|
import { CommentThread } from './CommentThread';
|
|
|
|
|
import type { TrackComment } from '../services/commentService';
|
2025-12-24 11:57:49 +00:00
|
|
|
|
|
|
|
|
// 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('');
|
2025-12-25 10:51:52 +00:00
|
|
|
toast.success('Commentaire publié');
|
2025-12-24 11:57:49 +00:00
|
|
|
},
|
|
|
|
|
onError: (error: any) => {
|
2025-12-25 10:51:52 +00:00
|
|
|
toast.error(error.message || 'Erreur lors de la publication');
|
2025-12-24 11:57:49 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (!newComment.trim() || !user) return;
|
|
|
|
|
createCommentMutation.mutate(newComment.trim());
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-25 10:51:52 +00:00
|
|
|
// Filter to show only top-level comments (no parent_id)
|
|
|
|
|
const topLevelComments =
|
|
|
|
|
commentsData?.comments?.filter((c: TrackComment) => !c.parent_id) || [];
|
2025-12-24 11:57:49 +00:00
|
|
|
const total = commentsData?.total || 0;
|
|
|
|
|
const totalPages = Math.ceil(total / limit);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="flex items-center gap-2">
|
|
|
|
|
<MessageCircle className="h-5 w-5" />
|
2025-12-25 10:51:52 +00:00
|
|
|
Commentaires ({commentsData?.total || 0})
|
2025-12-24 11:57:49 +00:00
|
|
|
</CardTitle>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className="space-y-4">
|
|
|
|
|
{/* Comment Form */}
|
|
|
|
|
{user ? (
|
|
|
|
|
<form onSubmit={handleSubmit} className="flex gap-2">
|
|
|
|
|
<Input
|
|
|
|
|
value={newComment}
|
|
|
|
|
onChange={(e) => setNewComment(e.target.value)}
|
2025-12-25 10:51:52 +00:00
|
|
|
placeholder="Écrire un commentaire..."
|
2025-12-24 11:57:49 +00:00
|
|
|
maxLength={500}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={!newComment.trim() || createCommentMutation.isPending}
|
|
|
|
|
>
|
2025-12-25 10:51:52 +00:00
|
|
|
{createCommentMutation.isPending ? (
|
|
|
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
|
|
|
) : (
|
|
|
|
|
<Send className="h-4 w-4" />
|
|
|
|
|
)}
|
2025-12-24 11:57:49 +00:00
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
) : (
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
2025-12-25 10:51:52 +00:00
|
|
|
Connectez-vous pour commenter
|
2025-12-24 11:57:49 +00:00
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Comments List */}
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<div className="flex justify-center py-8">
|
|
|
|
|
<LoadingSpinner />
|
|
|
|
|
</div>
|
|
|
|
|
) : error ? (
|
|
|
|
|
<div className="text-center text-destructive py-4">
|
|
|
|
|
Failed to load comments
|
|
|
|
|
</div>
|
2025-12-25 10:51:52 +00:00
|
|
|
) : topLevelComments.length === 0 ? (
|
2025-12-24 11:57:49 +00:00
|
|
|
<div className="text-center text-muted-foreground py-8">
|
2025-12-25 10:51:52 +00:00
|
|
|
Aucun commentaire pour le moment. Soyez le premier à commenter !
|
2025-12-24 11:57:49 +00:00
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="space-y-4">
|
2025-12-25 10:51:52 +00:00
|
|
|
{topLevelComments.map((comment: TrackComment) => (
|
|
|
|
|
<CommentThread
|
|
|
|
|
key={comment.id}
|
|
|
|
|
comment={comment}
|
|
|
|
|
trackId={trackId}
|
|
|
|
|
/>
|
2025-12-24 11:57:49 +00:00
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
{/* Pagination */}
|
|
|
|
|
{totalPages > 1 && (
|
|
|
|
|
<div className="flex items-center justify-center gap-2 pt-4">
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setPage((p) => Math.max(1, p - 1))}
|
|
|
|
|
disabled={page === 1}
|
|
|
|
|
>
|
2025-12-25 10:51:52 +00:00
|
|
|
Précédent
|
2025-12-24 11:57:49 +00:00
|
|
|
</Button>
|
|
|
|
|
<span className="text-sm text-muted-foreground">
|
2025-12-25 10:51:52 +00:00
|
|
|
Page {page} sur {totalPages}
|
2025-12-24 11:57:49 +00:00
|
|
|
</span>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setPage((p) => Math.min(totalPages, p + 1))}
|
|
|
|
|
disabled={page === totalPages}
|
|
|
|
|
>
|
2025-12-25 10:51:52 +00:00
|
|
|
Suivant
|
2025-12-24 11:57:49 +00:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|