import { useEffect } from "react"; import { useSocketStore } from "../../../store/socket/useSocketStore"; import { getRelativeTime } from "../../../components/ui/collaboration/function/getRelativeTime"; import useThreadResponseHandler from "../responseHandler/useThreadResponseHandler"; const CollaborationResponses = () => { const { threadSocket } = useSocketStore(); const { addThreadToScene, updateThreadInScene, removeThreadFromScene, addReplyToThread, removeReplyFromThread, } = useThreadResponseHandler(); //#region Thread useEffect(() => { if (!threadSocket) return; threadSocket.on("v1-thread:response:create", (data: any) => { if (!data.message || !data.data) { echo.error(`Error adding or updating thread`); return; } if (data.message === "Thread created Successfully") { const comment: ThreadSchema = { state: data.data.state, threadId: data.data._id, creatorId: data.data.createdBy, creatorName: data.data.creatorName, threadTitle: data.data.threadTitle, createdAt: getRelativeTime(data.data.createdAt), position: data.data.position, rotation: data.data.rotation, comments: [], }; addThreadToScene(comment, () => { echo.log("Thread created successfully"); }); } }); threadSocket.on("v1-thread:response:delete", (data: any) => { if (!data.message || !data.data) { echo.error(`Error in deleting thread`); return; } if (data.message === "Thread deleted Successfully") { removeThreadFromScene(data.data._id, () => { echo.log("Thread Deleted successfully"); }); } }); threadSocket.on("v1-thread:response:updateTitle", (data: any) => { if (!data.message || !data.data) { echo.error(`Error updating thread`); return; } if (data.message === "ThreadTitle updated Successfully") { const updatedThread: ThreadSchema = { state: data.data.state, threadId: data.data._id, creatorId: data.data.createdBy._id, creatorName: data.data.createdBy.userName, threadTitle: data.data.threadTitle, createdAt: getRelativeTime(data.data.createdAt), position: data.data.position, rotation: data.data.rotation, comments: data.data.comments, }; updateThreadInScene(data.data._id, updatedThread, () => { echo.log("Thread title is updated"); }); } }); threadSocket.on("v1-Comment:response:add", (data: any) => { if (data.message === "Thread comments add Successfully") { const reply: Reply = { creatorName: data.data.creatorName, creatorId: data.data.userId, createdAt: getRelativeTime(data.data.createdAt), comment: data.data.comment, lastUpdatedAt: "2hrs", replyId: data.data._id, }; addReplyToThread(data.data.threadId[0], reply); } }); threadSocket.on("v1-Comment:response:delete", (data: any) => { if (!data.message || !data.data) { echo.error(`Error deleting reply`); return; } if (data.message === "Thread comment deleted Successfully") { removeReplyFromThread(data.data._id, data.data.comments[0]._id, () => { echo.log("Reply added Successfully"); }); } }); return () => { if (threadSocket) { threadSocket.off("v1-thread:response:create"); threadSocket.off("v1-thread:response:delete"); threadSocket.off("v1-thread:response:updateTitle"); threadSocket.off("v1-Comment:response:add"); threadSocket.off("v1-Comment:response:delete"); } }; }, [threadSocket]); //#endregion return null; }; export default CollaborationResponses;