import { IconDefinition } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import React, { FunctionComponent, useContext, useMemo } from "react"; import { Badge, Collapse, ListGroupItem } from "react-bootstrap"; import { NavLink } from "react-router-dom"; import { siteChangeSidebar } from "../@redux/actions"; import { useReduxAction, useReduxStore } from "../@redux/hooks/base"; import { SidebarToggleContext } from "../App"; import { BadgeProvider, ChildBadgeProvider, CollapseItemType, LinkItemType, } from "./types"; export const HiddenKeysContext = React.createContext([]); export const BadgesContext = React.createContext({}); function useToggleSidebar() { return useReduxAction(siteChangeSidebar); } function useSidebarKey() { return useReduxStore((s) => s.site.sidebar); } export const LinkItem: FunctionComponent = ({ link, name, icon, }) => { const badges = useContext(BadgesContext); const toggle = useContext(SidebarToggleContext); const badgeValue = useMemo(() => { let badge: Nullable = null; if (name in badges) { let item = badges[name]; if (typeof item === "number") { badge = item; } } return badge; }, [badges, name]); return ( ); }; export const CollapseItem: FunctionComponent = ({ icon, name, children, }) => { const badges = useContext(BadgesContext); const hiddenKeys = useContext(HiddenKeysContext); const toggleSidebar = useContext(SidebarToggleContext); const sidebarKey = useSidebarKey(); const updateSidebar = useToggleSidebar(); const [badgeValue, childValue] = useMemo< [Nullable, Nullable] >(() => { let badge: Nullable = null; let child: Nullable = null; if (name in badges) { const item = badges[name]; if (typeof item === "number") { badge = item; } else if (typeof item === "object") { badge = 0; child = item; for (const it in item) { badge += item[it]; } } } return [badge, child]; }, [badges, name]); const active = useMemo(() => sidebarKey === name, [sidebarKey, name]); const collapseBoxClass = useMemo( () => `sidebar-collapse-box ${active ? "active" : ""}`, [active] ); const childrenElems = useMemo( () => children .filter((v) => !hiddenKeys.includes(v.hiddenKey ?? "")) .map((ch) => { let badge: Nullable = null; if (childValue && ch.name in childValue) { badge = childValue[ch.name]; } return ( ); }), [children, hiddenKeys, childValue, toggleSidebar] ); if (childrenElems.length === 0) { return null; } return (
{ if (active) { updateSidebar(""); } else { updateSidebar(name); } }} >
{childrenElems}
); }; interface DisplayProps { name: string; icon?: IconDefinition; badge?: number; } const DisplayItem: FunctionComponent = ({ name, icon, badge, }) => ( {icon && ( )} {name} {badge} );