Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update favorites navigation list on folder renames #45977

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/files/src/eventbus.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare module '@nextcloud/event-bus' {
// mapping of 'event name' => 'event type'
'files:favorites:removed': Node
'files:favorites:added': Node
'files:node:renamed': Node
}
}

Expand Down
44 changes: 42 additions & 2 deletions apps/files/src/views/favorites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { basename } from 'path'
import { expect } from '@jest/globals'
import { Folder, Navigation, getNavigation } from '@nextcloud/files'
import { CancelablePromise } from 'cancelable-promise'
import eventBus from '@nextcloud/event-bus'
import eventBus, { emit } from '@nextcloud/event-bus'
import * as initialState from '@nextcloud/initial-state'

import { action } from '../actions/favoriteAction'
Expand Down Expand Up @@ -47,9 +47,10 @@ describe('Favorites view definition', () => {
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')

expect(eventBus.subscribe).toHaveBeenCalledTimes(2)
expect(eventBus.subscribe).toHaveBeenCalledTimes(3)
expect(eventBus.subscribe).toHaveBeenNthCalledWith(1, 'files:favorites:added', expect.anything())
expect(eventBus.subscribe).toHaveBeenNthCalledWith(2, 'files:favorites:removed', expect.anything())
expect(eventBus.subscribe).toHaveBeenNthCalledWith(3, 'files:node:renamed', expect.anything())

// one main view and no children
expect(Navigation.views.length).toBe(1)
Expand Down Expand Up @@ -180,4 +181,43 @@ describe('Dynamic update of favourite folders', () => {
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)
})

test('Renaming a favorite folder updates the navigation', async () => {
jest.spyOn(eventBus, 'emit')
jest.spyOn(initialState, 'loadState').mockReturnValue([])
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))

registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')

// one main view and no children
expect(Navigation.views.length).toBe(1)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)

// expect(eventBus.emit).toHaveBeenCalledTimes(2)

// Create new folder to favorite
const folder = new Folder({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
})

// Exec the action
await action.exec(folder, favoritesView, '/')
expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:favorites:added', folder)

// Create a folder with the same id but renamed
const renamedFolder = new Folder({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar.renamed',
owner: 'admin',
})

// Exec the rename action
emit('files:node:renamed', renamedFolder)
expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:node:renamed', renamedFolder)
})
})
28 changes: 28 additions & 0 deletions apps/files/src/views/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ export default () => {
removePathFromFavorites(node.path)
})

/**
* Update favourites navigation when a folder is renamed
*/
subscribe('files:node:renamed', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}

if (node.attributes.favorite !== 1) {
artonge marked this conversation as resolved.
Show resolved Hide resolved
return
}

updateNodeFromFavorites(node as Folder)
})

/**
* Sort the favorites paths array and
* update the order property of the existing views
Expand Down Expand Up @@ -157,4 +172,17 @@ export default () => {
Navigation.remove(id)
updateAndSortViews()
}

// Update a folder from the favorites paths array and update the views
const updateNodeFromFavorites = function(node: Folder) {
const favoriteFolder = favoriteFolders.find((folder) => folder.fileid === node.fileid)

// Skip if it does not exists
if (favoriteFolder === undefined) {
return
}

removePathFromFavorites(favoriteFolder.path)
addToFavorites(node)
}
}
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

Loading