forked from reaviz/reachat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
259 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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,69 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { groupSessionsByDate } from './utils'; | ||
import { Session } from './types'; | ||
import { subDays, subWeeks, subMonths, subYears } from 'date-fns'; | ||
|
||
describe('groupSessionsByDate', () => { | ||
const createSession = (daysAgo: number): Session => ({ | ||
id: `session-${daysAgo}`, | ||
createdAt: subDays(new Date(), daysAgo), | ||
updatedAt: subDays(new Date(), daysAgo), | ||
name: `Test Session ${daysAgo} days ago`, | ||
conversations: [], | ||
}); | ||
|
||
it('groups sessions correctly', () => { | ||
const sessions: Session[] = [ | ||
createSession(0), // Today | ||
createSession(1), // Yesterday | ||
createSession(3), // Last Week | ||
createSession(20), // Last Month | ||
createSession(60), // A few months ago | ||
createSession(400) // Last Year | ||
]; | ||
|
||
const grouped = groupSessionsByDate(sessions); | ||
|
||
expect(Object.keys(grouped)).toEqual(['Today', 'Yesterday', 'Last Week', 'Last Month', expect.stringMatching(/^[A-Z][a-z]+$/), 'Last Year']); | ||
expect(grouped['Today'].length).toBe(1); | ||
expect(grouped['Yesterday'].length).toBe(1); | ||
expect(grouped['Last Week'].length).toBe(1); | ||
expect(grouped['Last Month'].length).toBe(1); | ||
expect(Object.values(grouped).flat().length).toBe(sessions.length); | ||
}); | ||
|
||
it('handles empty input', () => { | ||
const grouped = groupSessionsByDate([]); | ||
expect(Object.keys(grouped)).toHaveLength(0); | ||
}); | ||
|
||
it('groups multiple sessions in the same category', () => { | ||
const sessions: Session[] = [ | ||
createSession(0), | ||
createSession(0), | ||
createSession(1), | ||
createSession(1), | ||
]; | ||
|
||
const grouped = groupSessionsByDate(sessions); | ||
|
||
expect(grouped['Today'].length).toBe(2); | ||
expect(grouped['Yesterday'].length).toBe(2); | ||
}); | ||
|
||
it('sorts groups in the correct order', () => { | ||
const sessions: Session[] = [ | ||
createSession(400), // Last Year | ||
createSession(0), // Today | ||
createSession(60), // A few months ago | ||
createSession(1), // Yesterday | ||
]; | ||
|
||
const grouped = groupSessionsByDate(sessions); | ||
const groupOrder = Object.keys(grouped); | ||
|
||
expect(groupOrder[0]).toBe('Today'); | ||
expect(groupOrder[1]).toBe('Yesterday'); | ||
expect(groupOrder[groupOrder.length - 1]).toBe('Last Year'); | ||
}); | ||
}); |
This file contains 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,77 @@ | ||
import { format, isToday, isYesterday, isThisWeek, isThisMonth, isThisYear, parseISO } from 'date-fns'; | ||
import { Session } from './types'; | ||
|
||
interface GroupedSessions { | ||
[key: string]: Session[]; | ||
} | ||
|
||
const sortOrder = [ | ||
'Today', | ||
'Yesterday', | ||
'Last Week', | ||
'Last Month', | ||
'January', | ||
'February', | ||
'March', | ||
'April', | ||
'May', | ||
'June', | ||
'July', | ||
'August', | ||
'September', | ||
'October', | ||
'November', | ||
'December', | ||
'Last Year' | ||
]; | ||
|
||
export function groupSessionsByDate(sessions: Session[]): GroupedSessions { | ||
const grouped: GroupedSessions = {}; | ||
|
||
sessions.forEach(session => { | ||
const createdAt = new Date(session.createdAt); | ||
|
||
if (isToday(createdAt)) { | ||
if (!grouped['Today']) grouped['Today'] = []; | ||
grouped['Today'].push(session); | ||
} else if (isYesterday(createdAt)) { | ||
if (!grouped['Yesterday']) grouped['Yesterday'] = []; | ||
grouped['Yesterday'].push(session); | ||
} else if (isThisWeek(createdAt)) { | ||
if (!grouped['Last Week']) grouped['Last Week'] = []; | ||
grouped['Last Week'].push(session); | ||
} else if (isThisMonth(createdAt)) { | ||
if (!grouped['Last Month']) grouped['Last Month'] = []; | ||
grouped['Last Month'].push(session); | ||
} else if (isThisYear(createdAt)) { | ||
const monthName = format(createdAt, 'MMMM'); | ||
if (!grouped[monthName]) grouped[monthName] = []; | ||
grouped[monthName].push(session); | ||
} else { | ||
if (!grouped['Last Year']) grouped['Last Year'] = []; | ||
grouped['Last Year'].push(session); | ||
} | ||
}); | ||
|
||
// Remove empty groups | ||
Object.keys(grouped).forEach(key => { | ||
if (grouped[key].length === 0) { | ||
delete grouped[key]; | ||
} | ||
}); | ||
|
||
// Sort groups | ||
const sortedGroups = Object.keys(grouped).sort((a, b) => | ||
sortOrder.indexOf(a) - sortOrder.indexOf(b) | ||
); | ||
|
||
const sortedGrouped: GroupedSessions = {}; | ||
sortedGroups.forEach(key => { | ||
// Sort sessions within each group by createdAt date (most recent first) | ||
sortedGrouped[key] = grouped[key].sort((a, b) => | ||
new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() | ||
); | ||
}); | ||
|
||
return sortedGrouped; | ||
} |
Oops, something went wrong.