import { useEffect } from "react"
import CreatorSocket from "../utils/socket"

interface NotificationListenerProps {
  notificationId: string
  onNotification: (data: any) => void
}

const socketClient = new CreatorSocket("")

const NotificationListener: React.FC<NotificationListenerProps> = ({
  notificationId,
  onNotification,
}) => {
  useEffect(() => {

    if (!notificationId) return

    socketClient.connect()
    socketClient.on(notificationId, onNotification)
    socketClient.emit("registerNotification", { notificationId })

    return () => {
      socketClient.off(notificationId, onNotification)
      socketClient.disconnect()
    }
  }, [notificationId, onNotification])

  return null
}

export default NotificationListener
