From f81210326ac02ef51b7444452d218491ce9252c6 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Sirois Date: Wed, 1 Jan 2020 14:29:37 -0800 Subject: [PATCH] feat(resources): sort menu items: intro, folders, pages --- src/components/ResourcesSidebar/index.tsx | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/ResourcesSidebar/index.tsx b/src/components/ResourcesSidebar/index.tsx index 13ab293..dfd4f0b 100644 --- a/src/components/ResourcesSidebar/index.tsx +++ b/src/components/ResourcesSidebar/index.tsx @@ -1,4 +1,5 @@ import { graphql, useStaticQuery } from "gatsby" +import { descend, sortWith } from "ramda" import React, { FC, HTMLAttributes, memo, useState } from "react" import TriangleDown from "../../icons/triangle-down.svg" import { IAllResourcesQuery, IFileOrFolder, IFolder } from "../../types" @@ -28,6 +29,20 @@ const ALL_RESOURCES = graphql` } ` +const childrenSort = sortWith([ + descend(f => { + if (f.title === "intro") { + return 1 + } + + if (f.type === "folder") { + return 0 + } + + return -1 + }), +]) + function plantTree(item: IFileOrFolder, index?: number, firstLevel?: boolean) { if (item.type === "file") { const path = getPath(item) @@ -57,12 +72,14 @@ function Folder({ item }: { item: IFolder }) { setCollapse(prevState => !prevState) } + const sortedChildren = childrenSort(item.children) + return ( {humanize(item.title)} - {item.children.map(node => plantTree(node))} + {sortedChildren.map(node => plantTree(node))} ) } @@ -76,6 +93,7 @@ const FirstLevelFolder = memo( }) const collapsed = current !== index + const sortedChildren = childrenSort(item.children) return ( @@ -86,7 +104,7 @@ const FirstLevelFolder = memo( {humanize(item.title)} - {item.children.map(node => plantTree(node))} + {sortedChildren.map(node => plantTree(node))} ) }