import React, { useState, useRef } from 'react' import { motion, AnimatePresence } from 'framer-motion' import classNames from 'classnames' import EmojiPicker, { EmojiClickData } from 'emoji-picker-react' import { FaChartBar, FaSmile, FaTimes, FaImages, FaMapMarkerAlt } from 'react-icons/fa' import MediaManager from './MediaManager' import LocationPicker from './LocationPicker' import { SocialMedia } from '@/types/intranet' interface CreatePostProps { onCreatePost: (post: { content: string location?: string media?: { type: 'mixed' | 'poll' mediaItems?: SocialMedia[] poll?: { question: string options: Array<{ text: string }> } } }) => void } const CreatePost: React.FC = ({ onCreatePost }) => { const [content, setContent] = useState('') const [mediaType, setMediaType] = useState<'media' | 'poll' | null>(null) const [mediaItems, setMediaItems] = useState([]) const [location, setLocation] = useState(null) const [pollQuestion, setPollQuestion] = useState('') const [pollOptions, setPollOptions] = useState(['', '']) const [isExpanded, setIsExpanded] = useState(false) const [showEmojiPicker, setShowEmojiPicker] = useState(false) const [showMediaManager, setShowMediaManager] = useState(false) const [showLocationPicker, setShowLocationPicker] = useState(false) const textareaRef = useRef(null) const emojiPickerRef = useRef(null) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!content.trim() && mediaItems.length === 0 && !mediaType) return let media = undefined if (mediaType === 'media' && mediaItems.length > 0) { media = { type: 'mixed' as const, mediaItems } } else if (mediaType === 'poll' && pollQuestion && pollOptions.filter(o => o.trim()).length >= 2) { media = { type: 'poll' as const, poll: { question: pollQuestion, options: pollOptions.filter(o => o.trim()).map(text => ({ text })) } } } onCreatePost({ content, media, location: location || undefined }) // Reset form setContent('') setMediaType(null) setMediaItems([]) setLocation(null) setPollQuestion('') setPollOptions(['', '']) setIsExpanded(false) setShowEmojiPicker(false) } const handleEmojiClick = (emojiData: EmojiClickData) => { const emoji = emojiData.emoji const textarea = textareaRef.current if (!textarea) return const start = textarea.selectionStart const end = textarea.selectionEnd const text = content const before = text.substring(0, start) const after = text.substring(end) setContent(before + emoji + after) // Set cursor position after emoji setTimeout(() => { textarea.selectionStart = textarea.selectionEnd = start + emoji.length textarea.focus() }, 0) } const addPollOption = () => { if (pollOptions.length < 6) { setPollOptions([...pollOptions, '']) } } const removePollOption = (index: number) => { if (pollOptions.length > 2) { setPollOptions(pollOptions.filter((_, i) => i !== index)) } } const updatePollOption = (index: number, value: string) => { const newOptions = [...pollOptions] newOptions[index] = value setPollOptions(newOptions) } const clearMedia = () => { setMediaType(null) setMediaItems([]) setPollQuestion('') setPollOptions(['', '']) } const removeMediaItem = (id: string | undefined) => { if (!id) return setMediaItems(mediaItems.filter((m) => m.id !== id)) } // Close emoji picker when clicking outside React.useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (emojiPickerRef.current && !emojiPickerRef.current.contains(event.target as Node)) { setShowEmojiPicker(false) } } if (showEmojiPicker) { document.addEventListener('mousedown', handleClickOutside) } return () => { document.removeEventListener('mousedown', handleClickOutside) } }, [showEmojiPicker]) return (
{/* Text Input */}
Your avatar