|
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | + |
| 3 | +interface Message { |
| 4 | + text: string; |
| 5 | + sender: "user" | "bot"; |
| 6 | + timestamp: number; |
| 7 | +} |
| 8 | + |
| 9 | +const BOT_REPLIES = [ |
| 10 | + "Got it, thanks!", |
| 11 | + "Interesting, tell me more.", |
| 12 | + "That makes sense.", |
| 13 | + "I'll look into that.", |
| 14 | + "Thanks for letting me know!", |
| 15 | +]; |
| 16 | + |
| 17 | +const pickBotReply = () => BOT_REPLIES[Math.floor(Math.random() * BOT_REPLIES.length)]; |
| 18 | + |
| 19 | +const formatTime = (timestamp: number) => |
| 20 | + new Date(timestamp).toLocaleTimeString([], { |
| 21 | + hour: "2-digit", |
| 22 | + minute: "2-digit", |
| 23 | + }); |
| 24 | + |
| 25 | +const isMac = typeof navigator !== "undefined" && /Mac/.test(navigator.userAgent); |
| 26 | + |
| 27 | +export const App = () => { |
| 28 | + const [messages, setMessages] = useState<Message[]>([]); |
| 29 | + const [inputValue, setInputValue] = useState(""); |
| 30 | + const messagesEndRef = useRef<HTMLDivElement>(null); |
| 31 | + |
| 32 | + const addBotReply = () => { |
| 33 | + setTimeout(() => { |
| 34 | + setMessages((previous) => [ |
| 35 | + ...previous, |
| 36 | + { text: pickBotReply(), sender: "bot", timestamp: Date.now() }, |
| 37 | + ]); |
| 38 | + }, 800); |
| 39 | + }; |
| 40 | + |
| 41 | + const handleSendFromButton = () => { |
| 42 | + const trimmed = inputValue.trim(); |
| 43 | + if (!trimmed) return; |
| 44 | + |
| 45 | + setMessages((previous) => [ |
| 46 | + ...previous, |
| 47 | + { text: trimmed, sender: "user", timestamp: Date.now() }, |
| 48 | + ]); |
| 49 | + setInputValue(""); |
| 50 | + addBotReply(); |
| 51 | + }; |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 55 | + const modifierPressed = isMac ? event.metaKey : event.ctrlKey; |
| 56 | + if (event.key === "Enter" && modifierPressed) { |
| 57 | + event.preventDefault(); |
| 58 | + const trimmed = inputValue.trim(); |
| 59 | + if (!trimmed) return; |
| 60 | + |
| 61 | + setMessages((previous) => [ |
| 62 | + ...previous, |
| 63 | + { text: trimmed, sender: "user", timestamp: Date.now() }, |
| 64 | + ]); |
| 65 | + setInputValue(""); |
| 66 | + addBotReply(); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + window.addEventListener("keydown", handleKeyDown); |
| 71 | + return () => window.removeEventListener("keydown", handleKeyDown); |
| 72 | + }, []); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); |
| 76 | + }, [messages]); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="chat-container"> |
| 80 | + <div className="chat-header"> |
| 81 | + <h1>Chat</h1> |
| 82 | + <div className="shortcut-hint"> |
| 83 | + <kbd className="kbd">{isMac ? "⌘" : "Ctrl"}</kbd> |
| 84 | + <span>+</span> |
| 85 | + <kbd className="kbd">Enter</kbd> |
| 86 | + <span>to send</span> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div className="messages"> |
| 91 | + {messages.length === 0 && ( |
| 92 | + <div className="messages-empty">Send a message to start chatting</div> |
| 93 | + )} |
| 94 | + {messages.map((message, index) => ( |
| 95 | + <div |
| 96 | + key={index} |
| 97 | + className={`message ${message.sender === "user" ? "message-sent" : "message-received"}`} |
| 98 | + > |
| 99 | + {message.text} |
| 100 | + <div className="message-timestamp">{formatTime(message.timestamp)}</div> |
| 101 | + </div> |
| 102 | + ))} |
| 103 | + <div ref={messagesEndRef} /> |
| 104 | + </div> |
| 105 | + |
| 106 | + <div className="input-area"> |
| 107 | + <input |
| 108 | + value={inputValue} |
| 109 | + onChange={(event) => setInputValue(event.target.value)} |
| 110 | + onKeyDown={(event) => { |
| 111 | + if (event.key === "Enter" && !event.metaKey && !event.ctrlKey) { |
| 112 | + event.preventDefault(); |
| 113 | + handleSendFromButton(); |
| 114 | + } |
| 115 | + }} |
| 116 | + placeholder="Type a message..." |
| 117 | + /> |
| 118 | + <button |
| 119 | + className="send-button" |
| 120 | + onClick={handleSendFromButton} |
| 121 | + disabled={!inputValue.trim()} |
| 122 | + > |
| 123 | + Send |
| 124 | + </button> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
0 commit comments