#272 Keep undated assets last regardless of album sort order
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
||||
videoUrl
|
||||
} from '../immich'
|
||||
import { Response } from 'express-serve-static-core'
|
||||
import { AssetType, ImageSize, SharedLink } from '../types'
|
||||
import { Asset, AssetType, ImageSize, SharedLink } from '../types'
|
||||
import { getConfigOption, getNumericConfigOption } from '../config/access'
|
||||
import { canDownload, expiryDate, title } from '../share'
|
||||
import { toString } from '../utils/text'
|
||||
@@ -34,11 +34,7 @@ export async function gallery (res: Response, share: SharedLink, openItem?: numb
|
||||
// contiguous / ordered.
|
||||
const groupByDate = groupByDateMode()
|
||||
if (groupByDate) {
|
||||
const ascending = share.album?.order === 'asc'
|
||||
const sortKey = (a: typeof share.assets[number]) => a.localDateTime || a.fileCreatedAt || ''
|
||||
share.assets.sort((a, b) => ascending
|
||||
? sortKey(a).localeCompare(sortKey(b))
|
||||
: sortKey(b).localeCompare(sortKey(a)))
|
||||
share.assets.sort(dateSortComparator(share.album?.order))
|
||||
}
|
||||
|
||||
// Metadata display flags. Read once here and forwarded to the client via
|
||||
@@ -171,6 +167,23 @@ function description (share: SharedLink) {
|
||||
return share?.album?.description || ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Comparator for the date-grouping sort: ascending when the album's order is
|
||||
* `'asc'`, otherwise newest-first (individual shares and orderless albums).
|
||||
* Undated assets always sort last regardless of direction, so the client's
|
||||
* "Undated" group renders at the bottom.
|
||||
*/
|
||||
export function dateSortComparator (order?: string): (a: Asset, b: Asset) => number {
|
||||
const ascending = order === 'asc'
|
||||
const sortKey = (a: Asset) => a.localDateTime || a.fileCreatedAt || ''
|
||||
return (a, b) => {
|
||||
const ka = sortKey(a)
|
||||
const kb = sortKey(b)
|
||||
if (!ka || !kb) return ka ? -1 : kb ? 1 : 0 // undated always last
|
||||
return ascending ? ka.localeCompare(kb) : kb.localeCompare(ka)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise the operator's `ipp.gallery.groupByDate` config into a grouping
|
||||
* mode. Accepts `false` (off), `true` / `'month'` (legacy = month buckets) or
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { dateSortComparator } from '../src/gallery/builder'
|
||||
import { Asset, AssetType, KeyType } from '../src/types'
|
||||
|
||||
/*
|
||||
The date-grouping sort must follow the album's own order while
|
||||
keeping undated assets last in BOTH directions.
|
||||
*/
|
||||
|
||||
const asset = (localDateTime?: string): Asset => ({
|
||||
id: localDateTime || 'undated',
|
||||
key: 'k',
|
||||
keyType: KeyType.key,
|
||||
type: AssetType.image,
|
||||
isTrashed: false,
|
||||
localDateTime
|
||||
})
|
||||
|
||||
const dates = (assets: Asset[]) => assets.map(a => a.localDateTime || 'UNDATED')
|
||||
|
||||
const mixed = () => [
|
||||
asset('2024-06-01T10:00:00.000Z'),
|
||||
asset(),
|
||||
asset('2024-01-01T10:00:00.000Z'),
|
||||
asset('2025-03-01T10:00:00.000Z')
|
||||
]
|
||||
|
||||
describe('dateSortComparator', () => {
|
||||
it('defaults to newest-first with no album order', () => {
|
||||
expect(dates(mixed().sort(dateSortComparator(undefined)))).toEqual(
|
||||
['2025-03-01T10:00:00.000Z', '2024-06-01T10:00:00.000Z', '2024-01-01T10:00:00.000Z', 'UNDATED'])
|
||||
})
|
||||
|
||||
it('sorts oldest-first for asc albums', () => {
|
||||
expect(dates(mixed().sort(dateSortComparator('asc')))).toEqual(
|
||||
['2024-01-01T10:00:00.000Z', '2024-06-01T10:00:00.000Z', '2025-03-01T10:00:00.000Z', 'UNDATED'])
|
||||
})
|
||||
|
||||
it('sorts newest-first for desc albums', () => {
|
||||
expect(dates(mixed().sort(dateSortComparator('desc')))).toEqual(
|
||||
['2025-03-01T10:00:00.000Z', '2024-06-01T10:00:00.000Z', '2024-01-01T10:00:00.000Z', 'UNDATED'])
|
||||
})
|
||||
|
||||
it('keeps undated assets last in both directions', () => {
|
||||
const withUndatedFirst = [asset(), asset('2024-01-01T10:00:00.000Z')]
|
||||
expect(dates([...withUndatedFirst].sort(dateSortComparator('asc')))).toEqual(
|
||||
['2024-01-01T10:00:00.000Z', 'UNDATED'])
|
||||
expect(dates([...withUndatedFirst].sort(dateSortComparator(undefined)))).toEqual(
|
||||
['2024-01-01T10:00:00.000Z', 'UNDATED'])
|
||||
})
|
||||
|
||||
it('falls back to fileCreatedAt when localDateTime is missing', () => {
|
||||
const a: Asset = { ...asset(), fileCreatedAt: '2024-05-01T10:00:00.000Z' }
|
||||
const b = asset('2024-01-01T10:00:00.000Z')
|
||||
const sorted = [a, b].sort(dateSortComparator('asc'))
|
||||
expect(sorted[0]).toBe(b)
|
||||
expect(sorted[1]).toBe(a)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user