2022-10-08 13:27:09 +02:00
|
|
|
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";
|
2022-10-15 22:17:51 +02:00
|
|
|
import type { DiaryEntry } from "../../lib/content/types";
|
2022-10-08 13:27:09 +02:00
|
|
|
|
|
|
|
import styles from "../../styles/Blog/DiaryPageSelector.module.scss";
|
|
|
|
|
2022-10-08 13:35:40 +02:00
|
|
|
interface IContent {
|
2022-10-08 13:27:09 +02:00
|
|
|
name: string;
|
|
|
|
title: string;
|
|
|
|
pageSelected: number
|
|
|
|
}
|
|
|
|
|
2022-10-08 13:35:40 +02:00
|
|
|
interface IContentNavBar extends IContent {
|
|
|
|
mobile?: boolean;
|
2022-10-18 14:05:45 +02:00
|
|
|
bottom?: boolean;
|
2022-10-15 22:17:51 +02:00
|
|
|
pages: string[];
|
2022-10-08 13:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IContentNav extends IContent {
|
2022-10-08 13:27:09 +02:00
|
|
|
bottom?: boolean;
|
2022-10-15 22:17:51 +02:00
|
|
|
pages: DiaryEntry[];
|
2022-10-08 13:27:09 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 14:05:45 +02:00
|
|
|
const PageSelectorBar: NextPage<IContentNavBar> = ({ pages, name, title, pageSelected, mobile, bottom }) => {
|
2022-10-08 13:27:09 +02:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2022-10-23 14:58:10 +02:00
|
|
|
const prev = <span className={styles.leftSelectSpace}>{prevLink ? <Link href={prevLink}><a><<span className={styles.longLink}> {pageSelected - 1 === 0 ? "Main Page" : pages[pageSelected - 2]}</span><span className={styles.shortLink}><</span></a></Link> : null}</span>;
|
|
|
|
const next = <span className={styles.rightSelectSpace}>{nextLink ? <Link href={nextLink}><a><span className={styles.longLink}>{pages[pageSelected]} </span><span className={styles.shortLink}>></span>></a></Link> : null}</span>;
|
2022-10-08 13:27:09 +02:00
|
|
|
|
|
|
|
const select = (
|
|
|
|
<select onChange={onSelection} value={pageSelected}>
|
|
|
|
<option key={-1} value={0}>Main page</option>
|
|
|
|
{pages.map((entry, i) => <option key={i} value={i + 1}>{entry}</option>)}
|
|
|
|
</select>
|
|
|
|
);
|
|
|
|
|
2022-10-18 14:05:45 +02:00
|
|
|
const classNames = `${styles.barNav} ${mobile ? styles.mobile : ""} ${bottom ? styles.bottom : styles.top}`;
|
2022-10-08 13:35:40 +02:00
|
|
|
|
2022-10-08 13:27:09 +02:00
|
|
|
return (
|
2022-10-08 13:35:40 +02:00
|
|
|
<div className={classNames}>
|
2022-10-18 14:05:45 +02:00
|
|
|
<span></span> {/* Spacer */}
|
2022-10-08 13:27:09 +02:00
|
|
|
{prev}
|
|
|
|
<span style={{visibility: prevLink ? "visible" : "hidden"}}> | </span>
|
|
|
|
{select}
|
|
|
|
<span style={{visibility: nextLink ? "visible" : "hidden"}}> | </span>
|
|
|
|
{next}
|
2022-10-18 14:05:45 +02:00
|
|
|
<span></span> {/* Spacer */}
|
2022-10-08 13:27:09 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PageSelector: NextPage<IContentNav> = (content) => {
|
2022-10-15 22:17:51 +02:00
|
|
|
const entries = content.pages.map(p => p.title);
|
2022-10-08 13:27:09 +02:00
|
|
|
|
2022-10-18 14:05:45 +02:00
|
|
|
if (content.bottom) return <PageSelectorBar pages={entries} name={content.name} title={content.title} pageSelected={content.pageSelected} bottom />;
|
2022-10-08 13:27:09 +02:00
|
|
|
|
|
|
|
return (
|
2022-10-08 13:35:40 +02:00
|
|
|
<div className={styles.nav}>
|
2022-10-15 22:17:51 +02:00
|
|
|
<PageSelectorBar pages={entries} name={content.name} title={content.title} pageSelected={content.pageSelected} mobile />
|
2022-10-08 13:35:40 +02:00
|
|
|
|
2022-10-23 14:58:10 +02:00
|
|
|
<aside className={`${styles.sideNav} ${styles.desktop}`}>
|
2022-10-18 14:05:45 +02:00
|
|
|
<Link href={`/blog/diary/${content.name}`}><a><h4 className={content.pageSelected === 0 ? styles.thisPage : undefined}>{content.title}</h4></a></Link>
|
|
|
|
<ol>
|
|
|
|
{entries.map((e, idx) => <li key={idx} className={content.pageSelected - 1 === idx ? styles.thisPage : undefined}><Link href={`/blog/diary/${content.name}/${idx + 1}`}><a>{e}</a></Link></li>)}
|
|
|
|
</ol>
|
2022-10-23 14:58:10 +02:00
|
|
|
</aside>
|
2022-10-08 13:27:09 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PageSelector;
|