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'; interface TopicManagementProps { topics: ForumTopic[]; categories: ForumCategory[]; loading: boolean; onCreateTopic: (topic: { title: string; content: string; categoryId: string; isPinned?: boolean; isLocked?: boolean; }) => 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 [showCreateForm, setShowCreateForm] = useState(false); const [editingTopic, setEditingTopic] = useState(null); const [formData, setFormData] = useState({ title: '', content: '', categoryId: '', isPinned: false, isLocked: false, isSolved: false, }); const [submitting, setSubmitting] = useState(false); const resetForm = () => { setFormData({ title: '', content: '', categoryId: '', isPinned: false, isLocked: false, isSolved: false, }); 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, }); 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 = (date: Date) => { return new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: 'numeric', }).format(date); }; return (

Topic Management

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

{editingTopic ? 'Edit Topic' : 'Create New Topic'}

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 />