All files / app/features/profile/view ProfilePhotoGallery.tsx

80.48% Statements 33/41
71.42% Branches 10/14
61.53% Functions 8/13
77.77% Lines 28/36

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            3x 3x 3x 3x 3x 3x 3x 3x   3x           3x         3x             9x                                   17x           3x           9x                 30x     40x 40x 40x 40x 40x   40x               23x 20x     3x         3x                                             9x                                                                             9x                            
import {
  Box,
  ImageList,
  ImageListItem,
  styled,
  Typography,
} from "@mui/material";
import CircularProgress from "components/CircularProgress";
import FlagButton from "features/FlagButton";
import { useGallery } from "features/profile/hooks/useGallery";
import { useProfileUser } from "features/profile/hooks/useProfileUser";
import { useTranslation } from "i18n";
import { PROFILE } from "i18n/namespaces";
import { useState } from "react";
 
import PhotoLightbox from "./PhotoLightbox";
 
interface ProfilePhotoGalleryProps {
  galleryId: number;
}
 
const GalleryContainer = styled(Box)(({ theme }) => ({
  marginTop: theme.spacing(2),
  marginBottom: theme.spacing(1),
}));
 
const StyledImageList = styled(ImageList)(({ theme }) => ({
  width: "100%",
  margin: 0,
  // Override default MUI ImageList margins
  marginTop: "0 !important",
}));
 
const StyledImageListItem = styled(ImageListItem)(({ theme }) => ({
  cursor: "pointer",
  overflow: "hidden",
  borderRadius: theme.shape.borderRadius,
  transition: "box-shadow 0.2s ease, transform 0.2s ease",
  border: `1px solid var(--mui-palette-grey-300)`,
  "&:hover": {
    boxShadow: theme.shadows[4],
    transform: "scale(1.02)",
  },
  "& img": {
    width: "100%",
    height: "100%",
    objectFit: "cover",
    display: "block",
  },
}));
 
const LoadingContainer = styled(Box)(({ theme }) => ({
  display: "flex",
  justifyContent: "center",
  padding: theme.spacing(3),
}));
 
const ThumbnailContainer = styled(Box)({
  position: "relative",
  width: "100%",
  height: "100%",
});
 
const FlagButtonWrapper = styled(Box)(({ theme }) => ({
  position: "absolute",
  bottom: theme.spacing(0.5),
  right: theme.spacing(0.5),
  "& svg": {
    fontSize: 16,
  },
}));
 
export default function ProfilePhotoGallery({
  galleryId,
}: ProfilePhotoGalleryProps) {
  const { t } = useTranslation([PROFILE]);
  const { data: gallery, isLoading } = useGallery(galleryId);
  const profileUser = useProfileUser();
  const [lightboxOpen, setLightboxOpen] = useState(false);
  const [selectedPhotoIndex, setSelectedPhotoIndex] = useState(0);
 
  if (isLoading) {
    return (
      <LoadingContainer>
        <CircularProgress />
      </LoadingContainer>
    );
  }
 
  if (!gallery || !gallery.photosList || gallery.photosList.length === 0) {
    return null;
  }
 
  const handlePhotoClick = (index: number) => {
    setSelectedPhotoIndex(index);
    setLightboxOpen(true);
  };
 
  const handleCloseLightbox = () => {
    setLightboxOpen(false);
  };
 
  return (
    <GalleryContainer>
      <Typography variant="h1" sx={{ mb: 2 }}>
        {t("profile:heading.photos_section")}
      </Typography>
 
      <StyledImageList
        cols={4}
        rowHeight={200}
        gap={16}
        sx={{
          gridTemplateColumns: {
            xs: "repeat(2, 1fr) !important", // 2 columns on mobile
            sm: "repeat(3, 1fr) !important", // 3 columns on tablet
            md: "repeat(4, 1fr) !important", // 4 columns on desktop
          },
        }}
      >
        {gallery.photosList.map((photo, index) => (
          <StyledImageListItem
            key={photo.itemId}
            onClick={() => handlePhotoClick(index)}
            role="button"
            tabIndex={0}
            aria-label={
              photo.caption ||
              `${t("profile:gallery.photo_item_a11y")} ${index + 1} of ${gallery.photosList.length}`
            }
            onKeyDown={(e) => {
              Iif (e.key === "Enter" || e.key === " ") {
                e.preventDefault();
                handlePhotoClick(index);
              }
            }}
          >
            <ThumbnailContainer>
              <img
                src={photo.thumbnailUrl || photo.fullUrl}
                alt={photo.caption || ""}
                loading="lazy"
              />
              <FlagButtonWrapper
                onClick={(e) => {
                  e.stopPropagation();
                }}
                aria-label={t("profile:gallery.report_photo")}
              >
                <FlagButton
                  contentRef={`photo/${photo.itemId}`}
                  authorUser={profileUser.userId}
                />
              </FlagButtonWrapper>
            </ThumbnailContainer>
          </StyledImageListItem>
        ))}
      </StyledImageList>
 
      <PhotoLightbox
        photos={gallery.photosList.map((photo) => ({
          fullUrl: photo.fullUrl,
          thumbnailUrl: photo.thumbnailUrl,
          caption: photo.caption,
          itemId: photo.itemId,
        }))}
        initialIndex={selectedPhotoIndex}
        open={lightboxOpen}
        onClose={handleCloseLightbox}
        galleryOwnerId={profileUser.userId}
      />
    </GalleryContainer>
  );
}