49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { TemplateGeneration } from '@/lib/api/template-generations';
|
|
|
|
export interface GroupedData {
|
|
[date: string]: TemplateGeneration[];
|
|
}
|
|
|
|
export const groupByDate = (data: TemplateGeneration[]): GroupedData => {
|
|
const groups: GroupedData = {};
|
|
|
|
data.forEach(item => {
|
|
const date = new Date(item.createdAt);
|
|
const dateKey = date.toLocaleDateString('en-US', {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
weekday: 'short'
|
|
});
|
|
|
|
if (!groups[dateKey]) {
|
|
groups[dateKey] = [];
|
|
}
|
|
groups[dateKey].push(item);
|
|
});
|
|
|
|
return groups;
|
|
};
|
|
|
|
export const formatDateLabel = (dateStr: string): string => {
|
|
const date = new Date(dateStr);
|
|
const today = new Date();
|
|
const yesterday = new Date(today);
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
const dateOnly = new Date(date.toDateString());
|
|
const todayOnly = new Date(today.toDateString());
|
|
const yesterdayOnly = new Date(yesterday.toDateString());
|
|
|
|
if (dateOnly.getTime() === todayOnly.getTime()) {
|
|
return 'Today';
|
|
} else if (dateOnly.getTime() === yesterdayOnly.getTime()) {
|
|
return 'Yesterday';
|
|
} else {
|
|
return date.toLocaleDateString('en-US', {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
weekday: 'short'
|
|
});
|
|
}
|
|
};
|