diff options
author | Christopher Ng <chrng8@gmail.com> | 2024-09-09 16:11:43 -0700 |
---|---|---|
committer | Christopher Ng <chrng8@gmail.com> | 2024-09-09 16:11:43 -0700 |
commit | 060783c4f4dd0497754e27b1c3bc7a63ef0d0970 (patch) | |
tree | eea23eef39c3fdac6b2dc54de4461eb9efae4327 /apps | |
parent | 6b079e67a2b8c428cf3b89b98f1ef2dba514b962 (diff) | |
download | nextcloud-server-060783c4f4dd0497754e27b1c3bc7a63ef0d0970.tar.gz nextcloud-server-060783c4f4dd0497754e27b1c3bc7a63ef0d0970.zip |
fix(files): Render folders in natural sort order
- Nodes are returned from the endpoint in an undefined order
Signed-off-by: Christopher Ng <chrng8@gmail.com>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/src/services/FolderTree.ts | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/apps/files/src/services/FolderTree.ts b/apps/files/src/services/FolderTree.ts index 8b156182f69..82f0fb392e5 100644 --- a/apps/files/src/services/FolderTree.ts +++ b/apps/files/src/services/FolderTree.ts @@ -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, @@ -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 = { |