-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathmessage-list.tsx
More file actions
187 lines (165 loc) · 6.37 KB
/
message-list.tsx
File metadata and controls
187 lines (165 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"use client";
import React, {useLayoutEffect, useRef, useEffect, useCallback, useMemo, useState} from "react";
interface Message {
role: string;
content: string;
id: number;
}
// Draft messages are used to optmistically update the UI
// before the server responds.
interface DraftMessage extends Omit<Message, "id"> {
id?: number;
}
interface MessageListProps {
messages: (Message | DraftMessage)[];
}
interface ProcessedMessageProps {
messageContent: string;
index: number;
}
export default function MessageList({messages}: MessageListProps) {
const [scrollAreaRef, setScrollAreaRef] = useState<HTMLDivElement | null>(null);
// Track if user is at bottom - default to true for initial scroll
const isAtBottomRef = useRef(true);
const checkIfAtBottom = useCallback(() => {
if (!scrollAreaRef) return false;
const { scrollTop, scrollHeight, clientHeight } = scrollAreaRef;
return scrollTop + clientHeight >= scrollHeight - 10; // 10px tolerance
}, [scrollAreaRef]);
// Track Ctrl (Windows/Linux) or Cmd (Mac) key state
// This is so that underline is only visible when hover + cmd/ctrl
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.ctrlKey || e.metaKey) document.documentElement.classList.add('modifier-pressed');
};
const handleKeyUp = (e: KeyboardEvent) => {
if (!e.ctrlKey && !e.metaKey) document.documentElement.classList.remove('modifier-pressed');
};
window.addEventListener("keydown", handleKeyDown);
window.addEventListener("keyup", handleKeyUp);
return () => {
window.removeEventListener("keydown", handleKeyDown);
window.removeEventListener("keyup", handleKeyUp);
document.documentElement.classList.remove('modifier-pressed');
};
}, []);
// Track whether the user is scrolled to the bottom. Every scroll event
// updates the ref so auto-scroll decisions are always based on the
// user's actual position.
useEffect(() => {
if (!scrollAreaRef) return;
const handleScroll = () => {
isAtBottomRef.current = checkIfAtBottom();
};
handleScroll();
scrollAreaRef.addEventListener("scroll", handleScroll);
return () => scrollAreaRef.removeEventListener("scroll", handleScroll);
}, [checkIfAtBottom, scrollAreaRef]);
// Pin to bottom when new content arrives, but only if the user hasn't
// scrolled away. Always scroll when the latest message is from the user
// (they just sent it and should see it). Direct scrollTop assignment is
// synchronous and avoids the animation conflicts that smooth scrollTo
// causes during streaming.
useLayoutEffect(() => {
if (!scrollAreaRef) return;
const lastMessage = messages[messages.length - 1];
const isUserMessage = lastMessage && lastMessage.role === "user";
if (!isAtBottomRef.current && !isUserMessage) return;
scrollAreaRef.scrollTop = scrollAreaRef.scrollHeight;
isAtBottomRef.current = true;
}, [messages, scrollAreaRef]);
// If no messages, show a placeholder
if (messages.length === 0) {
return (
<div className="flex-1 p-6 flex items-center justify-center text-muted-foreground">
<p>No messages yet. Start the conversation!</p>
</div>
);
}
return (
<div className="overflow-y-auto flex-1" ref={setScrollAreaRef}>
<div
className="p-4 flex flex-col gap-4 max-w-4xl mx-auto min-h-0">
{messages.map((message, index) => (
<div
key={message.id ?? "draft"}
className={`${message.role === "user" ? "text-right" : ""}`}
>
<div
className={`inline-block rounded-lg ${
message.role === "user"
? "bg-accent-foreground rounded-lg max-w-[90%] px-4 py-3 text-accent"
: "max-w-[80ch]"
}`}
>
<div
className={`whitespace-pre-wrap break-words text-left text-xs md:text-sm leading-relaxed md:leading-normal ${
message.role === "user" ? "" : "font-mono"
}`}
>
{message.role !== "user" && message.content === "" ? (
<LoadingDots />
) : (
<ProcessedMessage
messageContent={message.content}
index={index}
/>
)}
</div>
</div>
</div>
))}
</div>
</div>
);
}
const LoadingDots = () => (
<div className="flex space-x-1">
<div
aria-hidden="true"
className={`size-2 rounded-full bg-foreground animate-pulse [animation-delay:0ms]`}
/>
<div
aria-hidden="true"
className={`size-2 rounded-full bg-foreground animate-pulse [animation-delay:300ms]`}
/>
<div
aria-hidden="true"
className={`size-2 rounded-full bg-foreground animate-pulse [animation-delay:600ms]`}
/>
<span className="sr-only">Loading...</span>
</div>
);
const ProcessedMessage = React.memo(function ProcessedMessage({
messageContent,
index,
}: ProcessedMessageProps) {
// Regex to find URLs
// https://stackoverflow.com/a/17773849
const urlRegex = useMemo<RegExp>(() => /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/, []);
const handleClick = (e: React.MouseEvent<HTMLAnchorElement>, url: string) => {
if (e.metaKey || e.ctrlKey) {
window.open(url, "_blank");
} else {
e.preventDefault(); // disable normal click to emulate terminal behaviour
}
}
const linkedContent = useMemo(() => {
return messageContent.split(urlRegex).map((content, idx) => {
if (urlRegex.test(content)) {
return (
<a
key={`${index}-${idx}`}
href={content}
onClick={(e) => handleClick(e, content)}
className="cursor-default [.modifier-pressed_&]:hover:underline [.modifier-pressed_&]:hover:cursor-pointer"
>
{content}
</a>
);
}
return <span key={`${index}-${idx}`}>{content}</span>;
});
}, [index, messageContent, urlRegex]);
return <>{linkedContent}</>;
});