import React, { useState } from 'react' import { Plus, Edit2, Trash2, CheckCircle, Circle, Heart, Loader2 } from 'lucide-react' import { ForumPost, ForumTopic } from '@/proxy/forum/forum' import ReactQuill from 'react-quill' import 'react-quill/dist/quill.snow.css' import { useStoreState } from '@/store/store' interface PostManagementProps { posts: ForumPost[] topics: ForumTopic[] loading: boolean onCreatePost: (post: { topicId: string content: string parentPostId?: string tenantId?: string }) => Promise onUpdatePost: (id: string, post: Partial) => Promise onDeletePost: (id: string) => Promise onMarkPostAsAcceptedAnswer: (id: string) => Promise onUnmarkPostAsAcceptedAnswer: (id: string) => Promise } export function PostManagement({ posts, topics, loading, onCreatePost, onUpdatePost, onDeletePost, onMarkPostAsAcceptedAnswer, onUnmarkPostAsAcceptedAnswer, }: PostManagementProps) { const { tenant } = useStoreState((state) => state.auth) const [content, setContent] = useState('') const [showCreateForm, setShowCreateForm] = useState(false) const [editingPost, setEditingPost] = useState(null) const [formData, setFormData] = useState({ topicId: '', parentPostId: '', tenantId: '', isAcceptedAnswer: false, }) const [submitting, setSubmitting] = useState(false) const resetForm = () => { setFormData({ topicId: '', parentPostId: '', tenantId: '', isAcceptedAnswer: false, }) setShowCreateForm(false) setEditingPost(null) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (submitting || !content.trim()) return try { setSubmitting(true) const data = { ...formData, content: content.trim() } if (editingPost) { await onUpdatePost(editingPost.id, data) } else { await onCreatePost(data) } resetForm() } catch (error) { console.error('Error submitting form:', error) } finally { setSubmitting(false) } } const handleEdit = (post: ForumPost) => { setEditingPost(post) setFormData({ topicId: post.topicId, parentPostId: '', tenantId: tenant.tenantId ?? '', isAcceptedAnswer: post.isAcceptedAnswer, }) setContent(post.content) setShowCreateForm(true) } const handleToggleAcceptedAnswer = async (post: ForumPost) => { try { if (post.isAcceptedAnswer) { await onUnmarkPostAsAcceptedAnswer(post.id) } else { await onMarkPostAsAcceptedAnswer(post.id) } } catch (error) { console.error('Error toggling accepted answer:', error) } } const handleDelete = async (id: string) => { if (confirm('Are you sure you want to delete this post?')) { try { await onDeletePost(id) } catch (error) { console.error('Error deleting post:', error) } } } const getTopicTitle = (topicId: string) => { const topic = topics.find((t) => t.id === topicId) return topic ? topic.title : 'Unknown Topic' } const formatDate = (value: string | Date) => { const date = value instanceof Date ? value : new Date(value) if (isNaN(date.getTime())) return 'Invalid Date' return new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }).format(date) } return (

Post Management

{/* Create/Edit Form */} {showCreateForm && (

{editingPost ? 'Edit Post' : 'Create New Post'}

)} {/* Posts List */}

Posts ({posts.length})

{loading ? (

Loading posts...

) : (
{posts .sort( (a, b) => new Date(b.creationTime).getTime() - new Date(a.creationTime).getTime(), ) .map((post) => (

{post.authorName}

{post.isAcceptedAnswer && (
Accepted Answer
)}

Reply to:{' '} {getTopicTitle(post.topicId)}

{formatDate(post.creationTime)}
{post.likeCount} likes
))}
)}
) }