Files
twenty/packages/twenty-front/src/modules/activities/events/utils/groupEventsByMonth.ts
brendanlaschke 922d632607 Basic log styling (#4634)
* basic log styling

* fixed mobile wrap and changed default event icon

* add group by test
2024-03-25 10:15:39 +01:00

34 lines
808 B
TypeScript

import { Event } from '@/activities/events/types/Event';
import { isDefined } from '~/utils/isDefined';
export type EventGroup = {
month: number;
year: number;
items: Event[];
};
export const groupEventsByMonth = (events: Event[]) => {
const acitivityGroups: EventGroup[] = [];
for (const event of events) {
const d = new Date(event.createdAt);
const month = d.getMonth();
const year = d.getFullYear();
const matchingGroup = acitivityGroups.find(
(x) => x.year === year && x.month === month,
);
if (isDefined(matchingGroup)) {
matchingGroup.items.push(event);
} else {
acitivityGroups.push({
year,
month,
items: [event],
});
}
}
return acitivityGroups.sort((a, b) => b.year - a.year || b.month - a.month);
};