SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.
JavaScript a2aagentic-aiai-agentsdevtoolsmcp-clientn8n-alternativeno-codeopen-sourceorchestrationsmythosvisual-builder
Refactor Chats Component for Improved Readability and Structure
Salman-Ahamed5a8703e509c8
diff --git a/packages/app/src/react/features/ai-chat/components/chats.tsx b/packages/app/src/react/features/ai-chat/components/chats.tsxindex f01ef40c..9f15bc33 100644--- a/packages/app/src/react/features/ai-chat/components/chats.tsx+++ b/packages/app/src/react/features/ai-chat/components/chats.tsx@@ -6,17 +6,14 @@ import { IChatMessage } from '@react/features/ai-chat/types/chat.types';import { AgentDetails } from '@src/react/shared/types/agent-data.types';import { FC, MutableRefObject, RefObject, useEffect, useMemo, useRef } from 'react';-/**- * Chats component properties- */interface IChatsProps {- agent: AgentDetails; // Agent details for avatar display- messages: IChatMessage[]; // Array of chat messages to display- handleScroll: () => void; // Scroll event handler- containerRef: RefObject<HTMLElement>; // Container ref for scroll control- smartScrollToBottom: (smooth?: boolean) => void; // Smart scroll to bottom function- handleFileDrop: (droppedFiles: File[]) => Promise<void>; // File drop handler for drag-and-drop uploads- shouldAutoScroll: boolean; // Whether auto-scroll is enabled (user is near bottom)+ agent: AgentDetails;+ messages: IChatMessage[];+ handleScroll: () => void;+ containerRef: RefObject<HTMLElement>;+ smartScrollToBottom: (smooth?: boolean) => void;+ handleFileDrop: (droppedFiles: File[]) => Promise<void>;+ shouldAutoScroll: boolean;}/**@@ -47,17 +44,6 @@ export const Chats: FC<IChatsProps> = (props) => {const { retryMessage } = useChatContext();const dropzoneRef = useDragAndDrop({ onDrop: handleFileDrop });- /**- * ULTRA-SMOOTH SCROLL OPTIMIZATION WITH USER CONTROL- *- * KEY IMPROVEMENTS:- * - Instant scroll during streaming (no smooth animation lag)- * - Aggressive 16ms throttle (60fps) for buttery smoothness- * - Respects user scroll position (shouldAutoScroll check)- * - User scrolls up >200px = auto-scroll disabled- * - User scrolls back to bottom = auto-scroll re-enabled- * - Uses requestAnimationFrame for perfect frame sync- */const lastScrolledIdRef = useRef<string | number | undefined>();const lastMessageLengthRef = useRef<number>(0);const lastScrollTimeRef = useRef<number>(0);@@ -79,37 +65,12 @@ export const Chats: FC<IChatsProps> = (props) => {lastScrolledIdRef.current = lastMessage.id;lastMessageLengthRef.current = messageLength;- /**- * ULTRA-SMOOTH SCROLL STRATEGY WITH USER CONTROL:- *- * 1. Check shouldAutoScroll FIRST- * - If user scrolled up >200px, shouldAutoScroll = false- * - Don't scroll if user is reading history- * - Result: User stays in control- *- * 2. During streaming: Use instant scrolling (no smooth animation)- * - Reason: Smooth animation causes lag with rapid updates- * - Result: Instant, responsive, no lag- *- * 3. Throttle at 16ms (60fps): Perfect for human perception- * - Reason: 60fps is maximum visible smoothness- * - Result: Buttery smooth, no excessive calls- *- * 4. Use RAF for frame-perfect timing- * - Reason: Syncs with browser paint cycle- * - Result: Smooth, no jank- */-- // CRITICAL: Check if user wants auto-scroll- // If user scrolled up >200px, shouldAutoScroll = false- // This respects user's intent to read history- if (!shouldAutoScroll) return; // Don't scroll if user is reading above+ if (!shouldAutoScroll) return;const now = performance.now();const timeSinceLastScroll = now - lastScrollTimeRef.current;- const shouldThrottle = !isNewMessage && timeSinceLastScroll < 16; // 60fps+ const shouldThrottle = !isNewMessage && timeSinceLastScroll < 16; // 60fps throttle- // Cancel pending RAFif (rafRef.current !== null) {cancelAnimationFrame(rafRef.current);rafRef.current = null;@@ -118,30 +79,25 @@ export const Chats: FC<IChatsProps> = (props) => {const performScroll = () => {lastScrollTimeRef.current = performance.now();- // CRITICAL: Use instant scroll during streaming for smoothness- // Smooth animation causes lag when content updates rapidlyif (containerRef?.current) {containerRef.current.scrollTo({top: containerRef.current.scrollHeight,- behavior: isGenerating ? 'auto' : 'smooth', // instant during streaming!+ behavior: isGenerating ? 'auto' : 'smooth',});}};if (shouldThrottle) {- // Throttle: Use RAF for next framerafRef.current = requestAnimationFrame(() => {performScroll();rafRef.current = null;});} else {- // Immediate: New message or throttle passedperformScroll();}}}- // Cleanupreturn () => {if (rafRef.current !== null) {cancelAnimationFrame(rafRef.current);@@ -153,10 +109,8 @@ export const Chats: FC<IChatsProps> = (props) => {const avatar = agent?.aiAgentSettings?.avatar;/**- * Group messages by turnId (Optimized)- * Uses incremental grouping for better performance with long conversations- * Messages with same turnId are grouped together- * Messages without turnId are treated as individual groups+ * Group messages by turnId. Messages with same turnId are grouped together.+ * User messages are always individual, AI messages with same turnId are grouped.*/const groupedMessages = useMemo(() => {const groups: Array<{@@ -165,29 +119,23 @@ export const Chats: FC<IChatsProps> = (props) => {isUserMessage: boolean;}> = [];- // Optimized: Early return for empty messagesif (messages.length === 0) return groups;- // Optimized: Process messages in a single passfor (let i = 0; i < messages.length; i++) {const message = messages[i];const turnId = message.turnId || null;const isUser = message.type === 'user';- // User messages are always individual (not grouped)if (isUser) {groups.push({ turnId, messages: [message], isUserMessage: true });continue;}- // For AI messages, check if we can add to existing groupconst lastGroup = groups[groups.length - 1];- // If last group has same turnId and is not a user message, add to itif (lastGroup && !lastGroup.isUserMessage && lastGroup.turnId === turnId && turnId !== null) {lastGroup.messages.push(message);} else {- // Otherwise create new groupgroups.push({ turnId, messages: [message], isUserMessage: false });}}@@ -213,7 +161,6 @@ export const Chats: FC<IChatsProps> = (props) => {const canRetry = lastMessageInGroup.type === 'error' && isLastGroup;const onRetryClick = canRetry ? retryMessage : undefined;- // User messages render individually (no grouping)if (group.isUserMessage) {const message = group.messages[0];@@ -228,7 +175,6 @@ export const Chats: FC<IChatsProps> = (props) => {);}- // AI messages with turnId render as a groupif (group.turnId && group.messages.length > 0) {return (<ChatsTurnGroup@@ -241,7 +187,6 @@ export const Chats: FC<IChatsProps> = (props) => {);}- // Fallback: Messages without turnId render individuallyreturn group.messages.map((message, messageIndex) => (<Chatkey={message.id || `${groupIndex}-${messageIndex}`}