"use client" import { useState, useEffect, useCallback } from "react" import { useAuth } from "@/lib/auth-context" import { Button } from "@/components/ui/button" import { Textarea } from "@/components/ui/textarea" import { Avatar, AvatarFallback } from "@/components/ui/avatar" import type { Comment } from "@/lib/types" interface TicketCommentsProps { ticketId: number } export function TicketComments({ ticketId }: TicketCommentsProps) { const [comments, setComments] = useState([]) const [newComment, setNewComment] = useState("") const [loading, setLoading] = useState(false) const { user, authHeader } = useAuth() const API_URL = process.env.NEXT_PUBLIC_API_URL + "/api" const fetchComments = useCallback(async () => { if (!authHeader) return try { const res = await fetch(`${API_URL}/tickets/${ticketId}/comments`, { headers: { "Authorization": authHeader } }) if (res.ok) { const data = await res.json() setComments(data) } } catch (e) { console.error(e) } }, [ticketId, authHeader]) useEffect(() => { if (authHeader) { fetchComments() } }, [fetchComments, authHeader]) const handlePost = async () => { if (!authHeader || !newComment.trim()) return setLoading(true) try { const res = await fetch(`${API_URL}/tickets/${ticketId}/comments`, { method: "POST", headers: { "Authorization": authHeader, "Content-Type": "application/json" }, body: JSON.stringify({ text: newComment }) }) if (res.ok) { setNewComment("") fetchComments() } } catch (e) { console.error(e) } finally { setLoading(false) } } return (

Comments

{comments.length === 0 &&

No comments yet.

} {comments.map((comment) => (
{comment.author.name ? comment.author.name.charAt(0) : '?'}
{comment.author.name} {new Date(comment.createdAt).toLocaleString()}

{comment.text}

))}