|
| 1 | +import React, { useState } from "react"; |
| 2 | + |
| 3 | +// Hàm tạo mã sự kiện ngẫu nhiên gồm 5 ký tự |
| 4 | +const generateEventCode = () => { |
| 5 | + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
| 6 | + let result = ''; |
| 7 | + for (let i = 0; i < 5; i++) { |
| 8 | + result += characters.charAt(Math.floor(Math.random() * characters.length)); |
| 9 | + } |
| 10 | + return result; |
| 11 | +}; |
| 12 | + |
| 13 | +const Livestream = () => { |
| 14 | + const [eventCode, setEventCode] = useState(generateEventCode()); |
| 15 | + const [message, setMessage] = useState(''); |
| 16 | + const [chatMessages, setChatMessages] = useState([]); |
| 17 | + |
| 18 | + // Hàm xử lý khi người dùng gửi tin nhắn |
| 19 | + const handleSendMessage = () => { |
| 20 | + if (message.trim()) { |
| 21 | + setChatMessages([...chatMessages, { text: message, timestamp: new Date().toLocaleTimeString() }]); |
| 22 | + setMessage(''); |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + return ( |
| 27 | + <div className="flex bg-[#f0f4f9] min-h-screen p-5"> |
| 28 | + <div className="w-2/3 p-4 bg-white rounded-lg shadow-lg"> |
| 29 | + <h1 className="text-2xl font-bold mb-4">Cài đặt sự kiện phát trực tiếp</h1> |
| 30 | + |
| 31 | + {/* Mã sự kiện phát trực tiếp */} |
| 32 | + <div className="mb-6"> |
| 33 | + <label className="block text-gray-700 font-semibold mb-2">Mã sự kiện phát trực tiếp:</label> |
| 34 | + <div className="flex items-center"> |
| 35 | + <input |
| 36 | + type="text" |
| 37 | + value={eventCode} |
| 38 | + readOnly |
| 39 | + className="border border-gray-300 rounded-lg p-2 text-center font-mono text-lg w-[120px] bg-gray-100 mr-4" |
| 40 | + /> |
| 41 | + <button |
| 42 | + onClick={() => setEventCode(generateEventCode())} |
| 43 | + className="px-4 py-2 bg-blue-500 text-white font-semibold rounded-lg hover:bg-blue-600" |
| 44 | + > |
| 45 | + Tạo mã mới |
| 46 | + </button> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + |
| 50 | + {/* Khung phát trực tiếp */} |
| 51 | + <div className="aspect-w-16 aspect-h-9 bg-black mb-4 rounded-lg"> |
| 52 | + {/* Khung video giả lập */} |
| 53 | + <p className="text-center text-white font-bold text-xl">Khung Video Trực Tiếp</p> |
| 54 | + </div> |
| 55 | + |
| 56 | + {/* Thông tin thêm */} |
| 57 | + <p className="text-gray-500 text-sm">Đang phát trực tiếp: Đây là khung video phát trực tiếp của bạn.</p> |
| 58 | + </div> |
| 59 | + |
| 60 | + {/* Thanh trò chuyện trực tiếp */} |
| 61 | + <div className="w-1/3 ml-4 p-4 bg-white rounded-lg shadow-lg flex flex-col"> |
| 62 | + <h2 className="text-xl font-bold mb-4">Trò chuyện trực tiếp</h2> |
| 63 | + |
| 64 | + {/* Hiển thị tin nhắn trò chuyện */} |
| 65 | + <div className="flex-grow overflow-y-auto mb-4 border rounded-lg p-2 bg-gray-50"> |
| 66 | + {chatMessages.length === 0 ? ( |
| 67 | + <p className="text-gray-500 text-center">Chưa có tin nhắn nào.</p> |
| 68 | + ) : ( |
| 69 | + chatMessages.map((msg, index) => ( |
| 70 | + <div key={index} className="mb-2"> |
| 71 | + <p className="text-sm"><span className="font-semibold">Người dùng:</span> {msg.text}</p> |
| 72 | + <p className="text-xs text-gray-400">{msg.timestamp}</p> |
| 73 | + </div> |
| 74 | + )) |
| 75 | + )} |
| 76 | + </div> |
| 77 | + |
| 78 | + {/* Nhập và gửi tin nhắn */} |
| 79 | + <div className="flex items-center"> |
| 80 | + <input |
| 81 | + type="text" |
| 82 | + value={message} |
| 83 | + onChange={(e) => setMessage(e.target.value)} |
| 84 | + placeholder="Nhập tin nhắn..." |
| 85 | + className="border rounded-lg p-2 flex-grow mr-2" |
| 86 | + /> |
| 87 | + <button |
| 88 | + onClick={handleSendMessage} |
| 89 | + className="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600" |
| 90 | + > |
| 91 | + Gửi |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export default Livestream; |
0 commit comments