Skip to content

Commit

Permalink
added: manual sorting for developers menu
Browse files Browse the repository at this point in the history
  • Loading branch information
HZ991 committed Sep 30, 2024
1 parent ff62c6d commit f454cfc
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useState } from 'react';

import { formatComponentSlug } from '@/utils/format';
import { formatComponentSlug, formatNavItems, sortDeveloperMenu, sortMenu } from '@/utils/format';

import { useSidebar } from '../../sidebar.context';

Expand All @@ -29,6 +29,14 @@ export function Navigation({ items, brand }: NavigationProps) {
function List({ items, level = 0, crumbs, brand }: ListProps) {
return items.map(item => {
if (item.children) {
if (item.label.toLowerCase() === 'developers') {
const formattedItems = sortDeveloperMenu(item.children);
return (
<Group key={item.label} label={item.label} level={level} crumbs={crumbs}>
<List items={formattedItems} level={level + 1} crumbs={crumbs} brand={brand} />
</Group>
);
}
return (
<Group key={item.label} label={item.label} level={level} crumbs={crumbs}>
<List items={item.children} level={level + 1} crumbs={crumbs} brand={brand} />
Expand Down
28 changes: 19 additions & 9 deletions apps/site/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ export function formatNavItems(navList: { name: string; slug: string }[]) {
return navItems;
}

function orderMenu(menuItems: Item[], order: string[]) {
const orderedMenu: Item[] = [];
order.forEach(item =>
menuItems.forEach(menuItem => {
if (item === menuItem.label.toLowerCase()) {
orderedMenu.push(menuItem);
}
}),
);
return orderedMenu;
}

// This had to be made as a separate function as adding some to formatNavItems too cognitively complex
export function sortMenu(menuItems: Item[]) {
const topLevelMenuOrder = [
Expand All @@ -46,13 +58,11 @@ export function sortMenu(menuItems: Item[]) {
'design tokens',
];

const orderedMenu: Item[] = [];
topLevelMenuOrder.forEach(item =>
menuItems.forEach(menuItem => {
if (item === menuItem.label.toLowerCase()) {
orderedMenu.push(menuItem);
}
}),
);
return orderedMenu;
return orderMenu(menuItems, topLevelMenuOrder);
}

export function sortDeveloperMenu(menuItems: Item[]) {
const developersMenuOrder = ['set up', 'using components', 'using brands', 'eslint configuration', 'unit testing'];

return orderMenu(menuItems, developersMenuOrder);
}

0 comments on commit f454cfc

Please sign in to comment.