Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | 'use client'; /** * TicketDetail - Admin ticket detail view with management controls */ import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { SupportTicketWithRelations, SupportMessage, CannedResponse } from '@/types/support'; import { TICKET_STATUS_CONFIG, TICKET_PRIORITY_CONFIG, TICKET_CATEGORY_CONFIG } from '@/constants/support'; import { Icon } from '@/components/ui/icons'; export interface TicketDetailProps { /** Ticket ID */ ticketId: string; } interface Agent { id: string; name: string; } export default function TicketDetail({ ticketId }: TicketDetailProps) { const [ticket, setTicket] = useState<SupportTicketWithRelations | null>(null); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [agents, setAgents] = useState<Agent[]>([]); const [cannedResponses, setCannedResponses] = useState<CannedResponse[]>([]); const [newMessage, setNewMessage] = useState(''); const [isInternal, setIsInternal] = useState(false); const [sending, setSending] = useState(false); useEffect(() => { fetchTicket(); fetchAgents(); fetchCannedResponses(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [ticketId]); const fetchTicket = async () => { try { const response = await fetch(`/api/admin/support/tickets/${ticketId}`); if (!response.ok) throw new Error('Failed to fetch ticket'); const data = await response.json(); setTicket(data.data); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; const fetchAgents = async () => { try { const response = await fetch('/api/admin/support/agents'); const data = await response.json(); setAgents(data.agents || []); } catch { // Silent fail } }; const fetchCannedResponses = async () => { try { const response = await fetch('/api/admin/support/canned-responses'); const data = await response.json(); setCannedResponses(data.responses || []); } catch { // Silent fail } }; const handleUpdateTicket = async (field: string, value: string) => { if (!ticket) return; try { const response = await fetch(`/api/admin/support/tickets/${ticketId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ [field]: value })}); if (!response.ok) throw new Error('Failed to update ticket'); const data = await response.json(); setTicket(data.ticket); } catch (err) { setError(err instanceof Error ? err.message : 'Update failed'); } }; const handleAssign = async (agentId: string) => { try { const response = await fetch(`/api/admin/support/tickets/${ticketId}/assign`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ agentId: agentId || null })}); if (!response.ok) throw new Error('Failed to assign ticket'); await fetchTicket(); } catch (err) { setError(err instanceof Error ? err.message : 'Assignment failed'); } }; const handleSendMessage = async () => { if (!newMessage.trim()) return; setSending(true); try { const response = await fetch(`/api/admin/support/tickets/${ticketId}/messages`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: newMessage, isInternal})}); if (!response.ok) throw new Error('Failed to send message'); setNewMessage(''); setIsInternal(false); await fetchTicket(); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to send message'); } finally { setSending(false); } }; const handleInsertCannedResponse = (content: string) => { setNewMessage((prev) => prev + (prev ? '\n\n' : '') + content); }; if (loading) { return ( <div className="p-8 text-center"> <div className="animate-spin h-8 w-8 border-4 border-blue-600 border-t-transparent rounded-full mx-auto" /> </div> ); } if (error || !ticket) { return ( <div className="p-8 text-center text-red-600"> {error || 'Ticket not found'} </div> ); } const categoryConfig = TICKET_CATEGORY_CONFIG[ticket.category as keyof typeof TICKET_CATEGORY_CONFIG]; return ( <div className="space-y-6"> {/* Header */} <div className="flex items-center justify-between"> <div className="flex items-center gap-4"> <Link href="/admin/support/tickets" className="text-gray-500 hover:text-gray-700" > <Icon name="chevron-left" size={20} /> </Link> <div> <p className="text-sm text-gray-500">{ticket.ticketNumber}</p> <h1 className="text-2xl font-bold text-gray-900">{ticket.subject}</h1> </div> </div> </div> <div className="grid grid-cols-1 lg:grid-cols-3 gap-6"> {/* Main Content */} <div className="lg:col-span-2 space-y-6"> {/* Messages */} <div className="bg-white rounded-lg border border-gray-200"> <div className="p-4 border-b border-gray-200"> <h2 className="font-semibold text-gray-900">Conversation</h2> </div> <div className="p-4 space-y-4 max-h-[500px] overflow-y-auto"> {ticket.messages?.map((message: SupportMessage) => ( <div key={message.id} className={`p-4 rounded-lg ${ message.isInternal ? 'bg-yellow-50 border border-yellow-200' : message.senderType === 'CUSTOMER' ? 'bg-gray-50' : 'bg-blue-50' }`} > <div className="flex items-center justify-between mb-2"> <div className="flex items-center gap-2"> <span className="font-medium text-gray-900"> {message.senderType === 'CUSTOMER' ? ticket.customerName : message.senderType === 'BOT' ? 'Chatbot' : message.senderType === 'SYSTEM' ? 'System' : message.senderName || 'Agent'} </span> {message.isInternal && ( <span className="text-xs bg-yellow-200 text-yellow-800 px-2 py-0.5 rounded"> Internal Note </span> )} </div> <span className="text-xs text-gray-500"> {new Date(message.createdAt).toLocaleString()} </span> </div> <p className="text-gray-700 whitespace-pre-wrap">{message.content}</p> </div> ))} </div> {/* Reply Form */} <div className="p-4 border-t border-gray-200"> {/* Canned Responses */} {cannedResponses.length > 0 && ( <div className="mb-3"> <select onChange={(e) => { const response = cannedResponses.find((r) => r.id === e.target.value); if (response) { handleInsertCannedResponse(response.content); e.target.value = ''; } }} className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500" defaultValue="" > <option value="" disabled> Insert canned response... </option> {cannedResponses.map((response) => ( <option key={response.id} value={response.id}> {response.title} </option> ))} </select> </div> )} <textarea value={newMessage} onChange={(e) => setNewMessage(e.target.value)} placeholder="Type your reply..." rows={4} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none" /> <div className="flex items-center justify-between mt-3"> <label className="flex items-center gap-2 text-sm"> <input type="checkbox" checked={isInternal} onChange={(e) => setIsInternal(e.target.checked)} className="h-4 w-4 text-blue-600 rounded border-gray-300" /> <span className="text-gray-700">Internal note (not visible to customer)</span> </label> <button type="button" onClick={handleSendMessage} disabled={sending || !newMessage.trim()} className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed" > {sending ? 'Sending...' : isInternal ? 'Add Note' : 'Send Reply'} </button> </div> </div> </div> </div> {/* Sidebar */} <div className="space-y-6"> {/* Ticket Info */} <div className="bg-white rounded-lg border border-gray-200 p-4"> <h3 className="font-semibold text-gray-900 mb-4">Ticket Details</h3> <div className="space-y-4"> {/* Status */} <div> <label className="block text-sm font-medium text-gray-500 mb-1"> Status </label> <select value={ticket.status} onChange={(e) => handleUpdateTicket('status', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" > {Object.entries(TICKET_STATUS_CONFIG).map(([key, config]) => ( <option key={key} value={key}> {config.label} </option> ))} </select> </div> {/* Priority */} <div> <label className="block text-sm font-medium text-gray-500 mb-1"> Priority </label> <select value={ticket.priority} onChange={(e) => handleUpdateTicket('priority', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" > {Object.entries(TICKET_PRIORITY_CONFIG).map(([key, config]) => ( <option key={key} value={key}> {config.label} </option> ))} </select> </div> {/* Category */} <div> <label className="block text-sm font-medium text-gray-500 mb-1"> Category </label> <span className="inline-block px-2 py-1 text-sm rounded bg-gray-100 text-gray-700"> {categoryConfig?.label || ticket.category} </span> </div> {/* Assigned To */} <div> <label className="block text-sm font-medium text-gray-500 mb-1"> Assigned To </label> <select value={ticket.assignedTo?.id || ''} onChange={(e) => handleAssign(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" > <option value="">Unassigned</option> {agents.map((agent) => ( <option key={agent.id} value={agent.id}> {agent.name} </option> ))} </select> </div> {/* Created */} <div> <label className="block text-sm font-medium text-gray-500 mb-1"> Created </label> <span className="text-sm text-gray-700"> {new Date(ticket.createdAt).toLocaleString()} </span> </div> </div> </div> {/* Customer Info */} <div className="bg-white rounded-lg border border-gray-200 p-4"> <h3 className="font-semibold text-gray-900 mb-4">Customer</h3> <div className="space-y-2"> <p className="font-medium text-gray-900">{ticket.customerName}</p> <p className="text-sm text-gray-600">{ticket.customerEmail}</p> </div> </div> {/* Related Order */} {ticket.order && ( <div className="bg-white rounded-lg border border-gray-200 p-4"> <h3 className="font-semibold text-gray-900 mb-4">Related Order</h3> <Link href={`/admin/orders/${ticket.order.id}`} className="text-blue-600 hover:text-blue-700 font-medium" > Order #{ticket.order.id} </Link> </div> )} {/* History */} {ticket.history && ticket.history.length > 0 && ( <div className="bg-white rounded-lg border border-gray-200 p-4"> <h3 className="font-semibold text-gray-900 mb-4">History</h3> <div className="space-y-3 text-sm"> {ticket.history.slice(0, 5).map((entry, idx) => ( <div key={idx} className="flex items-start gap-2"> <div className="w-2 h-2 bg-gray-400 rounded-full mt-1.5" /> <div> <p className="text-gray-700"> {entry.action}: {entry.oldValue || 'none'} → {entry.newValue} </p> <p className="text-xs text-gray-500"> {new Date(entry.createdAt).toLocaleString()} </p> </div> </div> ))} </div> </div> )} </div> </div> </div> ); } |