'use client'; import { Textarea } from '@/components/input'; import { Button } from '@/components/ui/button'; import { apiFetch } from '@/lib/api'; import { Loader2 } from 'lucide-react'; import Link from 'next/link'; import { useParams } from 'next/navigation'; import { useEffect, useState } from 'react'; interface Ticket { id: string; subject: string; status: 'awaiting_admin' | 'awaiting_user' | 'closed'; guestEmail: string | null; createdAt: string; lastMessageAt: string; } interface Message { id: string; authorIsAdmin: boolean; body: string; createdAt: string; } interface Detail { ticket: Ticket; userEmail: string | null; userName: string | null; messages: Message[]; } export default function AdminTicketDetail() { const params = useParams<{ id: string }>(); const [data, setData] = useState(null); const [reply, setReply] = useState(''); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); function load() { if (!params?.id) return; apiFetch(`/v1/admin/support/tickets/${params.id}`) .then(setData) .catch((e) => setError((e as Error).message)); } useEffect(load, [params?.id]); async function sendReply(e: React.FormEvent) { e.preventDefault(); if (!params?.id || reply.trim().length === 0) return; setBusy(true); setError(null); try { await apiFetch(`/v1/admin/support/tickets/${params.id}/messages`, { method: 'POST', body: JSON.stringify({ body: reply }), }); setReply(''); load(); } catch (err) { setError((err as Error).message); } finally { setBusy(false); } } async function setStatus(status: Ticket['status']) { if (!params?.id) return; setBusy(true); setError(null); try { await apiFetch(`/v1/admin/support/tickets/${params.id}/status`, { method: 'POST', body: JSON.stringify({ status }), }); load(); } catch (err) { setError((err as Error).message); } finally { setBusy(false); } } if (!data && !error) { return (
); } if (error || !data) { return (

{error ?? 'Ticket not found.'}

← Back to tickets
); } const { ticket, messages, userEmail, userName } = data; const fromLabel = userEmail ? `${userName ? `${userName} · ` : ''}${userEmail}` : ticket.guestEmail ? `${ticket.guestEmail} (guest)` : 'unknown'; return (
← All tickets

{ticket.subject}

From: {fromLabel}

{ticket.status.replace('_', ' ')}
{messages.map((m) => (
{m.authorIsAdmin ? 'Admin' : 'User'} {new Date(m.createdAt).toLocaleString()}

{m.body}

))}