import React from 'react'; import { Flex, Box, color, space, BoxProps } from '@stacks/ui'; import Link from 'next/link'; import { useAppState } from '@common/hooks/use-app-state'; import { SIDEBAR_WIDTH } from '@common/constants'; // @ts-ignore import nav from '@common/navigation.yaml'; import ArrowLeftIcon from 'mdi-react/ArrowLeftIcon'; import { getCategory, getTitle, slugify } from '@common/utils'; import { useRouter } from 'next/router'; import { getCapsizeStyles } from '@components/mdx/typography'; import { Text } from '@components/typography'; import { css } from '@stacks/ui-core'; import { SmartLink } from '@components/mdx'; import { useMobileMenuState } from '@common/hooks/use-mobile-menu'; import { useTheme } from '@emotion/react'; import { HOME } from '@common/constants_that_require_translations'; const Wrapper: React.FC = ({ width = `${SIDEBAR_WIDTH}px`, containerProps, children, ...rest }) => { return ( {children} ); }; const capitalize = s => { if (typeof s !== 'string') return ''; return s.charAt(0).toUpperCase() + s.slice(1); }; const convertToTitle = (path: string) => !path ? null : path === '/' ? HOME : capitalize(path.replace('/', '').replace(/-/g, ' ')); const PageItem = React.forwardRef( ( { isActive, color: _color = color('text-caption'), children, mb = space('base'), isTopLevel, ...props }: any, ref: any ) => { const typeStyles = isTopLevel ? getCapsizeStyles(16, 26) : getCapsizeStyles(14, 20); const styleProps = { outline: '0', display: 'block', color: isActive ? color('accent') : isTopLevel ? color('text-title') : _color, mb: isTopLevel ? space('base-loose') : mb, }; return ( {children} ); } ); const SectionTitle: React.FC = ({ children, ...rest }) => ( {children} ); const getRoutePath = (path, routes) => routes.find(route => route.path.endsWith(path)); const ChildPages = ({ items, handleClick }: any) => { const { routes } = useAppState(); const { handleClose } = useMobileMenuState(); return items?.pages ? items?.pages?.map((page, key) => { if (page.external) { return ( handleClose()} target="_blank" > {page.external.title} ); } const path = page.pages ? `${page.path}${page.pages[0].path}` : items.path ? `/${slugify(items.path)}${page.path}` : page.path; const router = useRouter(); const routePath = routes.find(route => route.path.endsWith(path)); const route = getRoutePath(path, routes); return ( { handleClick(page); handleClose(); } : handleClose } as="a" > {items.usePageTitles ? getTitle(route) : convertToTitle(page.path)} ); }) : null; }; const ChildSection: React.FC = ({ sections, ...rest }) => sections?.map((section, key) => { return ( {section.title} ); }); const BackItem = props => ( Back ); const Navigation = () => { const { routes } = useAppState(); const [selected, setSelected] = React.useState({ type: 'default', items: nav.sections, selected: undefined, }); const { handleClose } = useMobileMenuState(); const router = useRouter(); React.useEffect(() => { let currentSection; if (router.pathname === '/') { currentSection = { items: nav.sections, type: 'default', }; } else { nav.sections.forEach(section => { section.pages.forEach(page => { if (page.pages) { const pagesFound = page.pages.find(_page => { return router.pathname.endsWith(`${page.path}${_page.path}`); }); const sectionsFound = page?.sections?.find(_section => { return _section.pages.find(_page => { return router.pathname.endsWith(`${page.path}${_page.path}`); }); }); if (pagesFound || sectionsFound) { currentSection = { type: 'page', items: page, }; } } else if (!currentSection && router.pathname.endsWith(page.path)) { currentSection = { items: nav.sections, type: 'default', }; } }); }); } if (currentSection?.items && selected.items !== currentSection.items) { setSelected(currentSection); } }, [router.pathname]); const handleClick = (page: any) => { if (page.pages) { setSelected({ type: 'page', items: page, }); } handleClose(); }; const handleBack = () => setSelected({ type: 'default', items: nav.sections, }); if (selected.type === 'page') { return ( {convertToTitle(selected.items.path)} {selected.items ? : null} {selected.items?.sections ? ( ({ ...section, path: selected.items.path, }))} /> ) : null} ); } if (selected.type === 'default') { const urlCategory = getCategory(router.pathname); return selected.items.map((section, i) => { return ( {section.title ? ( {section.title} ) : null} {section.pages.map((page, key) => { const path = page.pages ? `${page.path}${page.pages[0].path}` : section?.title ? `/${slugify(section?.title)}${page.path}` : page.path; const route = getRoutePath(path, routes); return ( handleClick(page)} > {section.usePageTitles ? getTitle(route) : convertToTitle(page.path)} ); })} ); }); } return null; }; export const SideNav: React.FC = ({ containerProps, ...rest }) => { return ( ); };