[id].js 2.13 KB
Newer Older
PLN committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import { getPhotoById, getAllPhotos } from "../../lib/utils";
import {
  Box,
  Divider,
  Center,
  Text,
  Flex,
  Spacer,
  Button,
} from "@chakra-ui/react";
import Image from "next/image";
import Head from "next/head";
import Link from "next/link";
import BackHome from "../../components/BackHome";
15
import NavLinks from "../../components/NavLinks";
PLN committed
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
import { InfoIcon, AtSignIcon } from "@chakra-ui/icons";
import { Img } from "@chakra-ui/react";

export async function getStaticPaths() {
  const photos = await getAllPhotos();

  // Get the paths we want to pre-render based on posts
  const paths = photos.map((photo) => ({
    params: { id: "" + photo.id },
  }));

  return {
    paths: paths, // render page for all photos
    fallback: false, // ...and generate a 404 for other values
  };
}

export async function getStaticProps({ params }) {
  const pic = await getPhotoById(params.id);

  // Fix dimensions if really big picture
  console.log("Pic dims: ", pic.dimensions.width, pic.dimensions.height);
  if (pic.dimensions.widths > 1000 || pic.dimensions.heigth > 1000) {
    pic.dimensions.width /= 2;
    pic.dimensions.height = 2;
    console.log("Resized: ", pic.dimensions.width, pic.dimensions.height);
  }
  if (pic.dimensions.width > 1000 || pic.dimensions.heigth > 1000) {
    pic.dimensions.width /= 2;
    pic.dimensions.height /= 2;
    console.log("Resized: ", pic.dimensions.width, pic.dimensions.height);
  }
  return {
    props: {
      pic,
    },
  };
}

export default function Photos({ pic }) {
  return (
    <Box p="2rem" bg="gray.200" minH="100vh">
      <Head>
        <title> Photo {pic.id}</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <BackHome />
64
      <NavLinks prev={pic.prev} next={pic.next} />
PLN committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78

      <Center>
        <a href={pic.path}>
          <Image
            src={pic.path}
            width={pic.dimensions.width || 480}
            height={pic.dimensions.height || 480}
            // objectFit="cover"
            quality={80}
            priority
            loading="eager"
          />
        </a>
      </Center>
79
      <NavLinks prev={pic.prev} next={pic.next} />
PLN committed
80 81 82
    </Box>
  );
}