Clean up collectLeaves
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Amy 2021-08-30 23:28:28 -04:00
parent 81cb3d60e4
commit 4204d2ea93
2 changed files with 30 additions and 20 deletions

View File

@ -288,31 +288,37 @@ function NavItem(props: NavItemProps) {
);
}
type Leaves = {
interface Leaf {
name: string;
route: string;
exact?: boolean;
ancestors: { name: string; route: string }[];
}[];
function collectLeaves(
entry: { name: string; route: string; exact?: boolean; submenu?: Menu },
ancestors: { name: string; route: string }[]
): Leaves {
if (entry.submenu == null) {
return [{ ...entry, ancestors }];
}
const ret = [] as Leaves;
for (const subEntry of entry.submenu) {
ret.push(...collectLeaves(subEntry, [...ancestors, entry]));
}
return ret;
}
const leaves: Leaves = menu.reduce((prev, cur) => {
prev.push(...collectLeaves(cur, []));
return prev;
}, [] as Leaves);
function collectLeaves(
accumulator: Leaf[],
entry: {
name: string;
route: string;
exact?: boolean;
submenu?: Menu;
}
): Leaf[] {
if (entry.submenu == null) {
return [...accumulator, { ...entry, ancestors: [] }];
}
const subleaves = entry.submenu.reduce(collectLeaves, [] as Leaf[]);
return [
...accumulator,
...subleaves.map((leaf) => ({
...leaf,
ancestors: [...leaf.ancestors, { name: entry.name, route: entry.route }],
})),
];
}
const leaves: Leaf[] = menu.reduce(collectLeaves, [] as Leaf[]);
function shouldHighlight(
pathname: string,

View File

@ -45,7 +45,11 @@ export function ShapesBackground({ getConfig }: Props) {
}, [getConfig, width, height, prevWidth, prevRoute, router.asPath]);
return (
<div className={styles.shapesContainer} ref={shapesContainerRef}>
<div
className={styles.shapesContainer}
ref={shapesContainerRef}
aria-hidden
>
{Object.entries(config).map(([type, instances]) =>
instances.map((attributes, idx) => (
<Shape