diff --git a/src/components/listing-image-gallery/components/Modal.tsx b/src/components/listing-image-gallery/components/Modal.tsx new file mode 100644 index 0000000..07bd44f --- /dev/null +++ b/src/components/listing-image-gallery/components/Modal.tsx @@ -0,0 +1,84 @@ +"use client"; + +import { Dialog } from "@headlessui/react"; +import { motion } from "framer-motion"; +import { usePathname, useRouter, useSearchParams } from "next/navigation"; +import { useRef, useState } from "react"; +import useKeypress from "react-use-keypress"; +import { getNewParam } from "../ListingImageGallery"; +import type { ListingGalleryImage } from "../utils/types"; +import SharedModal from "./SharedModal"; +import { Route } from "next"; + +export default function Modal({ + images, + onClose, +}: { + images: ListingGalleryImage[]; + onClose?: () => void; +}) { + let overlayRef = useRef(null); + const searchParams = useSearchParams(); + const router = useRouter(); + const thisPathname = usePathname(); + const photoId = searchParams?.get("photoId"); + let index = Number(photoId); + + const [direction, setDirection] = useState(0); + const [curIndex, setCurIndex] = useState(index); + + function handleClose() { + onClose && onClose(); + } + + function changePhotoId(newVal: number) { + if (newVal > index) { + setDirection(1); + } else { + setDirection(-1); + } + setCurIndex(newVal); + router.push(`${thisPathname}/?${getNewParam({ value: newVal })}` as Route); + } + + useKeypress("ArrowRight", () => { + if (index + 1 < images.length) { + changePhotoId(index + 1); + } + }); + + useKeypress("ArrowLeft", () => { + if (index > 0) { + changePhotoId(index - 1); + } + }); + + return ( + <> + + + + + + ); +}