fix: Sort nodes correctly in useCollectionTrees. closes #6102
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import * as React from "react";
|
||||
import { NavigationNode, NavigationNodeType } from "@shared/types";
|
||||
import { sortNavigationNodes } from "@shared/utils/collections";
|
||||
import Collection from "~/models/Collection";
|
||||
import useStores from "~/hooks/useStores";
|
||||
|
||||
@@ -66,7 +67,9 @@ export default function useCollectionTrees(): NavigationNode[] {
|
||||
title: collection.name,
|
||||
url: collection.url,
|
||||
type: NavigationNodeType.Collection,
|
||||
children: collection.documents || [],
|
||||
children: collection.documents
|
||||
? sortNavigationNodes(collection.documents, collection.sort, true)
|
||||
: [],
|
||||
parent: null,
|
||||
};
|
||||
|
||||
|
||||
@@ -7,25 +7,25 @@ type Sort = {
|
||||
};
|
||||
|
||||
export const sortNavigationNodes = (
|
||||
documents: NavigationNode[],
|
||||
nodes: NavigationNode[],
|
||||
sort: Sort,
|
||||
sortChildren = true
|
||||
): NavigationNode[] => {
|
||||
// "index" field is manually sorted and is represented by the documentStructure
|
||||
// already saved in the database, no further sort is needed
|
||||
if (sort.field === "index") {
|
||||
return documents;
|
||||
return nodes;
|
||||
}
|
||||
|
||||
const orderedDocs = naturalSort(documents, sort.field, {
|
||||
const orderedDocs = naturalSort(nodes, sort.field, {
|
||||
direction: sort.direction,
|
||||
});
|
||||
|
||||
return orderedDocs.map((document) => ({
|
||||
...document,
|
||||
return orderedDocs.map((node) => ({
|
||||
...node,
|
||||
children: sortChildren
|
||||
? sortNavigationNodes(document.children, sort, sortChildren)
|
||||
: document.children,
|
||||
? sortNavigationNodes(node.children, sort, sortChildren)
|
||||
: node.children,
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user