import React, { useState, useRef, useEffect } from 'react' import { motion, AnimatePresence } from 'framer-motion' import classNames from 'classnames' import dayjs from 'dayjs' import relativeTime from 'dayjs/plugin/relativeTime' import 'dayjs/locale/tr' import { FaHeart, FaRegHeart, FaRegCommentAlt, FaTrash, FaPaperPlane, } from 'react-icons/fa' import MediaLightbox from './MediaLightbox' import LocationMap from './LocationMap' import UserProfileCard from './UserProfileCard' import { SocialPostDto } from '@/proxy/intranet/models' dayjs.extend(relativeTime) dayjs.locale('tr') interface PostItemProps { post: SocialPostDto onLike: (postId: string) => void onComment: (postId: string, content: string) => void onDelete: (postId: string) => void onVote: (postId: string, optionId: string) => void } const PostItem: React.FC = ({ post, onLike, onComment, onDelete, onVote }) => { const [showComments, setShowComments] = useState(false) const [commentText, setCommentText] = useState('') const [showAllImages, setShowAllImages] = useState(false) const [lightboxOpen, setLightboxOpen] = useState(false) const [lightboxIndex, setLightboxIndex] = useState(0) const [showUserCard, setShowUserCard] = useState(false) const [hoveredCommentAuthor, setHoveredCommentAuthor] = useState(null) const videoRef = useRef(null) // Intersection Observer for video autoplay/pause useEffect(() => { const video = videoRef.current if (!video) return const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { // Video ekranda görünür - oynat video.play().catch(err => { console.log('Video autoplay failed:', err) }) } else { // Video ekrandan çıktı - durdur video.pause() } }) }, { threshold: 0.5 // Video %50 görünür olduğunda oynat } ) observer.observe(video) return () => { observer.disconnect() } }, [post.media?.type]) const handleSubmitComment = (e: React.FormEvent) => { e.preventDefault() if (commentText.trim()) { onComment(post.id, commentText) setCommentText('') } } const getImageLayout = (images: string[]) => { const count = images.length if (count === 1) return 'single' if (count === 2) return 'double' if (count === 3) return 'triple' return 'multiple' } const renderMedia = () => { if (!post.media) return null switch (post.media.type) { case 'image': if (post.media.urls && post.media.urls.length > 0) { const layout = getImageLayout(post.media.urls) const displayImages = showAllImages ? post.media.urls : post.media.urls.slice(0, 4) const hasMore = post.media.urls.length > 4 return ( <>
{displayImages.map((url, index) => (
{`Post { setLightboxIndex(index) setLightboxOpen(true) }} /> {hasMore && index === 3 && !showAllImages && post.media?.urls && (
{ e.stopPropagation() setShowAllImages(true) }} > +{post.media.urls.length - 4}
)}
))}
setLightboxOpen(false)} media={{ type: 'image', urls: post.media.urls }} startIndex={lightboxIndex} /> ) } break case 'video': if (post.media.urls && post.media.urls.length > 0) { return ( <>
setLightboxOpen(true)} >
setLightboxOpen(false)} media={{ type: 'video', urls: [post.media.urls[0]] }} /> ) } break case 'poll': if (post.media.pollQuestion && post.media.pollOptions) { const isExpired = post.media.pollEndsAt ? new Date() > post.media.pollEndsAt : false const hasVoted = !!post.media.pollUserVoteId const totalVotes = post.media.pollTotalVotes || 0 const pollUserVoteId = post.media.pollUserVoteId return (

{post.media.pollQuestion}

{post.media.pollOptions.map((option) => { const percentage = totalVotes > 0 ? (option.votes / totalVotes) * 100 : 0 const isSelected = pollUserVoteId === option.id return ( ) })}
{totalVotes} oy • {isExpired ? 'Sona erdi' : post.media.pollEndsAt ? dayjs(post.media.pollEndsAt).fromNow() + ' bitiyor' : ''}
) } break } return null } return ( {/* Header */}
setShowUserCard(true)} onMouseLeave={() => setShowUserCard(false)} > {post.employee.fullName} {showUserCard && ( )}

{post.employee.fullName}

{post.employee.jobPosition?.name || 'Çalışan'} • {dayjs(post.creationTime).fromNow()}

{post.isOwnPost && ( )}
{/* Content */}

{post.content}

{renderMedia()} {/* Location */} {post.locationJson && (
)}
{/* Actions */}
{/* Comments Section */} {showComments && ( {/* Comment Form */}
setCommentText(e.target.value)} placeholder="Yorum yazın..." className="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-full bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Comments List */}
{post.comments.map((comment) => (
setHoveredCommentAuthor(comment.id)} onMouseLeave={() => setHoveredCommentAuthor(null)} > {comment.creator.fullName} {hoveredCommentAuthor === comment.id && ( )}

{comment.creator.fullName}

{comment.content}

{dayjs(comment.creationTime).fromNow()}

))}
)}
) } export default PostItem