| import { Activity } from "../types/heatmap"; |
|
|
| export const aggregateToWeeklyData = (dailyData: Activity[]): Activity[] => { |
| if (!dailyData || dailyData.length === 0) return []; |
|
|
| |
| const weeklyMap = new Map<string, { count: number; level: number; dates: string[]; items: string[] }>(); |
|
|
| for (const dayActivity of dailyData) { |
| const date = new Date(dayActivity.date); |
| |
| const weekStart = new Date(date); |
| weekStart.setDate(date.getDate() - date.getDay()); |
| const weekKey = weekStart.toISOString().split('T')[0]; |
|
|
| if (!weeklyMap.has(weekKey)) { |
| weeklyMap.set(weekKey, { count: 0, level: 0, dates: [], items: [] }); |
| } |
|
|
| const weekData = weeklyMap.get(weekKey)!; |
| weekData.count += dayActivity.count; |
| weekData.level = Math.max(weekData.level, dayActivity.level); |
| weekData.dates.push(dayActivity.date); |
| |
| |
| if (dayActivity.items) { |
| weekData.items.push(...dayActivity.items); |
| } |
| } |
|
|
| |
| const weeklyData: Activity[] = []; |
| |
| weeklyMap.forEach((weekInfo, weekStartDate) => { |
| weeklyData.push({ |
| date: weekStartDate, |
| count: weekInfo.count, |
| level: weekInfo.level, |
| items: weekInfo.items, |
| }); |
| }); |
|
|
| |
| weeklyData.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()); |
|
|
| return weeklyData; |
| }; |
|
|
| export const getWeekDateRange = (weekStartDate: string): string => { |
| const startDate = new Date(weekStartDate); |
| const endDate = new Date(startDate); |
| endDate.setDate(startDate.getDate() + 6); |
|
|
| const formatDate = (date: Date) => { |
| return date.toLocaleDateString('en-US', { |
| month: 'short', |
| day: 'numeric' |
| }); |
| }; |
|
|
| return `${formatDate(startDate)} - ${formatDate(endDate)}`; |
| }; |