Skip to content

Commit

Permalink
adding loading indicator when streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayush-Agnihotri committed Dec 27, 2024
1 parent 0ed6d99 commit fa35469
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/app/chat/[chatId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default function ChatPage() {
throw new Error(data.error || 'Failed to send message.');
}

setMessages((prevMessages) => [...prevMessages.slice(0, -1)]);
return response.body.getReader();
};

Expand Down Expand Up @@ -189,7 +190,15 @@ export default function ChatPage() {
sender: 'user',
};

setMessages((prevMessages) => [...prevMessages, userMessage]);
const assistantMessage: Message = {
id: `${messages.length + 1}`,
chatId: chatId,
content: ``,
timestamp: new Date().toLocaleString(),
sender: selectedModel,
};

setMessages((prevMessages) => [...prevMessages, userMessage, assistantMessage]);
setMessageStreaming(true);
sendStreamedMessage(message);
// displayToast();
Expand All @@ -205,7 +214,12 @@ export default function ChatPage() {

<div className="no-scrollbar m-auto mb-10 mt-5 flex w-4/5 flex-grow flex-col gap-3 overflow-y-scroll">
{messages.map((message, index) => (
<ChatMessage key={index} message={message} />
<ChatMessage
key={index}
lastMessage={index === messages.length - 1}
streaming={messageStreaming}
message={message}
/>
))}
<div ref={messagesEndRef}></div>
</div>
Expand Down
10 changes: 8 additions & 2 deletions src/app/components/chat/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { materialDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
import { useAuth } from '@/contexts/AuthContext';
import { useState } from 'react';
import Spinner from '../Spinner';

interface ChatMessageProps {
lastMessage: boolean;
streaming: boolean;
message: Message;
}

export default function ChatMessage({ message }: ChatMessageProps) {
export default function ChatMessage({ lastMessage, streaming, message }: ChatMessageProps) {
const { user } = useAuth();
const [copied, setCopied] = useState(false);

Expand Down Expand Up @@ -60,7 +63,10 @@ export default function ChatMessage({ message }: ChatMessageProps) {
className="h-10 w-10 flex-shrink-0 rounded-full object-cover"
/>
<div className="flex flex-col overflow-auto">
<span className="text-lg font-semibold">{getName()}</span>
<div className="flex gap-2 align-middle">
<span className="text-lg font-semibold">{getName()}</span>
{streaming && lastMessage && <Spinner width="1" height="1" />}
</div>
<div className="flex flex-row gap-1">
<span className="text-xs text-gray-400">{message.timestamp}</span>
{copied ? (
Expand Down

0 comments on commit fa35469

Please sign in to comment.