import * as React from 'react'; import { ContainerHeader, HeaderSection, ItemAvatar, MenuSection, Group, } from '@atlaskit/navigation-next'; import { gridSize as gridSizeFn } from '@atlaskit/theme'; import titleCase from 'title-case'; import LinkWithRouter from './link-with-router'; import TreeNavContent, { arrayToTreeItems } from './tree-nav-content'; const gridSize = gridSizeFn(); export type Data = { packageTitle?: string; packageId: string; homePath: string; parentId?: string; }[]; export type Props = { data: Data; }; // gets all the packages with parentId as the param id passed const getChildrenArray = (array: Data, id?: string) => array .filter(y => y.parentId === id) .map(x => ({ id: x.packageTitle || x.packageId, pagePath: x.homePath })); // @ts-ignore // transforms the nested folders into the format of {id , children} required for nav to build data const transformToNestedData = (arr, data, parentId) => { const [first, ...rest] = arr; return { id: first, parent: [parentId], children: first ? [transformToNestedData(rest, data, parentId)] : getChildrenArray(data, parentId), }; }; // @ts-ignore // loop through each child of the existingParent until you get a matching child for every nested folder in package const getExistingPackages = (nested, originalChildren) => { let nestedChild; let originalChild; for (let i = 0; i < originalChildren && originalChildren.length; i += 1) { const a = nested.children && nested.children.find((y: { id: any }) => y.id === originalChildren[i].id); if (a) { nestedChild = a; originalChild = originalChildren[i]; break; } } if (nestedChild) { getExistingPackages(nestedChild, originalChild.children); } else { originalChildren.push(...nested.children); } }; const AllPackagesNavContent = ({ data }: Props) => { const dataToConvert = data.filter(y => y.parentId); const packagesWithParentIds: any = []; dataToConvert.forEach(parent => { if ( packagesWithParentIds.findIndex((p: { parent: string[] }) => p.parent.some(x => x === parent.parentId), ) < 0 ) { const nestedFolders = parent.parentId && parent.parentId.split('/'); if (nestedFolders && nestedFolders.length > 1) { const nestedData = transformToNestedData( nestedFolders, dataToConvert, parent.parentId, ); const existingParent = packagesWithParentIds.find( (p: { id: string }) => p.id === nestedData.id, ); if (existingParent) { existingParent.parent.push(...nestedData.parent); getExistingPackages(nestedData, existingParent.children); } else { packagesWithParentIds.push(nestedData); } } else { packagesWithParentIds.push({ id: parent.parentId, parent: [parent.parentId], children: getChildrenArray(dataToConvert, parent.parentId), }); } } }); const treeData = packagesWithParentIds.length > 0 && { rootId: 'packages', items: packagesWithParentIds ? arrayToTreeItems(packagesWithParentIds, { parentId: 'packages', parentTitle: 'Packages', }) : {}, }; return ( <> {/* TODO: TSFix nav typing */} {({ css }: { css: {} }) => ( ( )} text="Brisk Documentation" /> )} {/* TODO: TSFix nav typing */} {({ className }: { className: string }) => ( {/* TODO: Hiding for the time being */} {/* */} {/* */} {data .filter(x => !x.parentId) .map(pkg => ( ))} {treeData && } )} > ); }; export default AllPackagesNavContent;