import { PropsOf, chakra, useColorModeValue, Flex } from '@chakra-ui/react' import NextLink from 'next/link' import { useRouter } from 'next/router' import { forwardRef, Ref, useEffect, useRef } from 'react' const StyledLink = forwardRef(function StyledLink( props: PropsOf & { isActive?: boolean isExternal?: boolean }, ref: Ref, ) { const { isActive, isExternal = false, ...rest } = props return ( ) }) type SidebarLinkProps = PropsOf & { href?: string icon?: React.ReactElement isExternal?: boolean } function checkHref(href: string, slug: string | string[]) { const _slug = Array.isArray(slug) ? slug : [slug] const path = href.split('/') const pathSlug = path[path.length - 1] return _slug.includes(pathSlug) } const SidebarLink = ({ href, children, isExternal = false, ...rest }: SidebarLinkProps) => { const router = useRouter() const isActive = checkHref(href, router.query.slug) || href === router.asPath const link = useRef() useEffect(() => { if (isActive && router.query.scroll === 'true') { link.current.scrollIntoView({ block: 'center' }) } }, [isActive, router.query]) return ( {children} ) } export default SidebarLink