import type { NextPage } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import type { ChangeEvent, ChangeEventHandler } from "react"; import { useEffect } from "react"; import styles from "../../styles/Blog/DiaryPageSelector.module.scss"; interface IContent { pages: string[]; name: string; title: string; pageSelected: number } interface IContentNavBar extends IContent { mobile?: boolean; } interface IContentNav extends IContent { bottom?: boolean; } const PageSelectorBar: NextPage = ({ pages, name, title, pageSelected, mobile }) => { const router = useRouter(); // When we are on the main page no previous page exists, otherwise we need to check if the previous page is the main page const prevLink = pageSelected === 0 ? null : pageSelected > 1 ? `/blog/diary/${name}/${pageSelected - 1}` : `/blog/diary/${name}`; const nextLink = pageSelected < pages.length ? `/blog/diary/${name}/${pageSelected + 1}` : null; useEffect(() => { if (prevLink) router.prefetch(prevLink); if (nextLink) router.prefetch(nextLink); }, [nextLink, prevLink, router]); const onSelection: ChangeEventHandler = async (e: ChangeEvent) => { const selected = Number.parseInt((e.target as HTMLSelectElement).value) || 0; const link = selected === 0 ? `/blog/diary/${name}` : `/blog/diary/${name}/${selected}`; await router.push(link); }; const prev = {prevLink ? < {pageSelected - 1 === 0 ? `${title} (Main Page)` : pages[pageSelected - 2]} : null}; const next = {nextLink ? {pages[pageSelected]} > : null}; const select = ( ); const classNames = mobile ? `${styles.barNav} ${styles.mobile}` : styles.barNav; return (
{prev}   |   {select}   |   {next}
); }; const PageSelector: NextPage = (content) => { if (content.bottom) return ; return (

{content.title}

    {content.pages.map((e, idx) =>
  • {e}
  • )}
); }; export default PageSelector;