"use client"; import { useEffect, useState, useRef } from "react"; import Pusher from "pusher-js"; import { cn } from "@/lib/utils"; import { ProductProps } from '@/app/(root)/order/[orderId]/_components/ProductInfo'; export type Chat = { id: number; userId: string; img: string; msg: string | null; order?: Omit; date: string; }; interface ChatBubbleProps { initialData: { message: string; id: string; User: { name: string | null; image: string | null; id: string | null; }; chat: Chat; }[]; } export const dynamic = "force-dynamic"; function ChatBubble({ initialData }: ChatBubbleProps) { const [totalComments, setTotalComments] = useState(initialData); const messageEndRef = useRef(null); useEffect(() => { const pusher = new Pusher(process.env.NEXT_PUBLIC_PUSHER_KEY as string, { cluster: "us2", }); const channel = pusher.subscribe("chat"); const handleMessage = (data: any) => { try { const parsedComments = JSON.parse(data.message); if (Array.isArray(parsedComments)) { setTotalComments((prev) => [...prev, ...parsedComments]); } else { setTotalComments((prev) => [...prev, parsedComments]); } } catch (error) { console.error("Failed to parse message:", error); } }; channel.bind("message", handleMessage); return () => { channel.unbind("message", handleMessage); pusher.unsubscribe("chat"); }; }, []); const scrollToBottom = () => { messageEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; useEffect(() => { scrollToBottom(); }, [totalComments]); return (
{totalComments.map((chat, index) => (
user
{chat.message}
{chat.chat && chat.chat.order && (
order
{chat.chat.order.game_title}
{chat.chat.order.price} EUR
Description:

{chat.chat.order.desc}

Type:

{chat.chat.order.type}

Game:

{chat.chat.order.game}

)}
))}
); } export { ChatBubble };