Skip to content

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.

Enhance Chats Component with Improved Type Safety and Documentation

Salman-Ahamed74b09e75b708
diff --git a/packages/app/src/react/features/ai-chat/components/chats.tsx b/packages/app/src/react/features/ai-chat/components/chats.tsx
index c426047d..7a6804f2 100644
--- a/packages/app/src/react/features/ai-chat/components/chats.tsx
+++ b/packages/app/src/react/features/ai-chat/components/chats.tsx
@@ -1,22 +1,44 @@
+/**
+ * Chats Component
+ * Displays chat messages with auto-scroll, drag-and-drop, and retry functionality
+ */
/* eslint-disable no-unused-vars */
import { Chat } from '@react/features/ai-chat/components';
import { useChatContext } from '@react/features/ai-chat/contexts';
import { useDragAndDrop } from '@react/features/ai-chat/hooks';
import { AgentDetails } from '@src/react/shared/types/agent-data.types';
-import { IChatMessage } from '@src/react/shared/types/chat.types';
import { FC, MutableRefObject, RefObject, useEffect, useRef } from 'react';
+import { IChatMessage } from '../types/chat.types';
+/**
+ * Chats component properties
+ */
interface MessagesProps {
+ /** Agent details for avatar display */
agent: AgentDetails;
+
+ /** Array of chat messages to display */
messages: IChatMessage[];
+
+ /** Scroll event handler */
handleScroll: () => void;
+
+ /** Container ref for scroll control */
containerRef: RefObject<HTMLElement>;
+
+ /** Smart scroll to bottom function */
smartScrollToBottom: (smooth?: boolean) => void;
+
+ /** File drop handler for drag-and-drop uploads */
handleFileDrop: (droppedFiles: File[]) => Promise<void>;
}
/**
* Combines multiple refs into a single ref callback
+ * Useful for managing multiple refs on the same element
+ *
+ * @param refs - Array of refs to combine
+ * @returns Combined ref callback
*/
const combineRefs =
<T extends HTMLElement>(...refs: Array<RefObject<T> | MutableRefObject<T | null>>) =>
@@ -27,21 +49,31 @@ const combineRefs =
});
};
+/**
+ * Chats Component
+ * Renders chat messages with smart auto-scroll and drag-and-drop support
+ */
export const Chats: FC<MessagesProps> = (props) => {
const { agent, messages, containerRef, handleFileDrop, ...scroll } = props;
const { handleScroll, smartScrollToBottom } = scroll;
const ref = useRef<HTMLDivElement>(null);
- const { isRetrying, retryLastMessage } = useChatContext();
+ const { retryLastMessage } = useChatContext();
const dropzoneRef = useDragAndDrop({ onDrop: handleFileDrop });
- // Smart auto-scroll when new messages arrive (respects user scroll position)
- // But only for system messages, not user messages (user messages handle their own scroll)
+ /**
+ * Smart auto-scroll effect
+ * Auto-scrolls only for system messages (AI responses), not user messages
+ * This ensures better UX - user stays in place when they send a message
+ */
useEffect(() => {
const lastMessage = messages[messages.length - 1];
- // Only auto-scroll for system messages, not user messages
- // lastMessage.me === false means it's a system message
- if (lastMessage && !lastMessage.me) {
+ if (!lastMessage) return;
+
+ // ✅ Use type-based discrimination instead of 'me' boolean
+ const isSystemMessage = lastMessage.type === 'system';
+
+ if (isSystemMessage) {
smartScrollToBottom();
}
}, [messages, smartScrollToBottom]);
@@ -60,18 +92,18 @@ export const Chats: FC<MessagesProps> = (props) => {
onScroll={handleScroll}
className="w-full h-full max-w-4xl flex-1 pb-4 space-y-6 px-2.5"
>
- {messages.map((message, i) => {
- const isLast = i === messages.length - 1;
- const onRetryClick = message.isError && isLast ? retryLastMessage : undefined;
- const retry = isRetrying && isLast;
+ {messages.map((message, index) => {
+ const isLastMessage = index === messages.length - 1;
+
+ // ✅ Use type-based discrimination - Only enable retry for error messages that are last
+ const canRetry = message.type === 'error' && isLastMessage;
+ const onRetryClick = canRetry ? retryLastMessage : undefined;
return (
<Chat
- key={i}
+ key={index}
{...message}
avatar={avatar}
- isRetrying={retry}
- isError={message.isError}
onRetryClick={onRetryClick}
scrollToBottom={smartScrollToBottom}
/>