Skip to content

Commit a2eac7f

Browse files
skjnldsvbackportbot[bot]
authored andcommitted
fix(files): sort favorites navigation alphabetically
Signed-off-by: skjnldsv <[email protected]>
1 parent df36554 commit a2eac7f

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

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

99
import * as filesUtils from '@nextcloud/files'
10+
import * as filesDavUtils from '@nextcloud/files/dav'
1011
import { CancelablePromise } from 'cancelable-promise'
1112
import { basename } from 'path'
1213
import { beforeEach, describe, expect, test, vi } from 'vitest'
@@ -43,7 +44,7 @@ describe('Favorites view definition', () => {
4344

4445
test('Default empty favorite view', async () => {
4546
vi.spyOn(eventBus, 'subscribe')
46-
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
47+
vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
4748
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
4849

4950
await registerFavoritesView()
@@ -89,26 +90,35 @@ describe('Favorites view definition', () => {
8990
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar',
9091
owner: 'admin',
9192
}),
93+
new Folder({
94+
id: 4,
95+
root: '/files/admin',
96+
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar/yabadaba',
97+
owner: 'admin',
98+
}),
9299
]
93-
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
100+
vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
94101
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
95102

96103
await registerFavoritesView()
97104
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
98105
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
99106

100107
// one main view and 3 children
101-
expect(Navigation.views.length).toBe(4)
108+
expect(Navigation.views.length).toBe(5)
102109
expect(favoritesView).toBeDefined()
103-
expect(favoriteFoldersViews.length).toBe(3)
110+
expect(favoriteFoldersViews.length).toBe(4)
111+
112+
// Sorted by basename: bar, bar, foo
113+
const expectedOrder = [2, 0, 1, 3]
104114

105115
favoriteFolders.forEach((folder, index) => {
106116
const favoriteView = favoriteFoldersViews[index]
107117
expect(favoriteView).toBeDefined()
108118
expect(favoriteView?.id).toBeDefined()
109119
expect(favoriteView?.name).toBe(basename(folder.path))
110120
expect(favoriteView?.icon).toMatch(/<svg.+<\/svg>/)
111-
expect(favoriteView?.order).toBe(index)
121+
expect(favoriteView?.order).toBe(expectedOrder[index])
112122
expect(favoriteView?.params).toStrictEqual({
113123
dir: folder.path,
114124
fileid: String(folder.fileid),
@@ -132,7 +142,7 @@ describe('Dynamic update of favorite folders', () => {
132142

133143
test('Add a favorite folder creates a new entry in the navigation', async () => {
134144
vi.spyOn(eventBus, 'emit')
135-
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
145+
vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
136146
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
137147

138148
await registerFavoritesView()
@@ -160,7 +170,7 @@ describe('Dynamic update of favorite folders', () => {
160170

161171
test('Remove a favorite folder remove the entry from the navigation column', async () => {
162172
vi.spyOn(eventBus, 'emit')
163-
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([
173+
vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([
164174
new Folder({
165175
id: 42,
166176
root: '/files/admin',
@@ -211,7 +221,7 @@ describe('Dynamic update of favorite folders', () => {
211221

212222
test('Renaming a favorite folder updates the navigation', async () => {
213223
vi.spyOn(eventBus, 'emit')
214-
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
224+
vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
215225
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
216226

217227
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)