Skip to content

Commit

Permalink
feat(files): Allow more than 50 favorite views
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Aug 29, 2024
1 parent 83ae862 commit 3eca671
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
2 changes: 1 addition & 1 deletion apps/files/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { entry as newFolderEntry } from './newMenu/newFolder.ts'
import { entry as newTemplatesFolder } from './newMenu/newTemplatesFolder.ts'
import { registerTemplateEntries } from './newMenu/newFromTemplate.ts'

import registerFavoritesView from './views/favorites'
import { registerFavoritesView } from './views/favorites.ts'
import registerRecentView from './views/recent'
import registerPersonalFilesView from './views/personal-files'
import registerFilesView from './views/files'
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/views/favorites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as initialState from '@nextcloud/initial-state'

import { action } from '../actions/favoriteAction'
import * as favoritesService from '../services/Favorites'
import registerFavoritesView from './favorites'
import { registerFavoritesView } from './favorites'

jest.mock('webdav/dist/node/request.js', () => ({
request: jest.fn(),
Expand Down
35 changes: 13 additions & 22 deletions apps/files/src/views/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,27 @@
import type { Folder, Node } from '@nextcloud/files'

import { subscribe } from '@nextcloud/event-bus'
import { FileType, View, getNavigation } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'
import { FileType, View, getFavoriteNodes, getNavigation } from '@nextcloud/files'
import { getLanguage, translate as t } from '@nextcloud/l10n'
import { basename } from 'path'
import { client } from '../services/WebdavClient.ts'
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
import StarSvg from '@mdi/svg/svg/star.svg?raw'

import { getContents } from '../services/Favorites'
import { hashCode } from '../utils/hashUtils'
import logger from '../logger'

// The return type of the initial state
interface IFavoriteFolder {
fileid: number
path: string
}

export const generateFavoriteFolderView = function(folder: IFavoriteFolder, index = 0): View {
const generateFavoriteFolderView = function(folder: Folder, index = 0): View {
return new View({
id: generateIdFromPath(folder.path),
name: basename(folder.path),
name: folder.displayname,

icon: FolderSvg,
order: index,

params: {
dir: folder.path,
fileid: folder.fileid.toString(),
fileid: String(folder.fileid),
view: 'favorites',
},

Expand All @@ -43,16 +37,11 @@ export const generateFavoriteFolderView = function(folder: IFavoriteFolder, inde
})
}

export const generateIdFromPath = function(path: string): string {
const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}`
}

export default () => {
// Load state in function for mock testing purposes
const favoriteFolders = loadState<IFavoriteFolder[]>('files', 'favoriteFolders', [])
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFavoriteFolderView(folder, index)) as View[]
logger.debug('Generating favorites view', { favoriteFolders })

export const registerFavoritesView = async () => {
const Navigation = getNavigation()
Navigation.register(new View({
id: 'favorites',
Expand All @@ -70,6 +59,9 @@ export default () => {
getContents,
}))

const favoriteFolders = (await getFavoriteNodes(client)).filter(node => node.type === FileType.Folder) as Folder[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFavoriteFolderView(folder, index)) as View[]
logger.debug('Generating favorites view', { favoriteFolders })
favoriteFoldersViews.forEach(view => Navigation.register(view))

/**
Expand Down Expand Up @@ -137,16 +129,15 @@ export default () => {

// Add a folder to the favorites paths array and update the views
const addToFavorites = function(node: Folder) {
const newFavoriteFolder: IFavoriteFolder = { path: node.path, fileid: node.fileid! }
const view = generateFavoriteFolderView(newFavoriteFolder)
const view = generateFavoriteFolderView(node)

// Skip if already exists
if (favoriteFolders.find((folder) => folder.path === node.path)) {
return
}

// Update arrays
favoriteFolders.push(newFavoriteFolder)
favoriteFolders.push(node)
favoriteFoldersViews.push(view)

// Update and sort views
Expand Down

0 comments on commit 3eca671

Please sign in to comment.