Skip to content

Commit

Permalink
refactor: Use sorting function from files library
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jun 21, 2024
1 parent 010044c commit 1a2fc17
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions lib/components/FilePicker/FileList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@
import type { Node } from '@nextcloud/files'
import type { FileListViews } from '../../composables/filesSettings'
import { FileType } from '@nextcloud/files'
import { getCanonicalLocale } from '@nextcloud/l10n'
import { FileType, FilesSortingMode, sortNodes } from '@nextcloud/files'
import { NcButton, NcCheckboxRadioSwitch } from '@nextcloud/vue'
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue'
import { useFilesSettings, useFilesViews } from '../../composables/filesSettings'
Expand Down Expand Up @@ -142,31 +141,13 @@ const { sortFavoritesFirst, cropImagePreviews } = useFilesSettings()
* Files sorted by columns
*/
const sortedFiles = computed(() => {
const ordering = {
ascending: <T, >(a: T, b: T, fn: (a: T, b: T) => number) => fn(a, b),
descending: <T, >(a: T, b: T, fn: (a: T, b: T) => number) => fn(b, a),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
none: <T, >(_a: T, _b: T, _fn: (a: T, b: T) => number) => 0,
}
const sorting = {
basename: (a: Node, b: Node) => (a.attributes?.displayName || a.basename).localeCompare(b.attributes?.displayName || b.basename, getCanonicalLocale()),
size: (a: Node, b: Node) => (a.size || 0) - (b.size || 0),
// reverted because "young" is smaller than "old"
mtime: (a: Node, b: Node) => (b.mtime?.getTime?.() || 0) - (a.mtime?.getTime?.() || 0),
}
return [...props.files].sort(
(a, b) =>
// Folders always come above the files
(b.type === FileType.Folder ? 1 : 0) - (a.type === FileType.Folder ? 1 : 0)
// Favorites above other files
|| (sortFavoritesFirst ? ((b.attributes.favorite ? 1 : 0) - (a.attributes.favorite ? 1 : 0)) : 0)
// then sort by name / size / modified
|| ordering[sortingConfig.value.order](a, b, sorting[sortingConfig.value.sortBy]),
)
},
)
return sortNodes(props.files, {
sortFavoritesFirst: sortFavoritesFirst.value,
sortFoldersFirst: true,
sortingOrder: sortingConfig.value.order === 'descending' ? 'desc' : 'asc',
sortingMode: sortingConfig.value.sortBy as FilesSortingMode,
})
})
/**
* Contains the selectable files, filtering out directories if `allowPickDirectory` is not set
Expand Down

0 comments on commit 1a2fc17

Please sign in to comment.