@@ -11,16 +11,19 @@ import axios from '@nextcloud/axios'
1111import { generateOcsUrl } from '@nextcloud/router'
1212import { getCurrentUser } from '@nextcloud/auth'
1313import { dirname , encodePath , joinPaths } from '@nextcloud/paths'
14+ import { getCanonicalLocale , getLanguage } from '@nextcloud/l10n'
1415
1516import { getContents as getFiles } from './Files.ts'
1617
1718// eslint-disable-next-line no-use-before-define
18- type Tree = Array < {
19+ type Tree = TreeNodeData [ ]
20+
21+ interface TreeNodeData {
1922 id : number ,
2023 basename : string ,
2124 displayName ?: string ,
2225 children : Tree ,
23- } >
26+ }
2427
2528export interface TreeNode {
2629 source : string ,
@@ -35,8 +38,19 @@ export const folderTreeId = 'folders'
3538
3639export const sourceRoot = `${ davRemoteURL } /files/${ getCurrentUser ( ) ?. uid } `
3740
41+ const collator = Intl . Collator (
42+ [ getLanguage ( ) , getCanonicalLocale ( ) ] ,
43+ {
44+ numeric : true ,
45+ usage : 'sort' ,
46+ } ,
47+ )
48+
49+ const compareNodes = ( a : TreeNodeData , b : TreeNodeData ) => collator . compare ( a . displayName ?? a . basename , b . displayName ?? b . basename )
50+
3851const getTreeNodes = ( tree : Tree , currentPath : string = '/' , nodes : TreeNode [ ] = [ ] ) : TreeNode [ ] => {
39- for ( const { id, basename, displayName, children } of tree ) {
52+ const sortedTree = tree . toSorted ( compareNodes )
53+ for ( const { id, basename, displayName, children } of sortedTree ) {
4054 const path = joinPaths ( currentPath , basename )
4155 const source = `${ sourceRoot } ${ path } `
4256 const node : TreeNode = {
0 commit comments