'use client'; import { useState, useEffect } from 'react'; import Image from 'next/image'; import { getVideoComments, CommentData } from '../actions'; interface CommentsProps { videoId: string; } export default function Comments({ videoId }: CommentsProps) { const [comments, setComments] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(false); const [isExpanded, setIsExpanded] = useState(false); useEffect(() => { let isMounted = true; setIsLoading(true); setError(false); setIsExpanded(false); getVideoComments(videoId, 40) .then(data => { if (isMounted) { const topLevel = data.filter(c => !c.is_reply); setComments(topLevel); setIsLoading(false); } }) .catch(err => { if (isMounted) { console.error('Failed to load comments:', err); setError(true); setIsLoading(false); } }); return () => { isMounted = false; }; }, [videoId]); if (error) { return (
Comments are turned off or unavailable.
); } if (isLoading) { return (

Comments

{[...Array(3)].map((_, i) => (
))}
); } // Always render all comments; CSS handles mobile collapse via max-height return (
{/* Collapsed header for mobile - tappable to expand */} {!isExpanded && comments.length > 0 && (
setIsExpanded(true)} style={{ cursor: 'pointer', display: 'none', // Hidden on desktop, shown via CSS on mobile alignItems: 'center', justifyContent: 'space-between', padding: '12px 16px', backgroundColor: 'var(--yt-hover)', borderRadius: '12px', marginBottom: '16px' }} >
Comments {comments.length}
{comments[0] && ( {comments[0].text.slice(0, 60)}... )}
)} {/* Desktop: always show full title. Mobile: hidden when collapsed */}

{comments.length} Comments

{comments.map((c) => (
{c.author}
{c.author} {c.timestamp}
{c.likes > 0 && (
{c.likes}
)}
))}
{/* Show more / collapse toggle on mobile */} {comments.length > 2 && ( )} {comments.length === 0 && (
No comments found.
)}
); }