lang
This commit is contained in:
35
src/providers/ChatSocketProvider.tsx
Normal file
35
src/providers/ChatSocketProvider.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useUser } from "@/hooks/useUser";
|
||||
import {
|
||||
acquireChatSocket,
|
||||
joinUserRoom,
|
||||
releaseChatSocket,
|
||||
} from "@/lib/chat/socketClient";
|
||||
|
||||
export default function ChatSocketProvider({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const user = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
if (!user?._id) return;
|
||||
|
||||
const socket = acquireChatSocket();
|
||||
const uid = String(user._id);
|
||||
|
||||
const join = () => joinUserRoom(uid);
|
||||
socket.on("connect", join);
|
||||
if (socket.connected) join();
|
||||
|
||||
return () => {
|
||||
socket.off("connect", join);
|
||||
releaseChatSocket();
|
||||
};
|
||||
}, [user?._id]);
|
||||
|
||||
return children;
|
||||
}
|
||||
Reference in New Issue
Block a user