All files / app/components/GalleryEditor GalleryItem.tsx

69.69% Statements 23/33
66.66% Branches 12/18
63.63% Functions 7/11
73.07% Lines 19/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                  2x 2x 2x 2x 2x                                     2x 432x 43x                                                 2x           42x                                     15x                       42x                   69x               2x                           2x                     43x                           43x   43x               43x                                                                                                                                          
import { Close, DragIndicator, Star } from "@mui/icons-material";
import {
  Box,
  IconButton,
  ImageListItem,
  ImageListItemBar,
  styled,
  Tooltip,
  Typography,
} from "@mui/material";
import CircularProgress from "components/CircularProgress";
import { useTranslation } from "i18n";
import { PROFILE } from "i18n/namespaces";
import React from "react";
 
import { GalleryItemData } from "./GalleryEditor";
 
interface GalleryItemProps {
  item: GalleryItemData;
  isFirst: boolean;
  isDragging: boolean;
  isDeleting: boolean;
  canEdit: boolean;
  onDelete: (itemId: number) => void;
  onDragStart: (e: React.DragEvent, itemId: number) => void;
  onDragEnd: (e: React.DragEvent) => void;
  onDragOver: (e: React.DragEvent) => void;
  onTouchStart: (e: React.TouchEvent, itemId: number) => void;
  onTouchMove: (e: React.TouchEvent) => void;
  onTouchEnd: () => void;
}
 
const StyledImageListItem = styled(ImageListItem, {
  shouldForwardProp: (prop) => prop !== "isDragging",
})<{ isDragging: boolean }>(({ theme, isDragging }) => ({
  position: "relative",
  borderRadius: theme.shape.borderRadius,
  overflow: "hidden",
  cursor: "grab",
  opacity: isDragging ? 0.4 : 1,
  transform: isDragging ? "scale(0.95)" : undefined,
  transition: "opacity 0.15s ease-out, transform 0.15s ease-out",
  touchAction: "none",
  userSelect: "none",
  WebkitUserSelect: "none",
  "&:hover": {
    boxShadow: isDragging ? undefined : theme.shadows[4],
    "& .drag-handle": {
      opacity: 1,
    },
    "& .MuiImageListItemBar-root": {
      opacity: 1,
    },
  },
  "&:active": {
    cursor: "grabbing",
  },
}));
 
const StyledImage = styled("img")({
  width: "100%",
  height: "100%",
  objectFit: "cover",
});
 
const DragHandle = styled(Box)(({ theme }) => ({
  position: "absolute",
  top: theme.spacing(1),
  left: theme.spacing(1),
  backgroundColor: "rgba(0, 0, 0, 0.6)",
  borderRadius: theme.shape.borderRadius,
  padding: theme.spacing(0.5),
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  opacity: 0,
  transition: "opacity 0.2s ease",
  color: theme.palette.common.white,
  cursor: "grab",
  "&:active": {
    cursor: "grabbing",
  },
}));
 
const PrimaryBadge = styled(Box)(({ theme }) => ({
  position: "absolute",
  top: theme.spacing(1),
  right: theme.spacing(1),
  backgroundColor: theme.palette.primary.main,
  borderRadius: theme.shape.borderRadius,
  padding: theme.spacing(0.5, 1),
  display: "flex",
  alignItems: "center",
  gap: theme.spacing(0.5),
}));
 
const StyledImageListItemBar = styled(ImageListItemBar)(({ theme }) => ({
  background:
    "linear-gradient(to top, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.4) 70%, transparent 100%)",
  opacity: 0,
  transition: "opacity 0.2s ease",
  "& .MuiImageListItemBar-titleWrap": {
    padding: theme.spacing(1, 1.5),
  },
}));
 
const DeleteButton = styled(IconButton)(({ theme }) => ({
  color: theme.palette.common.white,
  backgroundColor: "rgba(255, 255, 255, 0.1)",
  "&:hover": {
    backgroundColor: "rgba(255, 255, 255, 0.2)",
  },
}));
 
const LoadingOverlay = styled(Box)({
  position: "absolute",
  top: 0,
  left: 0,
  right: 0,
  bottom: 0,
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  backgroundColor: "rgba(0, 0, 0, 0.5)",
  zIndex: 2,
});
 
// Placeholder box that shows where the item will be dropped
export const DropPlaceholder = styled(ImageListItem)(({ theme }) => ({
  borderRadius: theme.shape.borderRadius,
  border: `2px dashed ${theme.palette.primary.main}`,
  backgroundColor: "var(--mui-palette-action-hover)",
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  opacity: 0.8,
  touchAction: "none",
}));
 
export default function GalleryItem({
  item,
  isFirst,
  isDragging,
  isDeleting,
  canEdit,
  onDelete,
  onDragStart,
  onDragEnd,
  onDragOver,
  onTouchStart,
  onTouchMove,
  onTouchEnd,
}: GalleryItemProps) {
  const { t } = useTranslation([PROFILE]);
 
  const handleDragStart = (e: React.DragEvent) => {
    Iif (!canEdit) {
      e.preventDefault();
      return;
    }
    onDragStart(e, item.itemId);
  };
 
  const handleTouchStart = (e: React.TouchEvent) => {
    Iif (!canEdit) return;
    onTouchStart(e, item.itemId);
  };
 
  return (
    <StyledImageListItem
      isDragging={isDragging}
      draggable={canEdit}
      onDragStart={handleDragStart}
      onDragEnd={onDragEnd}
      onDragOver={onDragOver}
      onTouchStart={handleTouchStart}
      onTouchMove={onTouchMove}
      onTouchEnd={onTouchEnd}
      data-item-id={item.itemId}
    >
      <StyledImage
        src={item.thumbnailUrl || item.fullUrl}
        alt={item.caption || t("profile:gallery.photo")}
        loading="lazy"
      />
 
      {isDeleting && (
        <LoadingOverlay>
          <CircularProgress size={32} />
        </LoadingOverlay>
      )}
 
      {canEdit && (
        <DragHandle className="drag-handle">
          <DragIndicator fontSize="small" />
        </DragHandle>
      )}
 
      {isFirst && (
        <PrimaryBadge>
          <Star fontSize="small" sx={{ color: "common.white", fontSize: 14 }} />
          <Typography
            variant="caption"
            sx={{ color: "common.white", fontWeight: 600 }}
          >
            {t("profile:gallery.primary")}
          </Typography>
        </PrimaryBadge>
      )}
 
      {canEdit && (
        <StyledImageListItemBar
          position="bottom"
          actionIcon={
            <Tooltip title={t("profile:gallery.remove_photo")}>
              <DeleteButton
                size="small"
                onClick={() => onDelete(item.itemId)}
                disabled={isDeleting}
                aria-label={t("profile:gallery.remove_photo")}
              >
                <Close fontSize="small" />
              </DeleteButton>
            </Tooltip>
          }
          actionPosition="right"
          title={item.caption || ""}
        />
      )}
    </StyledImageListItem>
  );
}