Skip to content

Commit 61a7055

Browse files
sort folder contents the same as we do for the tree
1 parent cfe5a7c commit 61a7055

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

packages/web/src/features/fileTree/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from 'path';
1010
import { simpleGit } from 'simple-git';
1111
import { FileTreeItem } from './types';
1212
import { buildFileTree, getPathspecs, isPathValid, normalizePath } from './utils';
13+
import { compareFileTreeItems } from './utils';
1314

1415
const logger = createLogger('file-tree');
1516

@@ -136,6 +137,9 @@ export const getFolderContents = async (params: { repoName: string, revisionName
136137
}
137138
});
138139

140+
// Sort the contents in place, first by type (trees before blobs), then by name.
141+
contents.sort(compareFileTreeItems);
142+
139143
return contents;
140144
}));
141145

packages/web/src/features/fileTree/utils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FileTreeNode } from "./types";
1+
import { FileTreeItem, FileTreeNode } from "./types";
22

33
export const normalizePath = (path: string): string => {
44
// Normalize the path by...
@@ -83,12 +83,7 @@ export const buildFileTree = (flatList: { type: string, path: string }[]): FileT
8383

8484
const sortedChildren = node.children
8585
.map(sortTree)
86-
.sort((a: FileTreeNode, b: FileTreeNode) => {
87-
if (a.type !== b.type) {
88-
return a.type === 'tree' ? -1 : 1;
89-
}
90-
return a.name.localeCompare(b.name, undefined, { sensitivity: 'base' });
91-
});
86+
.sort(compareFileTreeItems);
9287

9388
return {
9489
...node,
@@ -98,3 +93,10 @@ export const buildFileTree = (flatList: { type: string, path: string }[]): FileT
9893

9994
return sortTree(root);
10095
}
96+
97+
export const compareFileTreeItems = (a: FileTreeItem, b: FileTreeItem): number => {
98+
if (a.type !== b.type) {
99+
return a.type === 'tree' ? -1 : 1;
100+
}
101+
return a.name.localeCompare(b.name, undefined, { sensitivity: 'base' });
102+
}

0 commit comments

Comments
 (0)