"use client"; import React from 'react'; import { Flex, Typography, Layout } from 'antd'; import { useRouter } from 'next/navigation'; import { useQuery } from '@tanstack/react-query'; import PageTitle from '@/components/ui/PageTitle'; import type { Chat } from '@/types/chat'; import { getAllChat } from '@/app/api/user/chatService'; import LoadingScreen from '@/components/layout/LoadingScreen'; const { Text } = Typography; const ChatPage: React.FC = () => { const router = useRouter(); // Fetch chat data using React Query const { data: chatList = [], isLoading, } = useQuery({ queryKey: ['getAllChat'], queryFn: getAllChat, }); // Show loading state while fetching data if (isLoading) return // Optional: handle unexpected empty/null data if (!chatList || chatList.length === 0) { return ( RUCCAN CHAT No chat data available. ); } return ( RUCCAN CHAT {/* Chat List */} {chatList.map((chat) => { // Get last message from chat const lastChat = chat.chatList?.[chat.chatList.length - 1]; // Count unread messages const unreadCount = chat.chatList?.reduce( (acc, item) => (!item.read ? acc + 1 : acc), 0 ) ?? 0; return ( router.push(`/user/chat/${chat.id}`)} > {/* Chat Name & Time */} {chat.name} {lastChat?.time ?? ''} {/* Last Message & Unread Count */} {lastChat?.text ?? 'No messages yet.'} {/* Show unread count if > 0 */} {unreadCount > 0 && ( {unreadCount} )} ); })} ); }; export default ChatPage;