diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx
index 46e8568..50fcaca 100644
--- a/ui/components/ChatWindow.tsx
+++ b/ui/components/ChatWindow.tsx
@@ -8,7 +8,7 @@ import EmptyChat from './EmptyChat';
export type Message = {
id: string;
- createdAt?: Date;
+ createdAt: Date;
content: string;
role: 'user' | 'assistant';
sources?: Document[];
@@ -65,6 +65,7 @@ const ChatWindow = () => {
content: message,
id: Math.random().toString(36).substring(7),
role: 'user',
+ createdAt: new Date(),
},
]);
@@ -81,6 +82,7 @@ const ChatWindow = () => {
id: data.messageId,
role: 'assistant',
sources: sources,
+ createdAt: new Date(),
},
]);
added = true;
@@ -97,6 +99,7 @@ const ChatWindow = () => {
id: data.messageId,
role: 'assistant',
sources: sources,
+ createdAt: new Date(),
},
]);
added = true;
@@ -151,7 +154,7 @@ const ChatWindow = () => {
{messages.length > 0 ? (
<>
-
+
{
+ const [title, setTitle] = useState('');
+ const [timeAgo, setTimeAgo] = useState('');
+
+ useEffect(() => {
+ if (messages.length > 0) {
+ const newTitle =
+ messages[0].content.length > 20
+ ? `${messages[0].content.substring(0, 20)}...`
+ : messages[0].content;
+ setTitle(newTitle);
+ const newTimeAgo = formatTimeDifference(
+ new Date(),
+ messages[0].createdAt,
+ );
+ setTimeAgo(newTimeAgo);
+ }
+ }, [messages]);
+
+ useEffect(() => {
+ const intervalId = setInterval(() => {
+ if (messages.length > 0) {
+ const newTimeAgo = formatTimeDifference(
+ new Date(),
+ messages[0].createdAt,
+ );
+ setTimeAgo(newTimeAgo);
+ }
+ }, 1000);
+
+ return () => clearInterval(intervalId);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
-const Navbar = () => {
return (
{
/>
-
15 minutes ago
+
{timeAgo} ago
- Blog on AI
+ {title}
twMerge(clsx(...classes));
+
+export const formatTimeDifference = (date1: Date, date2: Date): string => {
+ const diffInSeconds = Math.floor(
+ Math.abs(date2.getTime() - date1.getTime()) / 1000,
+ );
+
+ if (diffInSeconds < 60)
+ return `${diffInSeconds} second${diffInSeconds !== 1 ? 's' : ''}`;
+ else if (diffInSeconds < 3600)
+ return `${Math.floor(diffInSeconds / 60)} minute${Math.floor(diffInSeconds / 60) !== 1 ? 's' : ''}`;
+ else if (diffInSeconds < 86400)
+ return `${Math.floor(diffInSeconds / 3600)} hour${Math.floor(diffInSeconds / 3600) !== 1 ? 's' : ''}`;
+ else if (diffInSeconds < 31536000)
+ return `${Math.floor(diffInSeconds / 86400)} day${Math.floor(diffInSeconds / 86400) !== 1 ? 's' : ''}`;
+ else
+ return `${Math.floor(diffInSeconds / 31536000)} year${Math.floor(diffInSeconds / 31536000) !== 1 ? 's' : ''}`;
+};