import React, { useState, useEffect, useRef } from 'react' import { createPortal } from 'react-dom' import { motion, AnimatePresence } from 'framer-motion' import { FaTimes, FaChevronLeft, FaChevronRight, FaMapMarkerAlt, FaCalendarAlt, FaUsers, FaExpand, FaCommentAlt, FaPaperPlane, FaHeart, } from 'react-icons/fa' import { Dialog } from '@/components/ui' import { useDialogContext } from '@/components/ui/Dialog/Dialog' import { EventCommentDto, EventDto } from '@/proxy/intranet/models' import useLocale from '@/utils/hooks/useLocale' import { currentLocalDate } from '@/utils/dateUtils' import Avatar from '@/components/ui/Avatar/Avatar' import { AVATAR_URL } from '@/constants/app.constant' import { intranetService } from '@/services/intranet.service' import dayjs from 'dayjs' import relativeTime from 'dayjs/plugin/relativeTime' import { useLocalization } from '@/utils/hooks/useLocalization' dayjs.extend(relativeTime) interface EventModalProps { event: EventDto onClose: () => void } const imgSrc = (img: string) => { if ( img.startsWith('data:') || img.startsWith('http://') || img.startsWith('https://') || img.startsWith('/') ) return img return `data:image/jpeg;base64,${img}` } function EventModalContent({ event, onClose }: EventModalProps) { const currentLocale = useLocale() const photos = (event.photos || '').split('|').filter(Boolean) const { translate } = useLocalization() const { isMaximized } = useDialogContext() // Photo slider state const [activePhoto, setActivePhoto] = useState(0) const [lightboxOpen, setLightboxOpen] = useState(false) const [lightboxIndex, setLightboxIndex] = useState(0) // Comments state const [comments, setComments] = useState(event.comments || []) const [commentText, setCommentText] = useState('') const [submitting, setSubmitting] = useState(false) const commentInputRef = useRef(null) const commentsEndRef = useRef(null) // Likes state const [likes, setLikes] = useState(event.likes) const [isLiked, setIsLiked] = useState(event.isLiked) const [liking, setLiking] = useState(false) useEffect(() => { // Refresh comments from API on open intranetService.getEventComments(event.id).then((res) => { if (res.data) setComments(res.data) }) }, [event.id]) const scrollToBottom = () => { commentsEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } const handleSubmitComment = async () => { if (!commentText.trim() || submitting) return setSubmitting(true) try { const res = await intranetService.createEventComment(event.id, commentText.trim()) if (res.data) { setComments((prev) => [...prev, res.data!]) setCommentText('') setTimeout(scrollToBottom, 100) } } finally { setSubmitting(false) } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() handleSubmitComment() } } const handleLike = async () => { if (liking) return setLiking(true) // Optimistic update setIsLiked((prev) => !prev) setLikes((prev) => (isLiked ? Math.max(0, prev - 1) : prev + 1)) try { const res = await intranetService.likeEvent(event.id) if (res.data) { setLikes(res.data.likes) setIsLiked(res.data.isLiked) } } catch { // Revert on error setIsLiked((prev) => !prev) setLikes((prev) => (isLiked ? prev + 1 : Math.max(0, prev - 1))) } finally { setLiking(false) } } const openLightbox = (idx: number) => { setLightboxIndex(idx) setLightboxOpen(true) } const closeLightbox = () => setLightboxOpen(false) const prevLightbox = (e: React.MouseEvent) => { e.stopPropagation() setLightboxIndex((i) => (i - 1 + photos.length) % photos.length) } const nextLightbox = (e: React.MouseEvent) => { e.stopPropagation() setLightboxIndex((i) => (i + 1) % photos.length) } return ( <> {/* Header */}

{event.name}

{currentLocalDate(event.date, currentLocale || 'tr')} {event.place && ( {event.place} )} {event.participantsCount > 0 && ( {event.participantsCount} )}
{event.user && (
{event.user.fullName}
)}
{/* Scrollable body */}
{/* Photo Gallery */} {photos.length > 0 && (
{/* Main photo */}
{`${event.name} openLightbox(activePhoto)} /> {photos.length > 1 && ( <> )}
{/* Thumbnail strip */} {photos.length > 1 && (
{photos.map((photo, idx) => ( ))}
)}
)} {/* Description */} {event.description && (

{event.description}

)} {/* Comments Section */}

{translate('::App.Intranet.Events.Comments')} ({comments.length})

{/* Comment List */}
{comments.length === 0 ? (

{translate('::App.Intranet.Events.NoComment')}

) : ( comments.map((comment) => (
{comment.user && ( )}

{comment.user?.fullName ?? 'Kullanıcı'}

{comment.content}

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

)) )}