Skip to content

Commit

Permalink
Gallery props cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelfan committed Dec 17, 2024
1 parent 47cc473 commit 9be4979
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/components/contentDisplay/profileHeader/ProfileHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ export default function ProfileHeader(props: Props) {

{showAvatar && profile.avatar && (
<Gallery
images={profile.avatar}
images={[{ src: profile.avatar }]}
onClose={() => setShowAvatar(false)}
/>
)}
{showBanner && profile.banner && (
<Gallery
images={profile.banner}
images={[{ src: profile.banner }]}
onClose={() => setShowBanner(false)}
/>
)}
Expand Down
50 changes: 21 additions & 29 deletions src/components/dataDisplay/gallery/Gallery.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Image from "next/image";
import { AppBskyEmbedImages } from "@atproto/api";
import * as Dialog from "@radix-ui/react-dialog";
import { KeyboardEvent, useState, useEffect, useCallback } from "react";
import Button from "@/components/actions/button/Button";
Expand All @@ -10,15 +9,24 @@ import {
BiSolidCloudDownload,
} from "react-icons/bi";

type GalleryImage = {
src: string;
alt?: string;
aspectRatio?: {
width: number;
height: number;
};
};

interface Props {
images: AppBskyEmbedImages.ViewImage[] | string;
images: GalleryImage[];
startingIndex?: number;
onClose: () => void;
}

export default function Gallery(props: Props) {
const { images, startingIndex = 0, onClose } = props;
const imageCount = Array.isArray(images) ? images.length : 0;
const imageCount = images.length;
const [currentIndex, setCurrentIndex] = useState(startingIndex);

const handleBackward = useCallback(() => {
Expand Down Expand Up @@ -47,13 +55,11 @@ export default function Gallery(props: Props) {
break;
}
},
[handleBackward, handleForward],
[handleBackward, handleForward]
);

const handleSaveImage = () => {
const imageUrl = Array.isArray(images)
? images[currentIndex].fullsize
: images;
const imageUrl = images[currentIndex].src;
const a = document.createElement("a");
a.href = imageUrl;
a.click();
Expand Down Expand Up @@ -121,28 +127,14 @@ export default function Gallery(props: Props) {
</Button>
)}

{/* Image Display */}
{Array.isArray(images) && (
<Image
src={images[currentIndex].fullsize}
alt={images[currentIndex].alt}
width={images[currentIndex].aspectRatio?.width ?? 900}
height={images[currentIndex].aspectRatio?.height ?? 900}
priority
className="fixed inset-0 z-[60] mx-auto h-full w-fit object-contain"
/>
)}

{typeof images === "string" && (
<Image
src={images}
alt={"Image"}
width={900}
height={900}
priority
className="fixed inset-0 z-[60] mx-auto h-full w-fit object-contain"
/>
)}
<Image
src={images[currentIndex].src}
alt={images[currentIndex].alt ?? ""}
width={images[currentIndex].aspectRatio?.width ?? 900}
height={images[currentIndex].aspectRatio?.height ?? 900}
priority
className="fixed inset-0 z-[60] mx-auto h-full w-fit object-contain"
/>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
Expand Down
28 changes: 16 additions & 12 deletions src/components/dataDisplay/postEmbed/ImageEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ interface Props {
export default function ImageEmbed(props: Props) {
const { content, depth } = props;
const imageCount = content.images.length;
const [showImage, setShowImage] = useState<number | undefined>(undefined);
const [currentImage, setCurrentImage] = useState<number>();

const generateImageLayout = (
count: number,
images: AppBskyEmbedImages.ViewImage[],
images: AppBskyEmbedImages.ViewImage[]
) => {
// adjust image grid layout based on number of images
switch (count) {
Expand All @@ -36,7 +36,7 @@ export default function ImageEmbed(props: Props) {
className="rounded-md h-full max-h-62 object-cover cursor-pointer hover:brightness-90 border border-skin-base"
onClick={(e) => {
e.stopPropagation();
setShowImage(i);
setCurrentImage(i);
}}
/>
{image.alt && <AltTag text={image.alt} />}
Expand All @@ -60,7 +60,7 @@ export default function ImageEmbed(props: Props) {
className="rounded-md object-cover h-full cursor-pointer hover:brightness-90 border border-skin-base"
onClick={(e) => {
e.stopPropagation();
setShowImage(0);
setCurrentImage(0);
}}
/>
{images[2].alt && <AltTag text={images[2].alt} />}
Expand All @@ -78,7 +78,7 @@ export default function ImageEmbed(props: Props) {
className="rounded-md object-cover w-full h-full cursor-pointer hover:brightness-90 border border-skin-base"
onClick={(e) => {
e.stopPropagation();
setShowImage(1);
setCurrentImage(1);
}}
/>
{images[1].alt && <AltTag text={images[1].alt} />}
Expand All @@ -94,7 +94,7 @@ export default function ImageEmbed(props: Props) {
className="rounded-md object-cover w-full h-full cursor-pointer hover:brightness-90 border border-skin-base"
onClick={(e) => {
e.stopPropagation();
setShowImage(2);
setCurrentImage(2);
}}
/>
{images[2].alt && <AltTag text={images[2].alt} />}
Expand All @@ -117,7 +117,7 @@ export default function ImageEmbed(props: Props) {
className="object-cover aspect-square rounded-md h-full max-h-64 cursor-pointer hover:brightness-90 border border-skin-base"
onClick={(e) => {
e.stopPropagation();
setShowImage(i);
setCurrentImage(i);
}}
/>
{images[i].alt && <AltTag text={images[i].alt} />}
Expand All @@ -140,7 +140,7 @@ export default function ImageEmbed(props: Props) {
className="rounded-md max-h-96 w-full object-cover cursor-pointer hover:brightness-90 border border-skin-base"
onClick={(e) => {
e.stopPropagation();
setShowImage(0);
setCurrentImage(0);
}}
/>
{images[0].alt && <AltTag text={images[0].alt} />}
Expand All @@ -153,11 +153,15 @@ export default function ImageEmbed(props: Props) {

return (
<>
{showImage !== undefined && (
{currentImage !== undefined && (
<Gallery
images={content.images}
startingIndex={showImage}
onClose={() => setShowImage(undefined)}
images={content.images.map((img) => ({
src: img.fullsize,
alt: img.alt,
aspectRatio: img.aspectRatio,
}))}
startingIndex={currentImage}
onClose={() => setCurrentImage(undefined)}
/>
)}
{depth < 2 && (
Expand Down

0 comments on commit 9be4979

Please sign in to comment.