Here's a chat page with the 4 components you requested, built with ShadCN and following SOLID, DRY, and KISS principles:
tsx Copy code
// ChatPage.tsx
import React, { useEffect, useState, useRef } from 'react';
import { AppSidebar } from './components/AppSidebar';
import { ChatHeader } from './components/ChatHeader';
import { MessageContainer } from './components/MessageContainer';
import { ChatFooter } from './components/ChatFooter';
import { useSSE } from './hooks/useSSE';
import { Message, FileAttachment } from './types';
const ChatPage: React.FC = () => {
const [messages, setMessages] = useState<Message[]>([]);
const [isLoading, setIsLoading] = useState(false);
const { startSSEConnection, stopSSEConnection } = useSSE({
onMessage: (content) => {
setMessages(prev => [...prev, { role: 'assistant', content, timestamp: new Date() }]);
setIsLoading(false);
}
});
const sendMessage = async (content: string, attachments: FileAttachment[] = []) => {
// Add user message to the chat
const userMessage: Message = { role: 'user', content, attachments, timestamp: new Date() };
setMessages(prev => [...prev, userMessage]);
setIsLoading(true);
try {
// Start SSE connection to receive streaming response
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: content, attachments }),
});
if (response.ok) {
const { conversationId } = await response.json();
startSSEConnection(`/api/chat/stream/${conversationId}`);
} else {
throw new Error('Failed to send message');
}
} catch (error) {
console.error('Error sending message:', error);
setIsLoading(false);
}
};
useEffect(() => {
return () => {
stopSSEConnection();
};
}, [stopSSEConnection]);
return (
<div className="flex h-screen">
<AppSidebar />
<div className="flex flex-col flex-1">
<ChatHeader modelName="Claude" />
<MessageContainer messages={messages} isLoading={isLoading} />
<ChatFooter onSendMessage={sendMessage} />
</div>
</div>
);
};
export default ChatPage;
tsx Copy code
// components/AppSidebar.tsx
import React from 'react';
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
export const AppSidebar: React.FC = () => {
return (
<div className="w-64 border-r h-full bg-background flex flex-col">
<div className="p-4 border-b">
<Button className="w-full" variant="outline">New Chat</Button>
</div>
<ScrollArea className="flex-1">
<div className="p-4 space-y-2">
{/* Chat history items would go here */}
<div className="p-2 hover:bg-accent rounded-md cursor-pointer">
Previous Chat 1
</div>
<div className="p-2 hover:bg-accent rounded-md cursor-pointer">
Previous Chat 2
</div>
</div>
</ScrollArea>
<div className="p-4 border-t">
<Button variant="ghost" className="w-full justify-start">
Settings
</Button>
</div>
</div>
);
};
tsx Copy code
// components/ChatHeader.tsx
import React from 'react';
import { Button } from "@/components/ui/button";
import { Settings, Info } from "lucide-react";
interface ChatHeaderProps {
modelName: string;
}
export const ChatHeader: React.FC<ChatHeaderProps> = ({ modelName }) => {
return (
<div className="border-b p-4 flex items-center justify-between">
<div className="flex items-center space-x-2">
<div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center">
<span className="text-primary text-sm font-medium">AI</span>
</div>
<h2 className="font-semibold">{modelName}</h2>
</div>
<div className="flex items-center space-x-2">
<Button variant="ghost" size="icon">
<Info className="h-4 w-4" />
</Button>
<Button variant="ghost" size="icon">
<Settings className="h-4 w-4" />
</Button>
</div>
</div>
);
};
tsx Copy code
// components/MessageContainer.tsx
import React, { useRef, useEffect } from 'react';
import { ScrollArea } from "@/components/ui/scroll-area";
import { Message } from '../types';
import { Loader2 } from "lucide-react";
import { Avatar } from "@/components/ui/avatar";
import { cn } from "@/lib/utils";
import { FileAttachmentDisplay } from './FileAttachmentDisplay';
interface MessageContainerProps {
messages: Message[];
isLoading: boolean;
}
export const MessageContainer: React.FC<MessageContainerProps> = ({ messages, isLoading }) => {
const scrollRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (scrollRef.current) {
scrollRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, [messages]);
return (
<ScrollArea className="flex-1 p-4">
<div className="space-y-4 max-w-4xl mx-auto">
{messages.map((message, index) => (
<div
key={index}
className={cn(
"flex items-start gap-4",
message.role === 'user' ? "justify-end" : "justify-start"
)}
>
{message.role === 'assistant' && (
<Avatar className="h-8 w-8 bg-primary/10">
<span className="text-primary text-xs">AI</span>
</Avatar>
)}
<div className={cn(
"rounded-lg p-4 max-w-[80%]",
message.role === 'user'
? "bg-primary text-primary-foreground"
: "bg-muted"
)}>
{message.attachments && message.attachments.length > 0 && (
<div className="mb-2">
<FileAttachmentDisplay attachments={message.attachments} />
</div>
)}
<div className="whitespace-pre-wrap">{message.content}</div>
<div className="text-xs opacity-70 mt-1">
{message.timestamp.toLocaleTimeString()}
</div>
</div>
{message.role === 'user' && (
<Avatar className="h-8 w-8 bg-primary">
<span className="text-primary-foreground text-xs">You</span>
</Avatar>
)}
</div>
))}
{isLoading && (
<div className="flex items-start gap-4">
<Avatar className="h-8 w-8 bg-primary/10">
<span className="text-primary text-xs">AI</span>
</Avatar>
<div className="rounded-lg p-4 bg-muted">
<Loader2 className="h-4 w-4 animate-spin" />
</div>
</div>
)}
<div ref={scrollRef} />
</div>
</ScrollArea>
);
};
tsx Copy code
// components/FileAttachmentDisplay.tsx
import React from 'react';
import { FileAttachment } from '../types';
import { FileIcon, ImageIcon } from 'lucide-react';
interface FileAttachmentDisplayProps {
attachments: FileAttachment[];
}
export const FileAttachmentDisplay: React.FC<FileAttachmentDisplayProps> = ({ attachments }) => {
return (
<div className="flex flex-wrap gap-2">
{attachments.map(attachment => (
<div key={attachment.id} className="border rounded overflow-hidden">
{attachment.type.startsWith('image/') ? (
<div className="relative w-32 h-32">
<img
src={attachment.url}
alt={attachment.name}
className="w-full h-full object-cover"
/>
</div>
) : (
<div className="p-2 flex items-center gap-2">
<FileIcon className="h-4 w-4" />
<span className="text-sm truncate max-w-[120px]">{attachment.name}</span>
</div>
)}
</div>
))}
</div>
);
};
tsx Copy code
// hooks/useSSE.tsx
import { useState, useEffect, useCallback } from 'react';
interface UseSSEProps {
onMessage: (content: string) => void;
onError?: (error: Event) => void;
}
export const useSSE = ({ onMessage, onError }: UseSSEProps) => {
const [eventSource, setEventSource] = useState<EventSource | null>(null);
const startSSEConnection = useCallback((url: string) => {
if (eventSource) {
eventSource.close();
}
const newEventSource = new EventSource(url);
newEventSource.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
onMessage(data.content);
} catch (error) {
console.error('Error parsing SSE message:', error);
}
};
newEventSource.onerror = (error) => {
console.error('SSE connection error:', error);
if (onError) onError(error);
newEventSource.close();
};
setEventSource(newEventSource);
return () => {
newEventSource.close();
};
}, [eventSource, onMessage, onError]);
const stopSSEConnection = useCallback(() => {
if (eventSource) {
eventSource.close();
setEventSource(null);
}
}, [eventSource]);
useEffect(() => {
return () => {
if (eventSource) {