123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import React, { memo } from "react";
- import { useUsers } from "../../../../components/users";
- // See https://stackoverflow.com/questions/3680429/click-through-div-to-underlying-elements
- // https://developer.mozilla.org/fr/docs/Web/CSS/pointer-events
- const Image = ({
- width,
- height,
- content = "/default.png",
- backContent,
- flipped = false,
- setState,
- unflippedFor,
- text,
- backText,
- overlay,
- }) => {
- const { currentUser } = useUsers();
- const size = {};
- if (width) {
- size.width = width;
- }
- if (height) {
- size.height = height;
- }
- const onDblClick = React.useCallback(
- (e) => {
- if (e.ctrlKey) {
- setState((prevItem) => {
- if (prevItem.unflippedFor !== null) {
- return { ...prevItem, unflippedFor: null };
- } else {
- return {
- ...prevItem,
- unflippedFor: currentUser.id,
- flipped: false,
- };
- }
- });
- } else {
- setState((prevItem) => ({
- ...prevItem,
- flipped: !prevItem.flipped,
- unflippedFor: null,
- }));
- }
- },
- [setState, currentUser.id]
- );
- let image;
- if (
- backContent &&
- (flipped || (unflippedFor && unflippedFor !== currentUser.id))
- ) {
- image = (
- <>
- {text && (
- <div
- className="image-text"
- style={{
- position: "absolute",
- right: 0,
- padding: "0 3px",
- backgroundColor: "black",
- color: "white",
- borderRadius: "50%",
- userSelect: "none",
- }}
- >
- {backText}
- </div>
- )}
- <img
- src={backContent}
- alt=""
- draggable={false}
- {...size}
- style={{ userSelect: "none", pointerEvents: "none" }}
- />
- </>
- );
- } else {
- image = (
- <div className="image-wrapper" style={{ position: "relative" }}>
- {unflippedFor && (
- <div
- style={{
- position: "absolute",
- top: "-18px",
- left: "4px",
- color: "#555",
- backgroundColor: "#CCCCCCA0",
- userSelect: "none",
- pointerEvents: "none",
- }}
- >
- Only you
- </div>
- )}
- {overlay && (
- <img
- src={overlay.content}
- alt=""
- style={{
- position: "absolute",
- userSelect: "none",
- pointerEvents: "none",
- }}
- />
- )}
- {text && (
- <div
- className="image-text"
- style={{
- position: "absolute",
- right: 0,
- padding: "0 3px",
- backgroundColor: "black",
- color: "white",
- borderRadius: "50%",
- userSelect: "none",
- }}
- >
- {text}
- </div>
- )}
- <img
- src={content}
- alt=""
- draggable={false}
- {...size}
- style={{ userSelect: "none", pointerEvents: "none" }}
- />
- </div>
- );
- }
- return <div onDoubleClick={onDblClick}>{image}</div>;
- };
- export default memo(Image);
|