import { userSignUpData } from "@/redux/reducer/authSlice"; import { formatChatMessageTime, formatMessageDate, formatPriceAbbreviated, t, } from "@/utils"; import { getMessagesApi } from "@/utils/api"; import { Fragment, useEffect, useRef, useState } from "react"; import { useSelector } from "react-redux"; import { Skeleton } from "@/components/ui/skeleton"; import { Loader2, ChevronUp } from "lucide-react"; import dynamic from "next/dynamic"; const SendMessage = dynamic(() => import("./SendMessage"), { ssr: false }); import GiveReview from "./GiveReview"; import { getNotification } from "@/redux/reducer/globalStateSlice"; import CustomImage from "@/components/Common/CustomImage"; import { cn } from "@/lib/utils"; // Skeleton component for chat messages const ChatMessagesSkeleton = () => { return (
{message?.message}
); } }; const ChatMessages = ({ selectedChatDetails, isSelling, setSelectedChatDetails, setBuyer, chatId, }) => { const notification = useSelector(getNotification); const [chatMessages, setChatMessages] = useState([]); const [currentMessagesPage, setCurrentMessagesPage] = useState(1); const [hasMoreChatMessages, setHasMoreChatMessages] = useState(false); const [isLoadPrevMesg, setIsLoadPrevMesg] = useState(false); const [IsLoading, setIsLoading] = useState(false); const [showReviewDialog, setShowReviewDialog] = useState(false); const lastMessageDate = useRef(null); const isAskForReview = !isSelling && selectedChatDetails?.item?.status === "sold out" && !selectedChatDetails?.item?.review && Number(selectedChatDetails?.item?.sold_to) === Number(selectedChatDetails?.buyer_id); const user = useSelector(userSignUpData); const userId = user?.id; useEffect(() => { if (selectedChatDetails?.id) { fetchChatMessgaes(1); } }, [selectedChatDetails?.id]); useEffect(() => { if ( notification?.type === "chat" && Number(notification?.item_offer_id) === Number(chatId) && (notification?.user_type === "Seller" ? !isSelling : isSelling) ) { const newMessage = { message_type: notification?.message_type_temp, message: notification?.message, sender_id: Number(notification?.sender_id), created_at: notification?.created_at, audio: notification?.audio, file: notification?.file, id: Number(notification?.id), item_offer_id: Number(notification?.item_offer_id), updated_at: notification?.updated_at, }; setChatMessages((prev) => [...prev, newMessage]); } }, [notification]); const fetchChatMessgaes = async (page) => { try { page > 1 ? setIsLoadPrevMesg(true) : setIsLoading(true); const response = await getMessagesApi.chatMessages({ item_offer_id: selectedChatDetails?.id, page, }); if (response?.data?.error === false) { const currentPage = Number(response?.data?.data?.current_page); const lastPage = Number(response?.data?.data?.last_page); const hasMoreChatMessages = currentPage < lastPage; const chatMessages = (response?.data?.data?.data).reverse(); setCurrentMessagesPage(currentPage); setHasMoreChatMessages(hasMoreChatMessages); page > 1 ? setChatMessages((prev) => [...chatMessages, ...prev]) : setChatMessages(chatMessages); } } catch (error) { console.log(error); } finally { setIsLoadPrevMesg(false); setIsLoading(false); } }; return ( <>{label}
{selectedChatDetails.formatted_amount}{messageDate}
)} {message.sender_id === userId ? ({formatChatMessageTime(message?.created_at)}
{formatChatMessageTime(message?.created_at)}