import * as React from "react";
import ReactCursorPosition from "react-cursor-position";
import Vec from "@tldraw/vec";
import { _ } from "lodash";
function usePrevious(value) {
const ref = React.useRef();
React.useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
const renderCursor = (
name,
color,
x,
y,
currentPoint,
pageState,
owner = false
) => {
const z = !owner ? 2 : 1;
let _x = null;
let _y = null;
if (!currentPoint) {
_x = ((x) + pageState?.camera?.point[0]) * pageState?.camera?.zoom;
_y = ((y) + pageState?.camera?.point[1]) * pageState?.camera?.zoom;
}
return (
<>
{name}
>
);
};
const PositionLabel = (props) => {
const {
position: { x = 0, y = 0 } = {},
currentUser,
currentPoint,
pageState,
publishCursorUpdate,
whiteboardId,
} = props;
const { name, color, userId, presenter } = currentUser;
const prevCurrentPoint = usePrevious(currentPoint);
React.useEffect(() => {
try {
const point = _.isEqual(currentPoint, prevCurrentPoint)
? [x, y]
: currentPoint;
publishCursorUpdate({
xPercent: ((point[0] / pageState?.camera?.zoom) - pageState?.camera?.point[0]),
yPercent: ((point[1] / pageState?.camera?.zoom) - pageState?.camera?.point[1]),
whiteboardId
});
} catch (e) {
console.log(e);
}
}, [x, y]);
return (
<>
{renderCursor(name, color, x, y, currentPoint, props.pageState)}
>
);
};
export default function Cursors(props) {
return (
<>
{props.children}
{props.otherCursors.map((c) => {
return (
c && props.currentUser.userId !== c?.userId &&
// !c.isPositionOutside &&
renderCursor(
c.userName,
c.presenter ? "#C70039" : "#AFE1AF",
c.xPercent,
c.yPercent,
null,
props.tldrawAPI?.getPageState(),
true
)
);
})}
>
);
}