From 55bd63e3f24b514259c318ba445f299a5f89a108 Mon Sep 17 00:00:00 2001 From: Alan Grainger <16197738+alangrainger@users.noreply.github.com> Date: Mon, 27 Jul 2026 12:27:03 +1200 Subject: [PATCH] #272 Keep undated assets last regardless of album sort order --- app/src/gallery/builder.ts | 25 ++++++++++++---- app/tests/dateSort.test.ts | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 app/tests/dateSort.test.ts diff --git a/app/src/gallery/builder.ts b/app/src/gallery/builder.ts index 2d8c82a..8cd7892 100644 --- a/app/src/gallery/builder.ts +++ b/app/src/gallery/builder.ts @@ -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 diff --git a/app/tests/dateSort.test.ts b/app/tests/dateSort.test.ts new file mode 100644 index 0000000..af84473 --- /dev/null +++ b/app/tests/dateSort.test.ts @@ -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) + }) +})