Skip to content

Commit

Permalink
fix(files): Render folders in natural sort order
Browse files Browse the repository at this point in the history
- Nodes are returned from the endpoint in an undefined order

Signed-off-by: Christopher Ng <chrng8@gmail.com>

[skip ci]
  • Loading branch information
Pytal authored and backportbot[bot] committed Sep 10, 2024
1 parent 8a14a0c commit b50be56
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions apps/files/src/services/FolderTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { dirname, encodePath, joinPaths } from '@nextcloud/paths'
import { getCanonicalLocale, getLanguage } from '@nextcloud/l10n'

import { getContents as getFiles } from './Files.ts'

// eslint-disable-next-line no-use-before-define
type Tree = Array<{
type Tree = TreeNodeData[]

interface TreeNodeData {
id: number,
basename: string,
displayName?: string,
children: Tree,
}>
}

export interface TreeNode {
source: string,
Expand All @@ -35,8 +38,19 @@ export const folderTreeId = 'folders'

export const sourceRoot = `${davRemoteURL}/files/${getCurrentUser()?.uid}`

const collator = Intl.Collator(
[getLanguage(), getCanonicalLocale()],
{
numeric: true,
usage: 'sort',
},
)

const compareNodes = (a: TreeNodeData, b: TreeNodeData) => collator.compare(a.displayName ?? a.basename, b.displayName ?? b.basename)

const getTreeNodes = (tree: Tree, currentPath: string = '/', nodes: TreeNode[] = []): TreeNode[] => {
for (const { id, basename, displayName, children } of tree) {
const sortedTree = tree.toSorted(compareNodes)
for (const { id, basename, displayName, children } of sortedTree) {
const path = joinPaths(currentPath, basename)
const source = `${sourceRoot}${path}`
const node: TreeNode = {
Expand Down

0 comments on commit b50be56

Please sign in to comment.