2020-05-18 19:21:29 +10:00
|
|
|
import React, { useRef, useContext } from "react";
|
2020-04-13 00:24:03 +10:00
|
|
|
import { Box, Image } from "theme-ui";
|
|
|
|
|
|
2020-04-23 10:09:12 +10:00
|
|
|
import TokenLabel from "../token/TokenLabel";
|
|
|
|
|
import TokenStatus from "../token/TokenStatus";
|
2020-04-13 00:24:03 +10:00
|
|
|
|
2020-04-23 10:09:12 +10:00
|
|
|
import usePreventTouch from "../../helpers/usePreventTouch";
|
2020-04-24 15:50:05 +10:00
|
|
|
import useDataSource from "../../helpers/useDataSource";
|
|
|
|
|
|
2020-05-18 19:21:29 +10:00
|
|
|
import AuthContext from "../../contexts/AuthContext";
|
|
|
|
|
|
2020-04-24 15:50:05 +10:00
|
|
|
import { tokenSources } from "../../tokens";
|
2020-04-13 00:24:03 +10:00
|
|
|
|
2020-04-24 18:39:11 +10:00
|
|
|
function MapToken({ token, tokenState, tokenSizePercent, className }) {
|
2020-05-18 19:21:29 +10:00
|
|
|
const { userId } = useContext(AuthContext);
|
2020-04-24 15:50:05 +10:00
|
|
|
const imageSource = useDataSource(token, tokenSources);
|
|
|
|
|
|
2020-04-13 00:24:03 +10:00
|
|
|
const imageRef = useRef();
|
|
|
|
|
// Stop touch to prevent 3d touch gesutre on iOS
|
|
|
|
|
usePreventTouch(imageRef);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
style={{
|
2020-04-24 18:39:11 +10:00
|
|
|
transform: `translate(${tokenState.x * 100}%, ${tokenState.y * 100}%)`,
|
2020-04-13 00:24:03 +10:00
|
|
|
width: "100%",
|
|
|
|
|
height: "100%",
|
2020-05-18 19:21:29 +10:00
|
|
|
transition:
|
|
|
|
|
tokenState.lastEditedBy === userId
|
|
|
|
|
? "initial"
|
|
|
|
|
: "transform 0.5s ease",
|
2020-04-13 00:24:03 +10:00
|
|
|
}}
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
pointerEvents: "none",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
style={{
|
2020-04-24 18:39:11 +10:00
|
|
|
width: `${tokenSizePercent * (tokenState.size || 1)}%`,
|
2020-04-13 00:24:03 +10:00
|
|
|
}}
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
pointerEvents: "all",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2020-04-13 11:21:47 +10:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
display: "flex", // Set display to flex to fix height being calculated wrong
|
|
|
|
|
width: "100%",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2020-04-13 00:24:03 +10:00
|
|
|
<Image
|
|
|
|
|
className={className}
|
|
|
|
|
sx={{
|
|
|
|
|
userSelect: "none",
|
|
|
|
|
touchAction: "none",
|
|
|
|
|
width: "100%",
|
2020-05-18 19:21:29 +10:00
|
|
|
// Fix image from being clipped when transitioning
|
|
|
|
|
willChange: "transform",
|
2020-04-13 00:24:03 +10:00
|
|
|
}}
|
2020-04-24 15:50:05 +10:00
|
|
|
src={imageSource}
|
|
|
|
|
// pass id into the dom element which is then used by the ProxyToken
|
2020-04-24 18:39:11 +10:00
|
|
|
data-id={tokenState.id}
|
2020-04-13 00:24:03 +10:00
|
|
|
ref={imageRef}
|
|
|
|
|
/>
|
2020-04-24 18:39:11 +10:00
|
|
|
{tokenState.statuses && (
|
|
|
|
|
<TokenStatus statuses={tokenState.statuses} />
|
|
|
|
|
)}
|
|
|
|
|
{tokenState.label && <TokenLabel label={tokenState.label} />}
|
2020-04-13 00:24:03 +10:00
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapToken;
|