-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ed717e
commit 3387d43
Showing
2 changed files
with
120 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,42 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useBoardPosition } from "../hooks/useBoardPosition"; | ||
import {socket} from "@/common/lib/socket"; | ||
import {motion } from "framer-motion"; | ||
import {BsCursorFill} from "react-icons/bs"; | ||
|
||
|
||
const SocketMouse = ({ userId}: { userId: string }) => { | ||
|
||
//board postn | ||
const boardPos= useBoardPosition(); | ||
const [x, setX]= useState(boardPos.x.get()); | ||
const [y, setY]= useState(boardPos.y.get()); | ||
import { socket } from "@/common/lib/socket"; | ||
import { motion } from "framer-motion"; | ||
import { BsCursorFill } from "react-icons/bs"; | ||
|
||
const SocketMouse = ({ userId }: { userId: string }) => { | ||
const boardPos = useBoardPosition(); | ||
const [x, setX] = useState(boardPos.x.get()); | ||
const [y, setY] = useState(boardPos.y.get()); | ||
const [pos, setPos] = useState({ x: -1, y: -1 }); | ||
|
||
//updated socket mouse, error prone | ||
|
||
useEffect(()=>{ | ||
socket.on("mouse_moved", (newX, newY, socketIdMoved) => { | ||
if(socketIdMoved === userId){ | ||
setPos({x: newX, y: newY}); | ||
} | ||
}); | ||
}, [userId]); | ||
|
||
//update x position when board position x changes | ||
useEffect(()=>{ | ||
const unsubscribe= boardPos.x.onChange(setX); | ||
return unsubscribe; | ||
|
||
}, [boardPos.x]); | ||
|
||
//update y position when board position y changes | ||
useEffect(()=>{ | ||
const unsubscribe= boardPos.y.onChange(setY); | ||
return unsubscribe; | ||
|
||
}, [boardPos.y]); | ||
|
||
|
||
return ( | ||
<motion.div className="{absolute top-0 left-0 text-blue-800 ${ pos.x === -1 && 'hidden' }}" | ||
animate={{x: pos.x+x, y: pos.y+y}} | ||
transition={{duration: 0.3, ease: "linear"}} | ||
> | ||
<BsCursorFill className="-rotate-90" /> | ||
</motion.div> | ||
) | ||
}; | ||
export default SocketMouse; | ||
useEffect(() => { | ||
socket.on("mouse_moved", (newX, newY, socketIdMoved) => { | ||
if (socketIdMoved === userId) { | ||
setPos({ x: newX, y: newY }); | ||
} | ||
}); | ||
}, [userId]); | ||
|
||
useEffect(() => { | ||
const unsubscribe = boardPos.x.onChange(setX); | ||
return unsubscribe; | ||
}, [boardPos.x]); | ||
|
||
useEffect(() => { | ||
const unsubscribe = boardPos.y.onChange(setY); | ||
return unsubscribe; | ||
}, [boardPos.y]); | ||
|
||
return ( | ||
<motion.div | ||
className={`absolute top-0 left-0 text-blue-800 ${pos.x === -1 && 'hidden'}`} | ||
animate={{ x: pos.x + x, y: pos.y + y }} | ||
transition={{ duration: 0.3, ease: "linear" }} | ||
> | ||
<BsCursorFill className="-rotate-90" /> | ||
</motion.div> | ||
); | ||
}; | ||
|
||
export default SocketMouse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters