2022-12-03 20:02:18 +00:00
|
|
|
import styles from '/styles/ReadyOrNot.module.css'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import useSWR from 'swr';
|
|
|
|
import ReadyOrNotMap from '../interfaces/ReadyOrNot';
|
|
|
|
import React, { useState } from 'react';
|
2022-12-04 15:42:49 +00:00
|
|
|
import Image from 'next/image';
|
|
|
|
import useWindowSize from '../components/windowsize';
|
2022-12-03 20:02:18 +00:00
|
|
|
|
|
|
|
const fetcher = (url: string) => fetch(url).then((res) => res.json())
|
|
|
|
|
|
|
|
function stopPropagation(e: any) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sidebar = () => {
|
2022-12-04 15:42:49 +00:00
|
|
|
const isMobile = useWindowSize();
|
2022-12-03 20:02:18 +00:00
|
|
|
const router = useRouter();
|
2022-12-09 17:29:47 +00:00
|
|
|
const [active, setActive] = useState(isMobile);
|
2022-12-09 20:38:23 +00:00
|
|
|
const { maps, isLoading, isError }: { maps: ReadyOrNotMap[], isLoading: boolean, isError: boolean } = useNavbar();
|
2022-12-03 20:02:18 +00:00
|
|
|
|
2022-12-09 20:38:23 +00:00
|
|
|
if (typeof (isMobile) === "boolean" && typeof (active) === "undefined") {
|
2022-12-09 17:29:47 +00:00
|
|
|
setActive(!isMobile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isError) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div><nav><a>Error loading Sidemenu</a></nav></div>
|
|
|
|
<div className={styles.sidebarPlaceholder}></div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2022-12-03 20:02:18 +00:00
|
|
|
else if (isLoading) {
|
|
|
|
return (
|
2022-12-09 17:29:47 +00:00
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<nav>
|
|
|
|
<a>Loading...</a>
|
|
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
<div className={styles.sidebarPlaceholder}></div>
|
|
|
|
</>
|
2022-12-03 20:02:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (
|
2022-12-09 17:29:47 +00:00
|
|
|
<>
|
|
|
|
<div className={styles.sidebar} onClick={() => setActive(!active)}>
|
2022-12-09 20:38:23 +00:00
|
|
|
<div className={[styles.sl_wrapper, (active ? styles.sl_active : styles.sl_inactive)].join(" ")}>
|
|
|
|
<nav className={styles.sidebarList}>
|
|
|
|
{maps.map((item) => (
|
|
|
|
<Link key={item.name} href={item.href}>
|
|
|
|
<a className={[styles.navElem, (router.query.map == item.href ? styles.ne_active : styles.ne_inactive)].join(" ")} onClick={stopPropagation}>{item.name}</a>
|
|
|
|
</Link>
|
|
|
|
))}
|
|
|
|
</nav>
|
|
|
|
</div>
|
2022-12-09 17:29:47 +00:00
|
|
|
<div className={styles.sidebarArrow}>
|
|
|
|
<Image src={active ? "/sidebar_arrow_flipped.webp" : "/sidebar_arrow.webp"} width={32} height={96} alt={active ? ">" : "<"} />
|
|
|
|
</div>
|
2022-12-03 20:02:18 +00:00
|
|
|
</div>
|
2022-12-09 17:29:47 +00:00
|
|
|
<div className={styles.sidebarPlaceholder}></div>
|
|
|
|
</>
|
2022-12-03 20:02:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function useNavbar() {
|
|
|
|
const { data, error } = useSWR(`/api/navbar`, fetcher)
|
|
|
|
|
|
|
|
return {
|
|
|
|
maps: data,
|
|
|
|
isLoading: !error && !data,
|
|
|
|
isError: error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Sidebar;
|