'use client'; import React, { useState, useEffect } from 'react'; import { ResizableHandle, ResizablePanel, ResizablePanelGroup, } from '@/components/ui/resizable'; import { cn } from '@/lib/utils'; import { Chat } from '@/lib/types'; import { ChatList } from './chat-list'; import { Message, UserData } from '@/lib/types'; import { MessageList } from './message-list'; import { MessageInput } from '../message-input'; import { ChatTopbar } from './chat-topbar'; import { Sidebar } from './sidebar'; interface ChatLayoutProps { defaultLayout: number[] | undefined; navCollapsedSize: number; chats: Chat[]; loggedInUser: UserData; } export function ChatLayout({ defaultLayout = [320, 1080], navCollapsedSize, chats: initialChats, loggedInUser, }: ChatLayoutProps) { const [isCollapsed, setIsCollapsed] = React.useState(false); const [selectedChat, setSelectedChat] = React.useState(initialChats[0]); const [isMobile, setIsMobile] = useState(false); const [chats, setChats] = useState(initialChats); const [isTyping, setIsTyping] = useState(false); useEffect(() => { const checkScreenWidth = () => { setIsMobile(window.innerWidth <= 768); }; checkScreenWidth(); window.addEventListener('resize', checkScreenWidth); return () => { window.removeEventListener('resize', checkScreenWidth); }; }, []); const handleSendMessage = async (newMessageText: string) => { if (!selectedChat) return; setIsTyping(true); const themedResponse = { theme: undefined }; const newMessage: Message = { id: `msg-${Date.now()}`, sender: loggedInUser, text: newMessageText, timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }), theme: themedResponse.theme, }; // Simulate a response setTimeout(() => { const otherUser = selectedChat.users.find(u => u.id !== loggedInUser.id); if (otherUser) { const responseMessage: Message = { id: `msg-${Date.now() + 1}`, sender: otherUser, text: `This is an automatic reply to: "${newMessageText}"`, timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }), }; setChats(prevChats => prevChats.map(chat => chat.id === selectedChat.id ? { ...chat, messages: [...chat.messages, newMessage, responseMessage] } : chat ) ); } setIsTyping(false); }, 1500); setChats(prevChats => prevChats.map(chat => chat.id === selectedChat.id ? { ...chat, messages: [...chat.messages, newMessage] } : chat ) ); }; const currentChat = chats.find(c => c.id === selectedChat?.id); return (
{isMobile ? (
{!selectedChat ? ( ) : (
setSelectedChat(null)} /> {/* @ts-ignore */}
)}
) : ( { document.cookie = `react-resizable-panels:layout=${JSON.stringify( sizes )}`; }} className="h-full items-stretch" > { setIsCollapsed(true); document.cookie = `react-resizable-panels:collapsed=${JSON.stringify( true )}`; }} onExpand={() => { setIsCollapsed(false); document.cookie = `react-resizable-panels:collapsed=${JSON.stringify( false )}`; }} className={cn( isCollapsed && 'min-w-[50px] transition-all duration-300 ease-in-out' )} > {selectedChat ? (
{/* @ts-ignore */}
) : (

Select a chat to start messaging

Your conversations will appear here.

)}
)}
); }