import { useEffect, useState } from "react"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, } from "@/components/ui/card"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { Textarea } from "@/components/ui/textarea"; import { cn } from "@/lib/utils"; import { useAuth } from "@/contexts/AuthContext"; import { useToast } from "@/hooks/use-toast"; import { communityService } from "@/lib/supabase-service"; import { Heart, MessageCircle, Share2, Volume2, VolumeX } from "lucide-react"; import type { FeedItem } from "@/pages/Feed"; interface FeedItemCardProps { item: FeedItem; isFollowing: boolean; onToggleFollow: (authorId: string) => void; onShare: (postId: string) => void; onLike: (postId: string) => void; onComment: (postId: string) => void; } export function FeedItemCard({ item, isFollowing, onToggleFollow, onShare, onLike, onComment, }: FeedItemCardProps) { const [muted, setMuted] = useState(true); const [showComments, setShowComments] = useState(false); const [comments, setComments] = useState([]); const [loadingComments, setLoadingComments] = useState(false); const [commentText, setCommentText] = useState(""); const [submittingComment, setSubmittingComment] = useState(false); const { user } = useAuth(); const { toast } = useToast(); const hasMedia = item.mediaType !== "none" && Boolean(item.mediaUrl); useEffect(() => { if (!showComments) return; let cancelled = false; (async () => { setLoadingComments(true); try { const data = await communityService.listComments(item.id); if (!cancelled) setComments(Array.isArray(data) ? data : []); } finally { if (!cancelled) setLoadingComments(false); } })(); return () => { cancelled = true; }; }, [showComments, item.id]); const submitComment = async () => { if (!user?.id) { toast({ description: "Please sign in to comment." }); return; } const content = commentText.trim(); if (!content) return; setSubmittingComment(true); try { const created = await communityService.addComment( item.id, user.id, content, ); if (created) { setComments((prev) => [...prev, created]); setCommentText(""); onComment?.(item.id); } } catch (e) { toast({ variant: "destructive", description: "Failed to add comment" }); } finally { setSubmittingComment(false); } }; return (
{item.authorName?.[0]?.toUpperCase() || "U"}
{item.authorName} {/* Removed snippet to avoid duplicate text with full caption below */}
{hasMedia && (
{item.mediaType === "video" ? ( <>
)} {item.caption && (

{item.caption}

)}
{item.mediaType === "video" ? "Video" : item.mediaType === "image" ? "Image" : "Update"}
{showComments && (
{loadingComments ? (

Loading comments…

) : comments.length === 0 ? (

Be the first to comment.

) : ( comments.map((c) => (
{(c.user_profiles?.full_name || c.user_profiles?.username || "U")[0]?.toUpperCase() || "U"}
{c.user_profiles?.full_name || c.user_profiles?.username || "Member"}
{c.content}
)) )}