|
| 1 | +import { useState, useEffect, useRef } from 'react' |
| 2 | +import { XMarkIcon } from '@heroicons/react/24/outline' |
| 3 | +import { InfiniteScroll } from '@inertiajs/react' |
| 4 | +import { usePage } from '@inertiajs/react' |
| 5 | +import Drawer from './Drawer' |
| 6 | +import { ChatMessage, User } from '../types' |
| 7 | + |
| 8 | +interface ChatDrawerProps { |
| 9 | + open: boolean |
| 10 | + onClose: () => void |
| 11 | + currentUser: User |
| 12 | + onSend: (message: string) => void |
| 13 | +} |
| 14 | + |
| 15 | +export default function ChatDrawer({ open, onClose, onSend, currentUser }: ChatDrawerProps) { |
| 16 | + const { props } = usePage<{ chat_messages: ChatMessage[] }>() |
| 17 | + const chatMessages = props.chat_messages || [] |
| 18 | + |
| 19 | + const [messageText, setMessageText] = useState('') |
| 20 | + |
| 21 | + const isCurrentUser = (username: string) => username === currentUser.username |
| 22 | + const scrollContainerRef = useRef<HTMLDivElement | null>(null) |
| 23 | + |
| 24 | + |
| 25 | + const AUTO_SCROLL_BUFFER = 200 |
| 26 | + useEffect(() => { |
| 27 | + if (scrollContainerRef.current && chatMessages.length > 0) { |
| 28 | + const container = scrollContainerRef.current |
| 29 | + const isNearBottom = container.scrollHeight - container.scrollTop - container.clientHeight < AUTO_SCROLL_BUFFER |
| 30 | + |
| 31 | + if (isNearBottom) { |
| 32 | + container.scrollTo({ |
| 33 | + top: container.scrollHeight, |
| 34 | + behavior: 'smooth' |
| 35 | + }) |
| 36 | + } |
| 37 | + } |
| 38 | + }, [chatMessages.length, chatMessages]) |
| 39 | + |
| 40 | + const handleSendMessage = () => { |
| 41 | + if (!messageText.trim()) return |
| 42 | + |
| 43 | + onSend(messageText) |
| 44 | + setMessageText('') |
| 45 | + } |
| 46 | + |
| 47 | + const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 48 | + if (e.key === 'Enter' && !e.shiftKey) { |
| 49 | + e.preventDefault() |
| 50 | + handleSendMessage() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return ( |
| 55 | + <Drawer open={open} onClose={onClose}> |
| 56 | + <div className="flex items-center justify-between px-6 py-4 border-b border-gray-200"> |
| 57 | + <h2 className="text-lg font-semibold text-gray-900">Chat</h2> |
| 58 | + <button |
| 59 | + onClick={onClose} |
| 60 | + className="p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100" |
| 61 | + > |
| 62 | + <XMarkIcon className="h-5 w-5" /> |
| 63 | + </button> |
| 64 | + </div> |
| 65 | + |
| 66 | + <div className="overflow-y-scroll p-4 [overflow-anchor:auto]" ref={scrollContainerRef}> |
| 67 | + <InfiniteScroll |
| 68 | + data="chat_messages" |
| 69 | + reverse |
| 70 | + onlyNext |
| 71 | + preserveUrl |
| 72 | + autoScroll={true} |
| 73 | + buffer={200} |
| 74 | + className="flex flex-col-reverse" |
| 75 | + > |
| 76 | + {chatMessages.map((message) => { |
| 77 | + const isCurrentUserMessage = isCurrentUser(message.user.username) |
| 78 | + return ( |
| 79 | + <div |
| 80 | + key={message.id} |
| 81 | + className={`flex gap-3 mb-4 ${isCurrentUserMessage ? 'flex-row-reverse' : 'flex-row'}`} |
| 82 | + > |
| 83 | + {!isCurrentUserMessage && ( |
| 84 | + <div className="flex-shrink-0"> |
| 85 | + <div className="w-10 h-10 rounded-full bg-sky-600 flex items-center justify-center text-white text-sm font-medium"> |
| 86 | + {message.user.username.charAt(0).toUpperCase()} |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + )} |
| 90 | + |
| 91 | + <div className={`flex flex-col ${isCurrentUserMessage ? 'items-end' : 'items-start'} max-w-[90%]`}> |
| 92 | + <div |
| 93 | + className={`px-4 py-2 rounded-lg ${isCurrentUserMessage |
| 94 | + ? 'bg-sky-600 text-white' |
| 95 | + : 'bg-gray-200 text-gray-900' |
| 96 | + }`} |
| 97 | + > |
| 98 | + <p className="text-sm">{message.body}</p> |
| 99 | + </div> |
| 100 | + <span className="mt-1 text-xs text-gray-500"> |
| 101 | + {new Date(message.created_at).toLocaleString([], { |
| 102 | + month: 'short', |
| 103 | + day: 'numeric', |
| 104 | + hour: '2-digit', |
| 105 | + minute: '2-digit' |
| 106 | + })} |
| 107 | + </span> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ) |
| 111 | + })} |
| 112 | + </InfiniteScroll> |
| 113 | + </div> |
| 114 | + |
| 115 | + <div className="border-t border-gray-200 p-4"> |
| 116 | + <div className="flex gap-2"> |
| 117 | + <input |
| 118 | + type="text" |
| 119 | + value={messageText} |
| 120 | + onChange={(e) => setMessageText(e.target.value)} |
| 121 | + onKeyUp={handleKeyPress} |
| 122 | + placeholder="Type a message..." |
| 123 | + className="flex-1 px-4 py-2 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-1 focus:ring-sky-500 focus:border-sky-500" |
| 124 | + /> |
| 125 | + <button |
| 126 | + type="button" |
| 127 | + onClick={handleSendMessage} |
| 128 | + className="px-4 py-2 bg-sky-600 text-white rounded-md text-sm font-medium hover:bg-sky-700 focus:outline-none focus:ring-2 focus:ring-sky-500 focus:ring-offset-2" |
| 129 | + > |
| 130 | + Send |
| 131 | + </button> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </Drawer> |
| 135 | + ) |
| 136 | +} |
0 commit comments