diff --git a/apps/www/src/components/Chat.tsx b/apps/www/src/components/Chat.tsx index ebcb2c15..40a94076 100644 --- a/apps/www/src/components/Chat.tsx +++ b/apps/www/src/components/Chat.tsx @@ -49,6 +49,7 @@ const ChatMessage: React.FC<{ }; export const Chat = () => { + const messagesDivRef = React.useRef(null); const storage = !IS_SERVER ? window.localStorage : null; const [currentApiKey, setCurrentApiKey] = React.useState( storage?.getItem(CHAT_OPENAI_API_KEY) ?? "", @@ -75,6 +76,12 @@ export const Chat = () => { }, }); + const scrollToBottom = () => { + if (messagesDivRef.current) { + messagesDivRef.current.scrollTop = messagesDivRef.current.scrollHeight; + } + }; + const handleUpdateApiKey = (e: React.ChangeEvent) => { const newApiKey = e.target.value; setCurrentApiKey(newApiKey); @@ -84,6 +91,12 @@ export const Chat = () => { setSelectedChatGptModel(value); }; + const onSubmit = (e: React.FormEvent) => { + storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey); + scrollToBottom(); + handleSubmit(e); + }; + const messagesWithoutSystem = messages.slice(1); const reversedMessagesWithoutSystem = R.reverse(messagesWithoutSystem); return ( @@ -114,14 +127,14 @@ export const Chat = () => {
{ - storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey); - handleSubmit(e); - }} + onSubmit={onSubmit} className="flex flex-col flex-1" > {/* Col-reverse is used to enable automatic scrolling as content populates the div */} -
+
{/* This div takes up all the remaining space so the messages start at the top */}
{reversedMessagesWithoutSystem.map((message, index) => { @@ -173,7 +186,7 @@ export const Chat = () => { } if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); - handleSubmit(e as unknown as React.FormEvent); + onSubmit(e as unknown as React.FormEvent); } }} placeholder="Message ChatGPT"