From e4f26c8abc2736091e14716fd018394601c9f94a Mon Sep 17 00:00:00 2001 From: DanPGill Date: Mon, 27 May 2024 18:31:41 +0100 Subject: [PATCH] Chat automatic scroll (#222) --- apps/www/src/components/Chat.tsx | 86 ++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/apps/www/src/components/Chat.tsx b/apps/www/src/components/Chat.tsx index 5bf4aba4..87b156e6 100644 --- a/apps/www/src/components/Chat.tsx +++ b/apps/www/src/components/Chat.tsx @@ -3,25 +3,31 @@ import { Button } from "@/components/ui/Button"; import { Input } from "@/components/ui/Input"; import { Textarea } from "@/components/ui/Textarea"; import { Icons } from "@/icons"; +import { cn } from "@/lib/utils"; import { nanoid, type Message } from "ai"; import { useChat } from "ai/react"; import * as React from "react"; +import * as R from "remeda"; const ChatMessage: React.FC<{ message: Message; isStreamFinished: boolean; -}> = ({ message, isStreamFinished }) => { + className?: string; +}> = ({ message, isStreamFinished, className }) => { const { role, content } = message; return ( -
-
- {role === "user" ? ( - - ) : ( +
+ {role === "assistant" && ( +
+
+ )} +
-
+ > { }; const messagesWithoutSystem = messages.slice(1); + const reversedMessagesWithoutSystem = R.reverse(messagesWithoutSystem); return ( -
+
{
-
- {messagesWithoutSystem.map((message, index) => { - const isStreamFinished = - ["user", "system"].includes(message.role) || - index < messagesWithoutSystem.length - 1 || - !isLoading; - return ( -
- - {index != messagesWithoutSystem.length - 1 && ( -
- )} -
- ); - })} -
-
-
-
+ {reversedMessagesWithoutSystem.length != 0 && ( +
+ {/* Col-reverse is used to enable automatic scrolling as content populates the div */} +
+
+ {reversedMessagesWithoutSystem.map((message, index) => { + const { role } = message; + const isStreamFinished = + ["user", "system"].includes(role) || + index > reversedMessagesWithoutSystem.length - 1 || + !isLoading; + const isLastElement = index != 0; + + return ( +
+ + {isLastElement && ( +
+ )} +
+ ); + })} +
+
+ )} +
+
+