diff --git a/client/src/components/ChatInput.tsx b/client/src/components/ChatInput.tsx index fbef502..04263e7 100644 --- a/client/src/components/ChatInput.tsx +++ b/client/src/components/ChatInput.tsx @@ -17,18 +17,13 @@ function ChatInput({ onSendMessage }: ChatInputProps) { return (
-
- setMessage(e.target.value)} - placeholder="Type a message..." - className="flex-1 px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" - /> - -
+ setMessage(e.target.value)} + placeholder="Message" + className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" + />
); } diff --git a/client/src/components/ChatMessage.tsx b/client/src/components/ChatMessage.tsx index 684308e..a20d3c3 100644 --- a/client/src/components/ChatMessage.tsx +++ b/client/src/components/ChatMessage.tsx @@ -16,9 +16,18 @@ function ChatMessageDisplay({ className={`flex items-center gap-2 text-sm text-gray-600 mb-1 ${isOwn ? "justify-end" : "justify-start"}`} > - {!isOwn && {message.author.name}} - {new Date(message.timestamp).toLocaleTimeString()} - {isOwn && {message.author.name}} + {!isOwn && ( + {message.author.name} + )} + + {new Date(message.timestamp).toLocaleTimeString([], { + hour: "2-digit", + minute: "2-digit", + })} + + {isOwn && ( + {message.author.name} + )}
-
+

Simple Chat

{children}
diff --git a/client/src/components/MessageDisplay.tsx b/client/src/components/MessageDisplay.tsx index e0a837d..2c1f459 100644 --- a/client/src/components/MessageDisplay.tsx +++ b/client/src/components/MessageDisplay.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect, useRef } from "react"; import { ServerMessage } from "../../../shared"; import { useChatState } from "../contexts/ChatContext"; import ChatMessage from "./ChatMessage"; @@ -6,6 +6,16 @@ import ChatMessage from "./ChatMessage"; function ChatMessages() { const { state } = useChatState(); const userId = state.currentUser?.userId; + const messagesEndRef = useRef(null); // ref to an empty div at the end to scroll to + + const scrollToBottom = () => { + messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); + }; + + // scroll to bottom when messages change + useEffect(() => { + scrollToBottom(); + }, [state.messages]); function renderMessage(message: ServerMessage) { switch (message.type) { @@ -40,7 +50,12 @@ function ChatMessages() { } } - return <>{state.messages.map(renderMessage)}; + return ( +
+ {state.messages.map(renderMessage)} +
+
+ ); } export default React.memo(ChatMessages);