Skip to content

Commit f17be42

Browse files
feat(cloud-agent): improve session sidebar date grouping with named days (#1712)
* feat(ui): group sessions by named days (up to 3) and add Older bucket switch from isThisWeek-based grouping to named-day buckets using startOfDay and differenceInCalendarDays; cap at three named days and aggregate remaining sessions under Older * refactor(cloud-agent-next): use entries() to iterate days in ChatSidebar --------- Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
1 parent eafccce commit f17be42

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/components/cloud-agent-next/ChatSidebar.tsx

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from 'lucide-react';
1515
import { TimeAgo } from '@/components/shared/TimeAgo';
1616
import { usePathname, useRouter } from 'next/navigation';
17-
import { isToday, isYesterday, isThisWeek } from 'date-fns';
17+
import { isToday, isYesterday, startOfDay, differenceInCalendarDays, format } from 'date-fns';
1818
import type { StoredSession } from './types';
1919
import { isNewSession } from '@/lib/cloud-agent/session-type';
2020
import { cn } from '@/lib/utils';
@@ -35,22 +35,56 @@ type DateGroup = {
3535
function groupSessionsByDate(sessions: StoredSession[]): DateGroup[] {
3636
const today: StoredSession[] = [];
3737
const yesterday: StoredSession[] = [];
38-
const thisWeek: StoredSession[] = [];
38+
const namedDayBuckets = new Map<string, { daysAgo: number; sessions: StoredSession[] }>();
3939
const older: StoredSession[] = [];
4040

41+
const now = new Date();
42+
const todayStart = startOfDay(now);
43+
4144
for (const session of sessions) {
4245
const date = new Date(session.updatedAt);
43-
if (isToday(date)) today.push(session);
44-
else if (isYesterday(date)) yesterday.push(session);
45-
else if (isThisWeek(date)) thisWeek.push(session);
46-
else older.push(session);
46+
if (isToday(date)) {
47+
today.push(session);
48+
} else if (isYesterday(date)) {
49+
yesterday.push(session);
50+
} else {
51+
const daysAgo = differenceInCalendarDays(todayStart, startOfDay(date));
52+
if (daysAgo <= 7) {
53+
const dayName = format(date, 'EEEE');
54+
const bucket = namedDayBuckets.get(dayName);
55+
if (bucket) {
56+
bucket.sessions.push(session);
57+
} else {
58+
namedDayBuckets.set(dayName, { daysAgo, sessions: [session] });
59+
}
60+
} else {
61+
older.push(session);
62+
}
63+
}
4764
}
4865

4966
const groups: DateGroup[] = [];
5067
if (today.length > 0) groups.push({ label: 'Today', sessions: today });
5168
if (yesterday.length > 0) groups.push({ label: 'Yesterday', sessions: yesterday });
52-
if (thisWeek.length > 0) groups.push({ label: 'This week', sessions: thisWeek });
53-
if (older.length > 0) groups.push({ label: 'Older', sessions: older });
69+
70+
const sortedNamedDays = [...namedDayBuckets.entries()].sort(
71+
(a, b) => a[1].daysAgo - b[1].daysAgo
72+
);
73+
74+
const MAX_NAMED_DAYS = 3;
75+
for (const [i, [dayName, bucket]] of sortedNamedDays.entries()) {
76+
if (i < MAX_NAMED_DAYS) {
77+
groups.push({ label: dayName, sessions: bucket.sessions });
78+
} else {
79+
older.push(...bucket.sessions);
80+
}
81+
}
82+
83+
if (older.length > 0) {
84+
older.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
85+
groups.push({ label: 'Older', sessions: older });
86+
}
87+
5488
return groups;
5589
}
5690

0 commit comments

Comments
 (0)