選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

page.tsx 3.6KB

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