import React, { useEffect, useState } from 'react' import { MessageCircle, Trash2, Send } from 'lucide-react' import axios from 'axios' import { useAuthStore } from '../../store/authStore' import { useNotificationStore } from '../../store/notificationStore' import { useUIStore } from '../../store/uiStore' import { Avatar } from '../ui/Avatar' import { Button } from '../ui/Button' const API = 'https://localhost:7125/api' function getAuthHeaders() { try { const token = JSON.parse(localStorage.getItem('chapterx-auth') || '{}')?.state?.token return token ? { Authorization: `Bearer ${token}` } : {} } catch { return {} } } function timeAgo(str: string): string { const diff = Date.now() - new Date(str).getTime() const m = Math.floor(diff / 60000) if (m < 1) return 'just now' if (m < 60) return `${m}m ago` const h = Math.floor(m / 60) if (h < 24) return `${h}h ago` return `${Math.floor(h / 24)}d ago` } interface BackendComment { id: number content: string userId: number storyId: number username: string createdAt: string } interface CommentSectionProps { storyId: number authorUserId: number onCountChange?: (count: number) => void } export const CommentSection: React.FC = ({ storyId, authorUserId, onCountChange }) => { const { currentUser } = useAuthStore() const { addNotification } = useNotificationStore() const { addToast } = useUIStore() const [comments, setComments] = useState([]) const [text, setText] = useState('') const [submitting, setSubmitting] = useState(false) useEffect(() => { axios.get(`${API}/comments/story/${storyId}`) .then(res => { const data = res.data ?? [] setComments(data) onCountChange?.(data.length) }) .catch(() => {}) }, [storyId]) const handleSubmit = async () => { if (!text.trim() || !currentUser) return setSubmitting(true) try { const res = await axios.post(`${API}/comments`, { content: text.trim(), userId: currentUser.user_id, storyId, }, { headers: getAuthHeaders() }) const newComment: BackendComment = { id: res.data?.id ?? Date.now(), content: text.trim(), userId: currentUser.user_id, storyId, username: currentUser.username, createdAt: new Date().toISOString(), } setComments(prev => { const next = [newComment, ...prev]; onCountChange?.(next.length); return next }) if (currentUser.user_id !== authorUserId) { await addNotification({ recipientUserId: authorUserId, type: 'comment', content: `${currentUser.username} commented: "${text.trim().slice(0, 60)}${text.length > 60 ? '...' : ''}"`, link: `/story/${storyId}`, }) } setText('') addToast('Comment posted!') } catch { addToast('Failed to post comment.', 'error') } setSubmitting(false) } const handleDelete = async (commentId: number) => { try { await axios.delete(`${API}/comments/${commentId}`, { headers: getAuthHeaders() }) setComments(prev => { const next = prev.filter(c => c.id !== commentId); onCountChange?.(next.length); return next }) addToast('Comment deleted', 'info') } catch { addToast('Failed to delete comment.', 'error') } } return (

Comments ({comments.length})

{currentUser ? (