diff --git a/frontend/src/components/notification-handler.tsx b/frontend/src/components/notification-handler.tsx new file mode 100644 index 0000000..a6440f2 --- /dev/null +++ b/frontend/src/components/notification-handler.tsx @@ -0,0 +1,116 @@ +"use client"; + +import { Bell, Heart, MessageCircle, Reply } from "lucide-react"; +import { useRouter } from "next/navigation"; +import * as React from "react"; +import { toast } from "sonner"; +import { useSocket } from "@/providers/socket-provider"; + +interface NotificationData { + type: "comment" | "reply" | "like_comment" | "message"; + userId: string; + username: string; + contentId?: string; + commentId?: string; + text: string; +} + +export function NotificationHandler() { + const { socket } = useSocket(); + const router = useRouter(); + + React.useEffect(() => { + if (!socket) return; + + const handleNotification = (data: NotificationData) => { + // Ne pas afficher de toast si on est déjà sur la page des messages pour un nouveau message + if (data.type === "message" && window.location.pathname === "/messages") { + return; + } + + toast.custom( + (t) => ( +
{ + toast.dismiss(t); + if (data.type === "message") { + router.push("/messages"); + } else if (data.contentId) { + router.push(`/meme/${data.contentId}`); + } + }} + onKeyDown={(e) => { + if (e.key === "Enter" || e.key === " ") { + toast.dismiss(t); + if (data.type === "message") { + router.push("/messages"); + } else if (data.contentId) { + router.push(`/meme/${data.contentId}`); + } + } + }} + > +
+ {data.type === "comment" && ( + + )} + {data.type === "reply" && } + {data.type === "like_comment" && ( + + )} + {data.type === "message" && ( + + )} +
+
+

@{data.username}

+

{data.text}

+
+ +
+ ), + { + duration: 5000, + position: "top-right", + }, + ); + }; + + socket.on("notification", handleNotification); + + // Aussi pour les nouveaux messages (si on veut un toast global) + socket.on("new_message", (data: { message: any }) => { + if (window.location.pathname !== "/messages") { + toast( + `Nouveau message de @${data.message.sender?.username || "un membre"}`, + { + description: data.message.text.substring(0, 50), + action: { + label: "Voir", + onClick: () => router.push("/messages"), + }, + }, + ); + } + }); + + return () => { + socket.off("notification"); + socket.off("new_message"); + }; + }, [socket, router]); + + return null; +}