Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

page.tsx 3.7KB

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