Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions resources/js/components/conversation.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StreamingIndicator from '@/components/streaming-indicator';
import { cn } from '@/lib/utils';
import { useEffect, useRef } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';

type Message = {
id?: number;
Expand All @@ -20,11 +20,34 @@ export default function Conversation({ messages, streamingData, isStreaming, str
const scrollRef = useRef<HTMLDivElement>(null);

// Auto-scroll to bottom when messages change or during streaming
const [shouldAutoScroll, setShouldAutoScroll] = useState(true);

const checkIfAtBottom = useCallback(() => {
const container = scrollRef.current;
if (!container) return false;

const offset = Math.abs(container.scrollHeight - container.scrollTop - container.clientHeight);
return offset <= 1; // Allow a small margin of error
}, []);

const handleScroll = useCallback(() => {
const nearBottom = checkIfAtBottom();
setShouldAutoScroll(nearBottom);
}, [checkIfAtBottom]);

useEffect(() => {
if (scrollRef.current) {
if (scrollRef.current && shouldAutoScroll) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}, [messages.length, streamingData]);
}, [messages.length, streamingData, shouldAutoScroll]);

useEffect(() => {
const container = scrollRef.current;
if (container) {
container.addEventListener('scroll', handleScroll);
return () => container.removeEventListener('scroll', handleScroll);
}
}, [handleScroll]);

return (
<div ref={scrollRef} className="flex-1 overflow-x-hidden overflow-y-auto">
Expand Down
Loading