Commit
·
2f91558
1
Parent(s):
676d35b
fix merge
Browse files
frontend/src/components/ui/chat.tsx
CHANGED
|
@@ -38,35 +38,11 @@ const Chat = React.forwardRef<HTMLDivElement, ChatProps>(
|
|
| 38 |
) => {
|
| 39 |
const messagesEndRef = React.useRef<HTMLDivElement>(null);
|
| 40 |
const messagesContainerRef = React.useRef<HTMLDivElement>(null);
|
| 41 |
-
const [userIsScrolling, setUserIsScrolling] = React.useState(false);
|
| 42 |
-
const scrollTimeoutRef = React.useRef<number>();
|
| 43 |
|
| 44 |
const scrollToBottom = () => {
|
| 45 |
-
if (messagesContainerRef.current && !userIsScrolling) {
|
| 46 |
-
messagesContainerRef.current.scrollTop =
|
| 47 |
-
messagesContainerRef.current.scrollHeight;
|
| 48 |
-
}
|
| 49 |
-
};
|
| 50 |
-
|
| 51 |
-
const handleScroll = () => {
|
| 52 |
if (messagesContainerRef.current) {
|
| 53 |
-
const
|
| 54 |
-
|
| 55 |
-
const isNearBottom = scrollHeight - scrollTop - clientHeight < 100;
|
| 56 |
-
|
| 57 |
-
// Only prevent auto-scroll if user is NOT near the bottom
|
| 58 |
-
setUserIsScrolling(!isNearBottom);
|
| 59 |
-
|
| 60 |
-
if (scrollTimeoutRef.current) {
|
| 61 |
-
clearTimeout(scrollTimeoutRef.current);
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
// Reset user scrolling state after a delay if they're not near bottom
|
| 65 |
-
if (!isNearBottom) {
|
| 66 |
-
scrollTimeoutRef.current = setTimeout(() => {
|
| 67 |
-
setUserIsScrolling(false);
|
| 68 |
-
}, 2000);
|
| 69 |
-
}
|
| 70 |
}
|
| 71 |
};
|
| 72 |
|
|
@@ -80,13 +56,13 @@ const Chat = React.forwardRef<HTMLDivElement, ChatProps>(
|
|
| 80 |
content: m.content.slice(0, 50) + "...",
|
| 81 |
}))
|
| 82 |
);
|
| 83 |
-
//
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
}, [messages
|
| 90 |
|
| 91 |
return (
|
| 92 |
<div
|
|
@@ -97,8 +73,7 @@ const Chat = React.forwardRef<HTMLDivElement, ChatProps>(
|
|
| 97 |
{/* Messages */}
|
| 98 |
<div
|
| 99 |
ref={messagesContainerRef}
|
| 100 |
-
|
| 101 |
-
className="flex-1 overflow-y-auto p-4 space-y-4 scroll-smooth"
|
| 102 |
>
|
| 103 |
{messages.length === 0 ? (
|
| 104 |
<div className="flex items-center justify-center h-full text-muted-foreground">
|
|
|
|
| 38 |
) => {
|
| 39 |
const messagesEndRef = React.useRef<HTMLDivElement>(null);
|
| 40 |
const messagesContainerRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
| 41 |
|
| 42 |
const scrollToBottom = () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if (messagesContainerRef.current) {
|
| 44 |
+
const element = messagesContainerRef.current;
|
| 45 |
+
element.scrollTop = element.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
};
|
| 48 |
|
|
|
|
| 56 |
content: m.content.slice(0, 50) + "...",
|
| 57 |
}))
|
| 58 |
);
|
| 59 |
+
// Always scroll to bottom when messages change
|
| 60 |
+
const timeoutId = setTimeout(() => {
|
| 61 |
+
scrollToBottom();
|
| 62 |
+
}, 50);
|
| 63 |
+
|
| 64 |
+
return () => clearTimeout(timeoutId);
|
| 65 |
+
}, [messages]);
|
| 66 |
|
| 67 |
return (
|
| 68 |
<div
|
|
|
|
| 73 |
{/* Messages */}
|
| 74 |
<div
|
| 75 |
ref={messagesContainerRef}
|
| 76 |
+
className="flex-1 overflow-y-auto p-4 space-y-4"
|
|
|
|
| 77 |
>
|
| 78 |
{messages.length === 0 ? (
|
| 79 |
<div className="flex items-center justify-center h-full text-muted-foreground">
|