import { IconDefinition } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { setSidebar } from "@redux/actions"; import { useReduxAction, useReduxStore } from "@redux/hooks/base"; import logo from "@static/logo64.png"; import React, { createContext, FunctionComponent, useContext, useMemo, useState, } from "react"; import { Badge, Collapse, Container, Image, ListGroup, ListGroupItem, } from "react-bootstrap"; import { NavLink, useHistory, useRouteMatch } from "react-router-dom"; import { BuildKey } from "utilities"; import { useGotoHomepage } from "utilities/hooks"; import { useNavigationItems } from "../Navigation"; import { Navigation } from "../Navigation/nav"; import "./style.scss"; const SelectionContext = createContext<{ selection: string | null; select: (selection: string | null) => void; }>({ selection: null, select: () => {} }); const Sidebar: FunctionComponent = () => { const open = useReduxStore((s) => s.showSidebar); const changeSidebar = useReduxAction(setSidebar); const cls = ["sidebar-container"]; const overlay = ["sidebar-overlay"]; if (open) { cls.push("open"); overlay.push("open"); } const goHome = useGotoHomepage(); const [selection, setSelection] = useState(null); return (
changeSidebar(false)} >
); }; const SidebarNavigation: FunctionComponent = () => { const navItems = useNavigationItems(); return ( {navItems.map((v, idx) => { if ("routes" in v) { return ( ); } else { return ( ); } })} ); }; const SidebarParent: FunctionComponent = ({ icon, badge, name, path, routes, enabled, component, }) => { const computedBadge = useMemo(() => { let computed = badge ?? 0; computed += routes.reduce((prev, curr) => { return prev + (curr.badge ?? 0); }, 0); return computed !== 0 ? computed : undefined; }, [badge, routes]); const enabledRoutes = useMemo( () => routes.filter((v) => v.enabled !== false && v.routeOnly !== true), [routes] ); const changeSidebar = useReduxAction(setSidebar); const { selection, select } = useContext(SelectionContext); const match = useRouteMatch({ path }); const open = match !== null || selection === path; const collapseBoxClass = useMemo( () => `sidebar-collapse-box ${open ? "active" : ""}`, [open] ); const history = useHistory(); if (enabled === false) { return null; } else if (enabledRoutes.length === 0) { if (component) { return ( changeSidebar(false)} > ); } else { return null; } } return (
{ if (open) { select(null); } else { select(path); } if (component !== undefined) { history.push(path); } }} >
{enabledRoutes.map((v, idx) => ( ))}
); }; interface SidebarChildProps { parent: string; } const SidebarChild: FunctionComponent< SidebarChildProps & Navigation.RouteWithoutChild > = ({ icon, name, path, badge, enabled, routeOnly, parent }) => { const changeSidebar = useReduxAction(setSidebar); const { select } = useContext(SelectionContext); if (enabled === false || routeOnly === true) { return null; } return ( { select(null); changeSidebar(false); }} > ); }; const SidebarContent: FunctionComponent<{ icon?: IconDefinition; name: string; badge?: number; }> = ({ icon, name, badge }) => { return ( {icon && ( )} {name} {badge !== 0 ? badge : null} ); }; export default Sidebar;