From 6d9ae02f2a2d688839ec08bbb0b71d8cfc9c06cd Mon Sep 17 00:00:00 2001 From: Richard Bergsma Date: Sun, 15 Jun 2025 16:37:06 +0200 Subject: [PATCH] Refactor ContributionCalendar to use UTC for date calculations - Updated date handling in ContributionCalendar to work entirely in UTC, ensuring consistency with Gitea's date representation. - Adjusted logic for calculating the start date of the 52-week period and generating calendar days accordingly. - Improved comments for clarity regarding the use of UTC in date manipulations. - Cleaned up redundant code and ensured proper date formatting in the rendering of calendar days. - Minor adjustments in the development.astro file to comment out a collapsible intro section for better organization. --- src/components/ContributionCalendar.jsx | 43 ++++++++++++++----------- src/pages/[lang]/development.astro | 4 +-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/components/ContributionCalendar.jsx b/src/components/ContributionCalendar.jsx index 74bb133..63d3cf8 100644 --- a/src/components/ContributionCalendar.jsx +++ b/src/components/ContributionCalendar.jsx @@ -1,22 +1,29 @@ import React, { useEffect, useState } from 'react'; -// Helper to get all days in the last 52 weeks, starting on Monday +// Helper to get all days in the last 52 weeks, starting on Monday - UTC version function getCalendarDays() { const days = []; + + // Work entirely in UTC to match Gitea const today = new Date(); - today.setHours(0, 0, 0, 0); - // Find the most recent Monday - const start = new Date(today); - const dayOfWeek = start.getDay(); - // getDay(): 0=Sunday, 1=Monday, ..., 6=Saturday - // If today is not Monday, go back to the previous Monday - const offset = (dayOfWeek === 0 ? -6 : 1 - dayOfWeek); // If Sunday, go back 6 days; else, go back to Monday - start.setDate(start.getDate() - (7 * 51) + offset); + const todayUTC = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate())); + + // Find the Monday that starts our 52-week period + const startDate = new Date(todayUTC); + startDate.setUTCDate(todayUTC.getUTCDate() - (52 * 7 - 1)); + + // Adjust startDate to the Monday of that week (using UTC day) + const startDayOfWeek = startDate.getUTCDay(); // 0 = Sunday, 1 = Monday, etc. + const daysToMonday = startDayOfWeek === 0 ? -6 : 1 - startDayOfWeek; + startDate.setUTCDate(startDate.getUTCDate() + daysToMonday); + + // Generate exactly 52 weeks (364 days) starting from that Monday for (let i = 0; i < 7 * 52; i++) { - const d = new Date(start); - d.setDate(start.getDate() + i); + const d = new Date(startDate); + d.setUTCDate(startDate.getUTCDate() + i); days.push(d); } + return days; } @@ -95,6 +102,8 @@ export default function ContributionCalendar({ data }) { const visibleDays = days.slice(startWeek * 7, 52 * 7); const visibleMonthLabels = monthLabels.filter(l => l.week >= startWeek); + const calendarRef = React.useRef(null); + // When expanding to full year on mobile, scroll to the right (most recent weeks) useEffect(() => { if (!isMobile) return; @@ -111,8 +120,6 @@ export default function ContributionCalendar({ data }) { // Get max count for scaling (optional, for more dynamic color) // const max = Math.max(...Object.values(data)); - const calendarRef = React.useRef(null); - return (
{Array.from({ length: weeksToShow }).map((_, weekIdx) => (
- {/* Days (rows) */} + {/* Days (rows) - Using UTC for consistency with Gitea */} {Array.from({ length: 7 }).map((_, dayIdx) => { - // Shift the dayIdx so that Monday is the first row and Sunday is the last - const shiftedDayIdx = (dayIdx + 1) % 7; - const day = visibleDays[weekIdx * 7 + shiftedDayIdx]; + const day = visibleDays[weekIdx * 7 + dayIdx]; if (!day) return
; + const dateStr = day.toISOString().slice(0, 10); const count = data[dateStr] || 0; + return (
); -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/pages/[lang]/development.astro b/src/pages/[lang]/development.astro index 2ecf2b0..d08d58f 100644 --- a/src/pages/[lang]/development.astro +++ b/src/pages/[lang]/development.astro @@ -128,10 +128,10 @@ const averagePerDay = contributionDays > 0 ? (totalContributions / contributionD

- +