import React, { useState } from 'react' import { Plus, Edit2, Trash2, Lock, Unlock, Pin, PinOff, CheckCircle, Circle, Eye, Loader2, } from 'lucide-react' import { ForumCategory, ForumTopic } from '@/proxy/forum/forum' import { useStoreState } from '@/store/store' import { useLocalization } from '@/utils/hooks/useLocalization' interface TopicManagementProps { topics: ForumTopic[] categories: ForumCategory[] loading: boolean onCreateTopic: (topic: { title: string content: string categoryId: string isPinned?: boolean isLocked?: boolean tenantId?: string }) => Promise onUpdateTopic: (id: string, topic: Partial) => Promise onDeleteTopic: (id: string) => Promise onPinTopic: (id: string) => Promise onUnpinTopic: (id: string) => Promise onLockTopic: (id: string) => Promise onUnlockTopic: (id: string) => Promise onMarkTopicAsSolved: (id: string) => Promise onMarkTopicAsUnsolved: (id: string) => Promise } export function TopicManagement({ topics, categories, loading, onCreateTopic, onUpdateTopic, onDeleteTopic, onPinTopic, onUnpinTopic, onLockTopic, onUnlockTopic, onMarkTopicAsSolved, onMarkTopicAsUnsolved, }: TopicManagementProps) { const { translate } = useLocalization() const { tenant } = useStoreState((state) => state.auth) const [showCreateForm, setShowCreateForm] = useState(false) const [editingTopic, setEditingTopic] = useState(null) const [formData, setFormData] = useState({ title: '', content: '', categoryId: '', isPinned: false, isLocked: false, isSolved: false, tenantId: '', }) const [submitting, setSubmitting] = useState(false) const resetForm = () => { setFormData({ title: '', content: '', categoryId: '', isPinned: false, isLocked: false, isSolved: false, tenantId: '', }) setShowCreateForm(false) setEditingTopic(null) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (submitting) return try { setSubmitting(true) if (editingTopic) { await onUpdateTopic(editingTopic.id, formData) } else { await onCreateTopic(formData) } resetForm() } catch (error) { console.error('Error submitting form:', error) } finally { setSubmitting(false) } } const handleEdit = (topic: ForumTopic) => { setEditingTopic(topic) setFormData({ title: topic.title, content: topic.content, categoryId: topic.categoryId, isPinned: topic.isPinned, isLocked: topic.isLocked, isSolved: topic.isSolved, tenantId: tenant.tenantId ?? '', }) setShowCreateForm(true) } const handlePin = async (topic: ForumTopic) => { try { if (topic.isPinned) { await onUnpinTopic(topic.id) } else { await onPinTopic(topic.id) } } catch (error) { console.error('Error toggling pin:', error) } } const handleLock = async (topic: ForumTopic) => { try { if (topic.isLocked) { await onUnlockTopic(topic.id) } else { await onLockTopic(topic.id) } } catch (error) { console.error('Error toggling lock:', error) } } const handleSolved = async (topic: ForumTopic) => { try { if (topic.isSolved) { await onMarkTopicAsUnsolved(topic.id) } else { await onMarkTopicAsSolved(topic.id) } } catch (error) { console.error('Error toggling solved status:', error) } } const handleDelete = async (id: string) => { if ( confirm( 'Are you sure you want to delete this topic? This will also delete all posts in this topic.', ) ) { try { await onDeleteTopic(id) } catch (error) { console.error('Error deleting topic:', error) } } } const getCategoryName = (categoryId: string) => { const category = categories.find((c) => c.id === categoryId) return category ? category.name : 'Unknown Category' } 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 (

{translate('::App.Forum.TopicManagement.Title')}

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

{editingTopic ? translate('::App.Forum.TopicManagement.AddTopic') : translate('::App.Forum.TopicManagement.EditTopic')}

setFormData({ ...formData, title: e.target.value })} className="w-full border border-gray-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 focus:border-transparent" required />