2022-05-18 10:24:24 +00:00
|
|
|
import {FC, PropsWithChildren, useEffect, useState} from "react";
|
2022-04-21 11:02:39 +00:00
|
|
|
import {INavigationItemProps} from "../types/data.types";
|
2022-05-12 14:07:26 +00:00
|
|
|
import Link from "next/link";
|
2022-05-18 10:24:24 +00:00
|
|
|
import CloseIcon from "/public/assets/sidebar-icon-close.svg";
|
2022-05-12 14:07:26 +00:00
|
|
|
|
|
|
|
const sidebar: INavigationItemProps = require("../public/compiled/sidebar.tree.min.json");
|
2022-04-21 11:02:39 +00:00
|
|
|
|
2022-05-12 14:07:26 +00:00
|
|
|
interface ISidebarProps{
|
2022-05-18 10:24:24 +00:00
|
|
|
className?: string;
|
|
|
|
onClose?: () => void;
|
|
|
|
onOpen?: () => void;
|
|
|
|
initialHide?: boolean;
|
|
|
|
hide: boolean;
|
2022-05-12 14:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IMenuProps{
|
|
|
|
items: INavigationItemProps[]
|
|
|
|
level?: number
|
|
|
|
}
|
2022-04-11 17:15:04 +00:00
|
|
|
|
2022-05-12 14:07:26 +00:00
|
|
|
interface IMenuItemProps{
|
|
|
|
item: INavigationItemProps
|
|
|
|
level?: number
|
2022-05-11 15:43:23 +00:00
|
|
|
}
|
2022-04-11 17:15:04 +00:00
|
|
|
|
2022-05-16 09:26:17 +00:00
|
|
|
const MenuItem: FC<PropsWithChildren<IMenuItemProps>> = ({level, item, children}) => (
|
2022-05-12 14:07:26 +00:00
|
|
|
<li className={`menuitem level-${level}`}>
|
|
|
|
<div className={"menuitem-title"}>
|
2022-05-16 09:26:17 +00:00
|
|
|
{
|
|
|
|
item.children.length?
|
|
|
|
<b>{item.title}</b>
|
|
|
|
:
|
|
|
|
<Link href={`/${item.path.join("/")}`}>
|
|
|
|
<a>
|
2022-05-12 14:07:26 +00:00
|
|
|
<span>{item.title}</span>
|
2022-05-16 09:26:17 +00:00
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
}
|
2022-05-12 14:07:26 +00:00
|
|
|
</div>
|
|
|
|
{
|
|
|
|
children
|
|
|
|
}
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
|
|
|
|
const Menu: FC<IMenuProps> = (props) => {
|
|
|
|
const {items, level = 0} = props;
|
|
|
|
return(
|
|
|
|
<ul className={"sidebar-menu"}>
|
|
|
|
{items.map((item) => {
|
|
|
|
return (
|
|
|
|
<MenuItem item={item}
|
|
|
|
level={level}
|
|
|
|
key={item.localPath}
|
|
|
|
>
|
|
|
|
<Menu items={item.children} level={level+1}/>
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Sidebar: FC<ISidebarProps> = (props) => {
|
2022-05-18 10:24:24 +00:00
|
|
|
const {className = "", onClose = ()=>{}, onOpen = () => {}, initialHide = false, hide} = props;
|
2022-05-12 14:07:26 +00:00
|
|
|
const mainItems = sidebar.children.filter((c) => c.children.length===0)
|
|
|
|
const subItems = sidebar.children.filter((c) => c.children.length!==0);
|
2022-05-18 10:24:24 +00:00
|
|
|
const [firstTime, setFirstTime] = useState(false);
|
|
|
|
|
|
|
|
const isOpen = !((!firstTime&&initialHide) || hide);
|
|
|
|
const hideClass = isOpen? "":"hide"
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if(hide){
|
|
|
|
setFirstTime(true);
|
|
|
|
}
|
|
|
|
if(isOpen){
|
|
|
|
onOpen();
|
|
|
|
}
|
|
|
|
}, [hide, isOpen])
|
2022-05-12 14:07:26 +00:00
|
|
|
|
2022-04-11 17:15:04 +00:00
|
|
|
return (
|
2022-05-18 10:24:24 +00:00
|
|
|
<div className={`${className} ${hideClass}`}>
|
|
|
|
<nav className={`sidebarNav`}>
|
|
|
|
<div className={"sidebar-close-icon button"} onClick={onClose}>
|
|
|
|
<CloseIcon/>
|
|
|
|
</div>
|
|
|
|
<Menu items={[{...sidebar, title: "", children: mainItems}]}/>
|
|
|
|
<br/>
|
|
|
|
<Menu items={subItems}/>
|
|
|
|
</nav>
|
|
|
|
</div>
|
2022-04-11 17:15:04 +00:00
|
|
|
)
|
|
|
|
}
|