You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use client";
  2. import React from 'react';
  3. import { Layout } from 'antd';
  4. import type { Chat } from '@/types/chat';
  5. import PageTitle from '@/components/ui/PageTitle';
  6. import { useRouter } from 'next/navigation';
  7. import { useQuery } from '@tanstack/react-query';
  8. import { getAllChat } from '@/app/api/user/chatService';
  9. const Chat: React.FC = () => {
  10. const router = useRouter();
  11. const {data:chatList, error, isLoading} = useQuery<Chat[] | undefined | null>({
  12. queryKey:["getAllChat"],
  13. queryFn: () => getAllChat()
  14. })
  15. return (
  16. <Layout>
  17. <PageTitle title='RUCCAN CHAT' />
  18. <div className="flex flex-col h-screen w-full bg-white">
  19. {/* Chat List */}
  20. {chatList?.map((chat) => {
  21. const lastChat = chat.chatList[chat.chatList.length - 1]
  22. const unreadCount = chat.chatList.reduce((accum, chatItem) => {
  23. return chatItem.read ? accum : accum + 1;
  24. }, 0);
  25. return (
  26. <div
  27. key={chat.id}
  28. className="px-4 py-3 border-b border-gray-100 hover:bg-gray-50 cursor-pointer flex flex-col gap-1"
  29. onClick={()=>{
  30. router.push(`/user/chat/${chat.id}`)
  31. }}
  32. >
  33. <div className="flex justify-between items-center">
  34. <span className="font-medium text-gray-900">{chat.name}</span>
  35. <span className="text-xs text-gray-500">{lastChat.time}</span>
  36. </div>
  37. <div className="flex justify-between items-center">
  38. <p className="text-sm text-gray-600 truncate max-w-[250px]">
  39. {lastChat.text}
  40. </p>
  41. {(unreadCount > 0) && (
  42. <span className="bg-[#72c4ff] text-white text-xs font-bold px-2 py-0.5 rounded-full">
  43. {unreadCount}
  44. </span>
  45. )}
  46. </div>
  47. </div>
  48. )
  49. })}
  50. </div>
  51. </Layout>
  52. );
  53. };
  54. export default Chat;