-
Notifications
You must be signed in to change notification settings - Fork 286
fix: list separators depending on sort by date #11708
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { groupEnvelopesByDate } from '../../../util/groupedEnvelopes.js' | ||
|
||
describe('groupEnvelopesByDate', () => { | ||
const now = new Date('2025-10-07T12:00:00Z').getTime() | ||
const makeEnvelope = (date) => ({ dateInt: Math.floor(date.getTime() / 1000) }) | ||
|
||
it('groups envelopes into lastHour, yesterday, lastMonth, July, and 2024', () => { | ||
const envelopes = [ | ||
makeEnvelope(new Date('2025-10-07T11:30:00Z')), | ||
makeEnvelope(new Date('2025-10-06T18:00:00Z')), | ||
makeEnvelope(new Date('2025-09-10T12:00:00Z')), | ||
makeEnvelope(new Date('2025-07-01T12:00:00Z')), | ||
makeEnvelope(new Date('2024-12-25T12:00:00Z')), | ||
] | ||
|
||
const result = groupEnvelopesByDate(envelopes, now, 'newest') | ||
|
||
expect(Array.isArray(result)).toBe(true) | ||
expect(result).toHaveLength(5) | ||
|
||
const labels = result.map(([label]) => label) | ||
expect(labels).toEqual( | ||
expect.arrayContaining(['lastHour', 'yesterday', 'lastMonth', 'July', '2024']), | ||
) | ||
|
||
result.forEach(([label, group]) => { | ||
expect(Array.isArray(group)).toBe(true) | ||
expect(group).toHaveLength(1) | ||
}) | ||
}) | ||
|
||
it('respects sortOrder = "oldest" for the lastHour group', () => { | ||
const newer = makeEnvelope(new Date('2025-10-07T11:50:00Z')) | ||
const older = makeEnvelope(new Date('2025-10-07T11:10:00Z')) | ||
|
||
const result = groupEnvelopesByDate([newer, older], now, 'oldest') | ||
|
||
expect(result[0][0]).toBe('lastHour') | ||
const lastHourGroup = result[0][1] | ||
expect(lastHourGroup[0]).toEqual(older) | ||
expect(lastHourGroup[1]).toEqual(newer) | ||
}) | ||
}) | ||
GretaD marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
export function groupEnvelopesByDate(envelopes, syncTimestamp, sortOrder = 'newest') { | ||
const now = new Date(syncTimestamp) | ||
ChristophWurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000) | ||
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()) | ||
const startOfYesterday = new Date(startOfToday) | ||
startOfYesterday.setDate(startOfYesterday.getDate() - 1) | ||
const startOfLastWeek = new Date(now) | ||
startOfLastWeek.setDate(startOfLastWeek.getDate() - 7) | ||
const startOfLastMonth = new Date(now) | ||
startOfLastMonth.setMonth(startOfLastMonth.getMonth() - 1) | ||
|
||
const groups = { | ||
lastHour: [], | ||
today: [], | ||
yesterday: [], | ||
lastWeek: [], | ||
lastMonth: [], | ||
} | ||
|
||
const monthsMap = {} | ||
const yearsMap = {} | ||
|
||
for (const envelope of envelopes) { | ||
const date = new Date(envelope.dateInt * 1000) | ||
|
||
if (date >= oneHourAgo) { | ||
groups.lastHour.push(envelope) | ||
} else if (date >= startOfToday) { | ||
groups.today.push(envelope) | ||
} else if (date >= startOfYesterday && date < startOfToday) { | ||
groups.yesterday.push(envelope) | ||
} else if (date >= startOfLastWeek) { | ||
groups.lastWeek.push(envelope) | ||
} else if (date >= startOfLastMonth) { | ||
groups.lastMonth.push(envelope) | ||
} else if (date.getFullYear() === now.getFullYear()) { | ||
const m = date.getMonth() | ||
monthsMap[m] ??= [] | ||
monthsMap[m].push(envelope) | ||
} else { | ||
const y = date.getFullYear() | ||
yearsMap[y] ??= [] | ||
yearsMap[y].push(envelope) | ||
} | ||
} | ||
|
||
const orderByDate = (a, b) => | ||
sortOrder === 'newest' ? b.dateInt - a.dateInt : a.dateInt - b.dateInt | ||
|
||
Object.values(groups).forEach(list => list.sort(orderByDate)) | ||
Object.values(monthsMap).forEach(list => list.sort(orderByDate)) | ||
Object.values(yearsMap).forEach(list => list.sort(orderByDate)) | ||
|
||
const groupOrder = [] | ||
|
||
const fixedGroups = ['lastHour', 'today', 'yesterday', 'lastWeek', 'lastMonth'] | ||
groupOrder.push(...(sortOrder === 'newest' ? fixedGroups : fixedGroups.toReversed())) | ||
|
||
const monthOrder = Object.keys(monthsMap).map(Number) | ||
monthOrder.sort((a, b) => (sortOrder === 'newest' ? b - a : a - b)) | ||
for (const m of monthOrder) { | ||
const monthName = new Date(now.getFullYear(), m, 1) | ||
.toLocaleString('default', { month: 'long' }) | ||
groups[monthName] = monthsMap[m] | ||
sortOrder === 'newest' ? groupOrder.push(monthName) : groupOrder.unshift(monthName) | ||
} | ||
|
||
const yearKeys = Object.keys(yearsMap).map(Number) | ||
yearKeys.sort((a, b) => (sortOrder === 'newest' ? b - a : a - b)) | ||
for (const y of yearKeys) { | ||
groups[String(y)] = yearsMap[y] | ||
sortOrder === 'newest' ? groupOrder.push(String(y)) : groupOrder.unshift(String(y)) | ||
} | ||
|
||
return groupOrder | ||
.filter(label => groups[label] && groups[label].length > 0) | ||
.map(label => [label, groups[label]]) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.