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 | 'use client'; /** * MessageThread - Displays messages within a ticket with reply input */ import React, { useState, useRef, useEffect } from 'react'; import { SupportMessageWithAttachments, MessageSenderType } from '@/types/support'; import { clientLogger } from '@/lib/logging/clientLogger'; import { MESSAGE_SENDER_CONFIG } from '@/constants/support'; import { Icon } from '@/components/ui/icons'; export interface MessageThreadProps { /** List of messages to display */ messages: SupportMessageWithAttachments[]; /** Handler for sending new messages (undefined if replies disabled) */ onSendMessage?: (content: string) => Promise<void>; /** Whether a message is being sent */ isLoading?: boolean; } export default function MessageThread({ messages, onSendMessage, isLoading = false}: MessageThreadProps) { const [replyContent, setReplyContent] = useState(''); const [isSending, setIsSending] = useState(false); const messagesEndRef = useRef<HTMLDivElement>(null); // Scroll to bottom when messages change useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const content = replyContent.trim(); if (!content || !onSendMessage || isSending) return; setIsSending(true); try { await onSendMessage(content); setReplyContent(''); } catch (error) { clientLogger.error('Failed to send message', error instanceof Error ? error : new Error(String(error))); } finally { setIsSending(false); } }; const formatTime = (date: Date) => { return new Date(date).toLocaleString('en-US', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit'}); }; return ( <div className="flex flex-col h-[500px]"> {/* Messages List */} <div className="flex-1 overflow-y-auto p-4 space-y-4"> {messages.length === 0 ? ( <div className="text-center text-gray-500 py-8"> No messages yet. Our team will respond shortly. </div> ) : ( messages.map((message) => { const senderConfig = MESSAGE_SENDER_CONFIG[message.senderType]; const isCustomer = message.senderType === MessageSenderType.CUSTOMER; return ( <div key={message.id} className={`flex ${isCustomer ? 'justify-end' : 'justify-start'}`} > <div className={` max-w-[80%] rounded-lg p-4 ${isCustomer ? 'bg-blue-600 text-white' : senderConfig.bgColor} `} > {/* Sender Info */} <div className={` flex items-center gap-2 mb-2 text-sm ${isCustomer ? 'text-blue-200' : 'text-gray-500'} `} > <span className="font-medium">{message.senderName}</span> {!isCustomer && ( <span className={` px-1.5 py-0.5 text-xs rounded ${senderConfig.bgColor} ${senderConfig.color} `} > {senderConfig.label} </span> )} </div> {/* Content */} <p className={` whitespace-pre-wrap ${isCustomer ? 'text-white' : 'text-gray-800'} `} > {message.content} </p> {/* Attachments */} {message.attachments && message.attachments.length > 0 && ( <div className="mt-3 space-y-2"> {message.attachments.map((attachment) => ( <a key={attachment.id} href={attachment.fileUrl} target="_blank" rel="noopener noreferrer" className={` inline-flex items-center gap-2 px-3 py-1.5 rounded ${ isCustomer ? 'bg-blue-500 hover:bg-blue-400 text-white' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' } text-sm transition-colors `} > <Icon name="paperclip" size={16} /> {attachment.fileName} </a> ))} </div> )} {/* Timestamp */} <p className={` text-xs mt-2 ${isCustomer ? 'text-blue-200' : 'text-gray-400'} `} > {formatTime(message.createdAt)} </p> </div> </div> ); }) )} <div ref={messagesEndRef} /> </div> {/* Reply Input */} {onSendMessage && ( <form onSubmit={handleSubmit} className="border-t border-gray-200 p-4"> <div className="flex gap-3"> <textarea value={replyContent} onChange={(e) => setReplyContent(e.target.value)} placeholder="Type your reply..." rows={3} className=" flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none " disabled={isSending || isLoading} /> <button type="submit" disabled={!replyContent.trim() || isSending || isLoading} className=" px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors disabled:bg-gray-300 disabled:cursor-not-allowed self-end " > {isSending ? 'Sending...' : 'Send'} </button> </div> </form> )} </div> ); } |