|
|
import { Activity } from "../types/heatmap"; |
|
|
|
|
|
export const aggregateToWeeklyData = (dailyData: Activity[]): Activity[] => { |
|
|
if (!dailyData || dailyData.length === 0) return []; |
|
|
|
|
|
const weeklyData: Activity[] = []; |
|
|
let currentWeekStart: Date | null = null; |
|
|
let currentWeekCount = 0; |
|
|
let currentWeekLevel = 0; |
|
|
|
|
|
for (const dayActivity of dailyData) { |
|
|
const date = new Date(dayActivity.date); |
|
|
const dayOfWeek = date.getDay(); |
|
|
|
|
|
|
|
|
if (dayOfWeek === 0 || currentWeekStart === null) { |
|
|
|
|
|
if (currentWeekStart !== null) { |
|
|
weeklyData.push({ |
|
|
date: currentWeekStart.toISOString().split('T')[0], |
|
|
count: currentWeekCount, |
|
|
level: currentWeekLevel, |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
currentWeekStart = new Date(date); |
|
|
currentWeekCount = dayActivity.count; |
|
|
currentWeekLevel = dayActivity.level; |
|
|
} else { |
|
|
|
|
|
currentWeekCount += dayActivity.count; |
|
|
|
|
|
currentWeekLevel = Math.max(currentWeekLevel, dayActivity.level); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (currentWeekStart !== null) { |
|
|
weeklyData.push({ |
|
|
date: currentWeekStart.toISOString().split('T')[0], |
|
|
count: currentWeekCount, |
|
|
level: currentWeekLevel, |
|
|
}); |
|
|
} |
|
|
|
|
|
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)}`; |
|
|
}; |