|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React, { useState, useEffect } from 'react' |
| 4 | +import { useParams, useRouter } from 'next/navigation' |
| 5 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' |
| 6 | +import { Button } from '@/components/ui/button' |
| 7 | +import { Badge } from '@/components/ui/badge' |
| 8 | +import { Skeleton } from '@/components/ui/skeleton' |
| 9 | +import { ArrowLeft, MessageSquare, Clock, Calendar, Bug, Mail } from 'lucide-react' |
| 10 | +import { toast } from 'sonner' |
| 11 | +import Link from 'next/link' |
| 12 | + |
| 13 | +interface TicketReply { |
| 14 | + id: string |
| 15 | + admin_id: string |
| 16 | + message: string |
| 17 | + created_at: string |
| 18 | + admin?: { |
| 19 | + first_name?: string |
| 20 | + last_name?: string |
| 21 | + avatar_url?: string |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +interface SupportTicket { |
| 26 | + id: string |
| 27 | + subject: string |
| 28 | + message: string |
| 29 | + status: 'open' | 'in_progress' | 'resolved' | 'closed' |
| 30 | + created_at: string |
| 31 | + updated_at: string |
| 32 | + type: 'contact' | 'bug' |
| 33 | + replies?: TicketReply[] |
| 34 | +} |
| 35 | + |
| 36 | +export default function UserTicketDetailPage() { |
| 37 | + const params = useParams() |
| 38 | + const router = useRouter() |
| 39 | + const ticketId = params.id as string |
| 40 | + |
| 41 | + const [ticket, setTicket] = useState<SupportTicket | null>(null) |
| 42 | + const [loading, setLoading] = useState(true) |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (ticketId) { |
| 46 | + fetchTicket() |
| 47 | + } |
| 48 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 49 | + }, [ticketId]) |
| 50 | + |
| 51 | + const fetchTicket = async () => { |
| 52 | + try { |
| 53 | + const response = await fetch(`/api/support/tickets/${ticketId}`) |
| 54 | + if (response.ok) { |
| 55 | + const data = await response.json() |
| 56 | + setTicket(data.ticket) |
| 57 | + } else { |
| 58 | + toast.error('Failed to load ticket details') |
| 59 | + router.push('/protected/help') |
| 60 | + } |
| 61 | + } catch (error) { |
| 62 | + console.error('Error fetching ticket:', error) |
| 63 | + toast.error('Failed to load ticket details') |
| 64 | + } finally { |
| 65 | + setLoading(false) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const getStatusColor = (status: string) => { |
| 70 | + switch (status) { |
| 71 | + case 'open': return 'bg-red-500/10 text-red-400 border-red-500/20' |
| 72 | + case 'in_progress': return 'bg-yellow-500/10 text-yellow-400 border-yellow-500/20' |
| 73 | + case 'resolved': return 'bg-green-500/10 text-green-400 border-green-500/20' |
| 74 | + case 'closed': return 'bg-zinc-500/10 text-zinc-400 border-zinc-500/20' |
| 75 | + default: return 'bg-zinc-500/10 text-zinc-400 border-zinc-500/20' |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (loading) { |
| 80 | + return ( |
| 81 | + <div className="p-6 space-y-6"> |
| 82 | + <Skeleton className="h-10 w-64" /> |
| 83 | + <Skeleton className="h-96" /> |
| 84 | + </div> |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + if (!ticket) { |
| 89 | + return ( |
| 90 | + <div className="p-6"> |
| 91 | + <Card> |
| 92 | + <CardContent className="py-12 text-center"> |
| 93 | + <p className="text-muted-foreground">Ticket not found</p> |
| 94 | + <Button asChild className="mt-4"> |
| 95 | + <Link href="/protected/help">Back to Help Center</Link> |
| 96 | + </Button> |
| 97 | + </CardContent> |
| 98 | + </Card> |
| 99 | + </div> |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <div className="p-6 space-y-6"> |
| 105 | + <div className="flex items-center gap-4"> |
| 106 | + <Button variant="outline" size="icon" asChild> |
| 107 | + <Link href="/protected/help"> |
| 108 | + <ArrowLeft className="h-4 w-4" /> |
| 109 | + </Link> |
| 110 | + </Button> |
| 111 | + <div className="flex-1"> |
| 112 | + <h1 className="text-2xl font-bold">Ticket Details</h1> |
| 113 | + <p className="text-sm text-muted-foreground">ID: {ticket.id}</p> |
| 114 | + </div> |
| 115 | + <Badge className={getStatusColor(ticket.status)}> |
| 116 | + {ticket.status.replace('_', ' ')} |
| 117 | + </Badge> |
| 118 | + </div> |
| 119 | + |
| 120 | + <Card> |
| 121 | + <CardHeader> |
| 122 | + <div className="flex items-center gap-2"> |
| 123 | + {ticket.type === 'bug' ? ( |
| 124 | + <Bug className="h-5 w-5 text-red-500" /> |
| 125 | + ) : ( |
| 126 | + <Mail className="h-5 w-5 text-blue-500" /> |
| 127 | + )} |
| 128 | + <CardTitle>{ticket.subject}</CardTitle> |
| 129 | + </div> |
| 130 | + <CardDescription> |
| 131 | + {ticket.type === 'bug' ? 'Bug Report' : 'Support Request'} |
| 132 | + </CardDescription> |
| 133 | + </CardHeader> |
| 134 | + <CardContent> |
| 135 | + <div className="prose prose-sm max-w-none"> |
| 136 | + <p className="whitespace-pre-wrap">{ticket.message}</p> |
| 137 | + </div> |
| 138 | + <div className="flex items-center gap-4 text-xs text-zinc-400 mt-4"> |
| 139 | + <div className="flex items-center gap-1"> |
| 140 | + <Calendar className="h-3 w-3" /> |
| 141 | + <span>Created: {new Date(ticket.created_at).toLocaleString()}</span> |
| 142 | + </div> |
| 143 | + <div className="flex items-center gap-1"> |
| 144 | + <Clock className="h-3 w-3" /> |
| 145 | + <span>Last updated: {new Date(ticket.updated_at).toLocaleString()}</span> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + </CardContent> |
| 149 | + </Card> |
| 150 | + |
| 151 | + {ticket.replies && ticket.replies.length > 0 && ( |
| 152 | + <Card> |
| 153 | + <CardHeader> |
| 154 | + <CardTitle className="flex items-center gap-2"> |
| 155 | + <MessageSquare className="h-5 w-5 text-purple-500" /> |
| 156 | + Reply History |
| 157 | + </CardTitle> |
| 158 | + </CardHeader> |
| 159 | + <CardContent className="space-y-4"> |
| 160 | + {ticket.replies.map((reply) => ( |
| 161 | + <div key={reply.id} className="border-l-4 border-purple-500/30 bg-purple-500/5 rounded-r-lg p-4 space-y-2"> |
| 162 | + <div className="flex items-center gap-2"> |
| 163 | + <div className="h-8 w-8 rounded-full bg-gradient-to-br from-purple-500 to-blue-600 flex items-center justify-center text-white text-sm font-semibold"> |
| 164 | + {reply.admin?.first_name?.[0] || 'A'} |
| 165 | + </div> |
| 166 | + <div> |
| 167 | + <p className="text-sm font-medium"> |
| 168 | + {reply.admin?.first_name && reply.admin?.last_name |
| 169 | + ? `${reply.admin.first_name} ${reply.admin.last_name}` |
| 170 | + : 'Support Team'} |
| 171 | + </p> |
| 172 | + <p className="text-xs text-muted-foreground"> |
| 173 | + {new Date(reply.created_at).toLocaleString()} |
| 174 | + </p> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + <div className="pl-10"> |
| 178 | + <p className="text-sm text-foreground whitespace-pre-wrap"> |
| 179 | + {reply.message} |
| 180 | + </p> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + ))} |
| 184 | + </CardContent> |
| 185 | + </Card> |
| 186 | + )} |
| 187 | + </div> |
| 188 | + ) |
| 189 | +} |
0 commit comments