b7f52be4c9
CI / Run CI (push) Has been cancelled
CI / check-backend (push) Has been cancelled
CI / check-frontend (push) Has been cancelled
CI / tests (push) Has been cancelled
CI / e2e-tests (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { IThread } from 'src/types';
|
|
|
|
export const groupByDate = (data: IThread[]) => {
|
|
const groupedData: { [key: string]: IThread[] } = {};
|
|
|
|
const today = new Date();
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
[...data]
|
|
.sort(
|
|
(a, b) =>
|
|
new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
|
|
)
|
|
.forEach((item) => {
|
|
const threadDate = new Date(item.createdAt);
|
|
threadDate.setHours(0, 0, 0, 0);
|
|
|
|
const daysDiff = Math.floor(
|
|
(today.getTime() - threadDate.getTime()) / 86400000
|
|
);
|
|
const locale = navigator.language;
|
|
|
|
let category: string;
|
|
if (daysDiff === 0) {
|
|
category = 'Today';
|
|
} else if (daysDiff === 1) {
|
|
category = 'Yesterday';
|
|
} else if (daysDiff <= 7) {
|
|
category = 'Previous 7 days';
|
|
} else if (daysDiff <= 30) {
|
|
category = 'Previous 30 days';
|
|
} else {
|
|
category = threadDate.toLocaleString(locale, {
|
|
month: 'long',
|
|
year: 'numeric'
|
|
});
|
|
}
|
|
|
|
groupedData[category] ??= [];
|
|
groupedData[category].push(item);
|
|
});
|
|
|
|
return groupedData;
|
|
};
|