File size: 1,779 Bytes
cd201c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Activity } from "../types/heatmap";

export const aggregateToWeeklyData = (dailyData: Activity[]): Activity[] => {
  if (!dailyData || dailyData.length === 0) return [];

  // Create a map to group activities by week
  const weeklyMap = new Map<string, { count: number; level: number; dates: string[] }>();

  for (const dayActivity of dailyData) {
    const date = new Date(dayActivity.date);
    // Get the Sunday of this week (week start)
    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: [] });
    }

    const weekData = weeklyMap.get(weekKey)!;
    weekData.count += dayActivity.count;
    weekData.level = Math.max(weekData.level, dayActivity.level);
    weekData.dates.push(dayActivity.date);
  }

  // Convert to true weekly data - one entry per week, not per day
  const weeklyData: Activity[] = [];
  
  weeklyMap.forEach((weekInfo, weekStartDate) => {
    weeklyData.push({
      date: weekStartDate, // Use the week start date
      count: weekInfo.count,
      level: weekInfo.level,
    });
  });

  // Sort by date to ensure proper order
  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)}`;
};