Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort NavigationTree alphabetically #112

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/basic/components/accordion/docs.mdx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a simple accordion to show the sorting

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: 'Accordion'
description: 'UI component named after a funky instrument 🪗'
path: 'Content/Accordion'
---
import {Accordion} from '.'

<Accordion/>
15 changes: 15 additions & 0 deletions examples/basic/components/accordion/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ReactElement } from 'react'

interface AccordionProps {
color?: string
children: ReactElement
}

export function Accordion({ children }: AccordionProps) {
return (
<details>
<summary>Hello 👋</summary>
Nice meeting you! How's everything going?
</details>
)
}
Comment on lines +8 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the built in HTML elements for this, I imagine down the line we'll use the components package Dylan is working on

25 changes: 25 additions & 0 deletions packages/swingset/src/get-nav-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ComponentNode,
CategoryNode,
NavigationTree,
NavigationNode,
} from './types.js'

export function getNavigationTree(
Expand Down Expand Up @@ -83,5 +84,29 @@ export function getNavigationTree(
}

const tree = Array.from(categories.values())

tree.sort(compareTitleSort)

return tree
}

function compareTitleSort<T extends CategoryNode | NavigationNode>(
a: T,
b: T
): -1 | 0 | 1 {
const aHasChildren = 'children' in a && a.children.length > 0
const bHasChildren = 'children' in b && b.children.length > 0
if (aHasChildren) {
a.children.sort(compareTitleSort)
}
if (bHasChildren) {
b.children.sort(compareTitleSort)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this might not be the proper place to call sort recursively, as each element might get passed to the compare function more than once, which would result in us sorting an element's children multiple times. We'll likely want to separate out traversing the tree from the actual sorting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting, something like:

  • Create a toBeSorted = Map()
  • Traverse the tree, while checking which nodes have children
  • If hasChildren == true. Store reference to the node in toBeSorted
  • Loop through the map and sort

if (a.title > b.title) {
return 1
}
if (b.title > a.title) {
return -1
}
return 0
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the compare function recursive per our discussion. @BRKalow

This way the tree will be sorted no matter how deep the structure gets as long as we keep using the children key to handle nesting