Skip to content

Commit 0605f61

Browse files
committed
fix(files): sort favorites navigation alphabetically
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 6425d83 commit 0605f61

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

apps/files/src/views/favorites.spec.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
import type { Folder as CFolder, Navigation } from '@nextcloud/files'
88

99
import { expect } from '@jest/globals'
10-
import * as filesUtils from '@nextcloud/files'
11-
import { CancelablePromise } from 'cancelable-promise'
10+
1211
import * as eventBus from '@nextcloud/event-bus'
12+
import * as filesUtils from '@nextcloud/files'
13+
import * as filesDavUtils from '@nextcloud/files/dav'
1314
import { basename } from 'path'
15+
import { CancelablePromise } from 'cancelable-promise'
1416

1517
import { action } from '../actions/favoriteAction'
16-
import * as favoritesService from '../services/Favorites'
1718
import { registerFavoritesView } from './favorites'
19+
import * as favoritesService from '../services/Favorites'
1820

1921
const { Folder, getNavigation } = filesUtils
2022

@@ -31,6 +33,11 @@ jest.mock('@nextcloud/files', () => ({
3133
...jest.requireActual('@nextcloud/files'),
3234
}))
3335

36+
jest.mock('@nextcloud/files/dav', () => ({
37+
__esModule: true,
38+
...jest.requireActual('@nextcloud/files/dav'),
39+
}))
40+
3441
jest.mock('@nextcloud/event-bus', () => ({
3542
__esModule: true,
3643
...jest.requireActual('@nextcloud/event-bus'),
@@ -60,7 +67,7 @@ describe('Favorites view definition', () => {
6067

6168
test('Default empty favorite view', async () => {
6269
jest.spyOn(eventBus, 'subscribe')
63-
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
70+
jest.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
6471
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
6572

6673
await registerFavoritesView()
@@ -106,26 +113,35 @@ describe('Favorites view definition', () => {
106113
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar',
107114
owner: 'admin',
108115
}),
116+
new Folder({
117+
id: 4,
118+
root: '/files/admin',
119+
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar/yabadaba',
120+
owner: 'admin',
121+
}),
109122
]
110-
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
123+
jest.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
111124
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
112125

113126
await registerFavoritesView()
114127
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
115128
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
116129

117130
// one main view and 3 children
118-
expect(Navigation.views.length).toBe(4)
131+
expect(Navigation.views.length).toBe(5)
119132
expect(favoritesView).toBeDefined()
120-
expect(favoriteFoldersViews.length).toBe(3)
133+
expect(favoriteFoldersViews.length).toBe(4)
134+
135+
// Sorted by basename: bar, bar, foo
136+
const expectedOrder = [2, 0, 1, 3]
121137

122138
favoriteFolders.forEach((folder, index) => {
123139
const favoriteView = favoriteFoldersViews[index]
124140
expect(favoriteView).toBeDefined()
125141
expect(favoriteView?.id).toBeDefined()
126142
expect(favoriteView?.name).toBe(basename(folder.path))
127143
expect(favoriteView?.icon).toBe('<svg>SvgMock</svg>')
128-
expect(favoriteView?.order).toBe(index)
144+
expect(favoriteView?.order).toBe(expectedOrder[index])
129145
expect(favoriteView?.params).toStrictEqual({
130146
dir: folder.path,
131147
fileid: String(folder.fileid),
@@ -150,7 +166,7 @@ describe('Dynamic update of favourite folders', () => {
150166

151167
test('Add a favorite folder creates a new entry in the navigation', async () => {
152168
jest.spyOn(eventBus, 'emit')
153-
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
169+
jest.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
154170
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
155171

156172
await registerFavoritesView()
@@ -179,7 +195,7 @@ describe('Dynamic update of favourite folders', () => {
179195
test('Remove a favorite folder remove the entry from the navigation column', async () => {
180196
jest.spyOn(eventBus, 'emit')
181197
jest.spyOn(eventBus, 'subscribe')
182-
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([
198+
jest.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([
183199
new Folder({
184200
id: 42,
185201
root: '/files/admin',
@@ -226,7 +242,7 @@ describe('Dynamic update of favourite folders', () => {
226242

227243
test('Renaming a favorite folder updates the navigation', async () => {
228244
jest.spyOn(eventBus, 'emit')
229-
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
245+
jest.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
230246
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
231247

232248
await registerFavoritesView()

apps/files/src/views/favorites.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*/
55
import type { Folder, Node } from '@nextcloud/files'
66

7+
import { FileType, View, getNavigation } from '@nextcloud/files'
8+
import { getCanonicalLocale, getLanguage, t } from '@nextcloud/l10n'
9+
import { getFavoriteNodes } from '@nextcloud/files/dav'
710
import { subscribe } from '@nextcloud/event-bus'
8-
import { FileType, View, getFavoriteNodes, getNavigation } from '@nextcloud/files'
9-
import { getLanguage, translate as t } from '@nextcloud/l10n'
10-
import { client } from '../services/WebdavClient.ts'
11+
1112
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
1213
import StarSvg from '@mdi/svg/svg/star.svg?raw'
1314

15+
import { client } from '../services/WebdavClient.ts'
1416
import { getContents } from '../services/Favorites'
1517
import { hashCode } from '../utils/hashUtils'
1618
import logger from '../logger'
@@ -118,7 +120,7 @@ export const registerFavoritesView = async () => {
118120
* update the order property of the existing views
119121
*/
120122
const updateAndSortViews = function() {
121-
favoriteFolders.sort((a, b) => a.path.localeCompare(b.path, getLanguage(), { ignorePunctuation: true }))
123+
favoriteFolders.sort((a, b) => a.basename.localeCompare(b.basename, [getLanguage(), getCanonicalLocale()], { ignorePunctuation: true, numeric: true, usage: 'sort' }))
122124
favoriteFolders.forEach((folder, index) => {
123125
const view = favoriteFoldersViews.find((view) => view.id === generateIdFromPath(folder.path))
124126
if (view) {
@@ -176,4 +178,6 @@ export const registerFavoritesView = async () => {
176178
removePathFromFavorites(favoriteFolder.path)
177179
addToFavorites(node)
178180
}
181+
182+
updateAndSortViews()
179183
}

0 commit comments

Comments
 (0)