-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
8 changed files
with
195 additions
and
29 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useEffect, useState, useContext } from 'react'; | ||
import { BsFillCursorFill } from 'react-icons/bs'; | ||
import { pubCursor } from '@api/socket'; | ||
import { socketContext } from '@hooks/useSocket'; | ||
import { useGetTripsAuthority } from '@hooks/useGetTripsAuthority'; | ||
|
||
type TripCursorData = { | ||
memberId: number; | ||
x: number; | ||
y: number; | ||
name: string; | ||
}; | ||
|
||
type PlanCursorProps = { | ||
date: string; | ||
}; | ||
|
||
const PlanCursor = ({ date }: PlanCursorProps) => { | ||
const token = localStorage.getItem('accessToken'); | ||
const { memberId } = useGetTripsAuthority(); | ||
const { callBackPub, tripId, tripMember } = useContext(socketContext); | ||
const [position, setPosition] = useState({ x: 0, y: 0 }); | ||
|
||
const myName = tripMember?.data?.tripMembers.find( | ||
(member) => member.memberId === memberId, | ||
); | ||
|
||
useEffect(() => { | ||
const handleMouseMove = (e: MouseEvent) => { | ||
setPosition({ x: e.clientX, y: e.clientY }); | ||
}; | ||
const cursorStyle = (style: string): void => { | ||
document.querySelectorAll('*').forEach((el) => { | ||
const element = el as HTMLElement; | ||
element.style.cursor = style; | ||
}); | ||
}; | ||
cursorStyle('none'); | ||
document.addEventListener('mousemove', handleMouseMove); | ||
return () => { | ||
cursorStyle('auto'); | ||
document.removeEventListener('mousemove', handleMouseMove); | ||
}; | ||
}, []); | ||
|
||
// useEffect(() => { | ||
// if (token && position && myName && date && tripId) { | ||
// const timeoutId = setTimeout(() => { | ||
// callBackPub(() => | ||
// pubCursor( | ||
// { | ||
// token: token, | ||
// visitDate: date, | ||
// x: position.x, | ||
// y: position.y, | ||
// }, | ||
// tripId, | ||
// ), | ||
// ); | ||
// }, 1000); | ||
|
||
// return () => clearTimeout(timeoutId); | ||
// } | ||
// }, [position]); | ||
|
||
useEffect(() => { | ||
if (token && position && myName && date && tripId) { | ||
callBackPub(() => | ||
pubCursor( | ||
{ | ||
token: token, | ||
visitDate: date, | ||
x: position.x, | ||
y: position.y, | ||
}, | ||
tripId, | ||
), | ||
); | ||
} | ||
}, [position]); | ||
|
||
return ( | ||
<div | ||
className="pointer-events-none fixed z-50 w-full -translate-x-2 -translate-y-2 transform" | ||
style={{ left: `${position.x}px`, top: `${position.y}px` }}> | ||
<BsFillCursorFill size={15} color="blue" className="scale-x-[-1]" /> | ||
<div className="text-bold text-blue absolute left-1 top-2 p-1 text-center text-xs"> | ||
{myName?.name} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PlanCursor; |
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useState, useContext } from 'react'; | ||
import { BsFillCursorFill } from 'react-icons/bs'; | ||
import { pubCursor } from '@api/socket'; | ||
import { socketContext } from '@hooks/useSocket'; | ||
import { useGetTripsAuthority } from '@hooks/useGetTripsAuthority'; | ||
|
||
type TripCursorData = { | ||
memberId: number; | ||
x: number; | ||
y: number; | ||
name: string; | ||
}; | ||
|
||
type PlanCursorProps = { | ||
date: string; | ||
}; | ||
|
||
const PlanOtherCursor = () => { | ||
const { memberId } = useGetTripsAuthority(); | ||
const { tripCursor } = useContext(socketContext); | ||
const [otherCursors, setOtherCursors] = useState<TripCursorData[]>([]); | ||
|
||
console.log(otherCursors); | ||
useEffect(() => { | ||
if ( | ||
tripCursor && | ||
tripCursor.data && | ||
tripCursor.data.memberId !== memberId | ||
) { | ||
setOtherCursors((prevCursors) => { | ||
const existingCursorIndex = prevCursors.findIndex( | ||
(cursor) => cursor.memberId === tripCursor.data!.memberId, | ||
); | ||
|
||
if (existingCursorIndex !== -1) { | ||
const updatedCursors = [...prevCursors]; | ||
updatedCursors[existingCursorIndex] = tripCursor.data!; | ||
return updatedCursors; | ||
} else { | ||
return [...prevCursors, tripCursor.data!]; | ||
} | ||
}); | ||
} | ||
}, [tripCursor, memberId]); | ||
|
||
return ( | ||
<> | ||
{otherCursors.map((cursor, index) => ( | ||
<div | ||
key={index} | ||
className="pointer-events-none fixed z-50 w-full -translate-x-2 -translate-y-2 transform" | ||
style={{ left: `${cursor.x}px`, top: `${cursor.y}px` }}> | ||
<BsFillCursorFill size={15} color="red" className="scale-x-[-1]" /> | ||
<div className="text-bold absolute left-1 top-2 p-1 text-center text-xs text-red"> | ||
{cursor.name} | ||
</div> | ||
</div> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default PlanOtherCursor; |
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
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