import type { SidebarItemType } from "@/providers/sidebar" import { useSidebar } from "@/providers/sidebar" import clsx from "clsx" import dynamic from "next/dynamic" import Link from "next/link" import { useEffect, useMemo, useRef, useState } from "react" import type { MethodLabelProps } from "../../MethodLabel" import checkSidebarItemVisibility from "@/utils/check-sidebar-item-visibility" import Loading from "../../Loading" const MethodLabel = dynamic( async () => import("../../MethodLabel") ) as React.FC export type SidebarItemProps = { item: SidebarItemType nested?: boolean } & React.AllHTMLAttributes const SidebarItem = ({ item, nested = false, className }: SidebarItemProps) => { const [showLoading, setShowLoading] = useState(false) const { isItemActive, setMobileSidebarOpen: setSidebarOpen } = useSidebar() const active = useMemo(() => { return isItemActive(item, nested) }, [isItemActive, item, nested]) const collapsed = !isItemActive(item, true) const ref = useRef(null) useEffect(() => { if (active && ref.current && window.innerWidth >= 1025) { if ( !checkSidebarItemVisibility(ref.current, { topMargin: 57, }) ) { // scroll to element ref.current.scrollIntoView({ block: "center", }) } } if (active) { setShowLoading(true) } }, [active]) return (
  • { if (window.innerWidth < 1025) { setSidebarOpen(false) } }} replace shallow > {item.title} {item.method && } {item.hasChildren && (
      {showLoading && !item.loaded && ( )} {item.children?.map((childItem, index) => ( ))}
    )}
  • ) } export default SidebarItem