import React from 'react'; import { getProductDirectory } from '../../../utils/getLocalDirectory'; import InternalLink from '../../InternalLink'; import { ArrowStyle, DirectoryGroupHeaderStyle, DirectoryGroupItemStyle, DirectoryLinksStyle, ProductRootLinkStyle } from './styles'; import type { DirectoryItem } from '../../../directory/directory'; export type DirectoryGroupProps = { title: string; items: DirectoryItem[]; url: string; filterKey: string; }; export type DirectoryGroupState = { isExpanded: boolean; }; class DirectoryGroup extends React.Component< DirectoryGroupProps, DirectoryGroupState > { itemsToDisplay: DirectoryItem[] = []; currentRoute = ''; shouldDisplay = ({ filters }): boolean => { return ( // the filter key is undefined this.props.filterKey === undefined || // href doesn't have any q/[filter]/[filter]; via ChooseFilterPage this.props.filterKey === 'all' || // this page is available independent of filter filters === undefined || filters.length === 0 || // this page is available in specific filtered versions (one of which is the globally-selected) (filters && filters.includes(this.props.filterKey)) ); }; constructor(props) { super(props); this.initialize(); if ( this.props.items && this.props.items.some(({ route }) => this.currentRoute.startsWith(route)) ) { this.state = { isExpanded: true }; } else { this.state = { isExpanded: this.props.url.startsWith('/start') }; } } initialize = () => { this.itemsToDisplay = this.props.items.filter(this.shouldDisplay); this.currentRoute = this.props.url.split('/q/').shift() as string; }; toggleOpen = () => { this.setState(({ isExpanded }) => { return { isExpanded: !isExpanded }; }); }; render() { this.initialize(); if (this.itemsToDisplay.length === 0) { return <>; } return (

{this.props.title}

{this.state.isExpanded && ( {this.itemsToDisplay.map((item) => ( {item.isCodeTitle ? ( {item.title} ) : ( item.title )}
))}
)}
); } } type DirectoryProps = { url: string; filterKey: string; }; export default class Directory extends React.Component { render() { const directory = getProductDirectory(this.props.url) as { productRoot: { title: string; route: string }; items: { title: string; items: DirectoryItem[] }[]; }; const productRoot = directory.productRoot; return (
{productRoot.title} {Object.entries(directory.items).map((folderName) => ( ))}
); } }