Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

page.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use client";
  2. import React from 'react';
  3. import { Flex, Typography, Layout } from 'antd';
  4. import { useRouter } from 'next/navigation';
  5. import { useQuery } from '@tanstack/react-query';
  6. import PageTitle from '@/components/ui/PageTitle';
  7. import type { Chat } from '@/types/chat';
  8. import { getAllChat } from '@/app/api/user/chatService';
  9. import LoadingScreen from '@/components/layout/LoadingScreen';
  10. const { Text } = Typography;
  11. const ChatPage: React.FC = () => {
  12. const router = useRouter();
  13. // Fetch chat data using React Query
  14. const {
  15. data: chatList = [],
  16. error,
  17. isLoading,
  18. } = useQuery<Chat[] | undefined | null>({
  19. queryKey: ['getAllChat'],
  20. queryFn: getAllChat,
  21. });
  22. // Show loading state while fetching data
  23. if (isLoading) return <LoadingScreen/>
  24. // Optional: handle unexpected empty/null data
  25. if (!chatList || chatList.length === 0) {
  26. return (
  27. <Layout>
  28. <PageTitle>RUCCAN CHAT</PageTitle>
  29. <Flex vertical justify="center" align="center">
  30. <Text type="secondary">No chat data available.</Text>
  31. </Flex>
  32. </Layout>
  33. );
  34. }
  35. return (
  36. <Layout>
  37. <PageTitle>RUCCAN CHAT</PageTitle>
  38. {/* Chat List */}
  39. <Flex vertical className="!w-full !bg-white overflow-y-auto">
  40. {chatList.map((chat) => {
  41. // Get last message from chat
  42. const lastChat = chat.chatList?.[chat.chatList.length - 1];
  43. // Count unread messages
  44. const unreadCount = chat.chatList?.reduce(
  45. (acc, item) => (!item.read ? acc + 1 : acc),
  46. 0
  47. ) ?? 0;
  48. return (
  49. <Flex
  50. key={chat.id}
  51. vertical
  52. gap={4}
  53. className="!px-4 !py-3 !border-b !border-gray-100 hover:!bg-gray-50 !cursor-pointer"
  54. onClick={() => router.push(`/user/chat/${chat.id}`)}
  55. >
  56. {/* Chat Name & Time */}
  57. <Flex justify="space-between" align="center">
  58. <Text className="!font-medium text-gray-900">{chat.name}</Text>
  59. <Text className="!text-xs text-gray-500">
  60. {lastChat?.time ?? ''}
  61. </Text>
  62. </Flex>
  63. {/* Last Message & Unread Count */}
  64. <Flex justify="space-between" align="center">
  65. <Text
  66. className="!text-sm text-gray-600 truncate max-w-[250px]"
  67. title={lastChat?.text}
  68. >
  69. {lastChat?.text ?? 'No messages yet.'}
  70. </Text>
  71. {/* Show unread count if > 0 */}
  72. {unreadCount > 0 && (
  73. <Text className="!bg-[#72c4ff] !text-white !text-xs !font-bold px-2 py-0.5 !rounded-full">
  74. {unreadCount}
  75. </Text>
  76. )}
  77. </Flex>
  78. </Flex>
  79. );
  80. })}
  81. </Flex>
  82. </Layout>
  83. );
  84. };
  85. export default ChatPage;