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.
This commit is contained in:
@@ -1,22 +1,29 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
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() {
|
function getCalendarDays() {
|
||||||
const days = [];
|
const days = [];
|
||||||
|
|
||||||
|
// Work entirely in UTC to match Gitea
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
today.setHours(0, 0, 0, 0);
|
const todayUTC = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate()));
|
||||||
// Find the most recent Monday
|
|
||||||
const start = new Date(today);
|
// Find the Monday that starts our 52-week period
|
||||||
const dayOfWeek = start.getDay();
|
const startDate = new Date(todayUTC);
|
||||||
// getDay(): 0=Sunday, 1=Monday, ..., 6=Saturday
|
startDate.setUTCDate(todayUTC.getUTCDate() - (52 * 7 - 1));
|
||||||
// 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
|
// Adjust startDate to the Monday of that week (using UTC day)
|
||||||
start.setDate(start.getDate() - (7 * 51) + offset);
|
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++) {
|
for (let i = 0; i < 7 * 52; i++) {
|
||||||
const d = new Date(start);
|
const d = new Date(startDate);
|
||||||
d.setDate(start.getDate() + i);
|
d.setUTCDate(startDate.getUTCDate() + i);
|
||||||
days.push(d);
|
days.push(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
return days;
|
return days;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +102,8 @@ export default function ContributionCalendar({ data }) {
|
|||||||
const visibleDays = days.slice(startWeek * 7, 52 * 7);
|
const visibleDays = days.slice(startWeek * 7, 52 * 7);
|
||||||
const visibleMonthLabels = monthLabels.filter(l => l.week >= startWeek);
|
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)
|
// When expanding to full year on mobile, scroll to the right (most recent weeks)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isMobile) return;
|
if (!isMobile) return;
|
||||||
@@ -111,8 +120,6 @@ export default function ContributionCalendar({ data }) {
|
|||||||
// Get max count for scaling (optional, for more dynamic color)
|
// Get max count for scaling (optional, for more dynamic color)
|
||||||
// const max = Math.max(...Object.values(data));
|
// const max = Math.max(...Object.values(data));
|
||||||
|
|
||||||
const calendarRef = React.useRef(null);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
@@ -183,14 +190,14 @@ export default function ContributionCalendar({ data }) {
|
|||||||
<div style={{ display: 'flex' }}>
|
<div style={{ display: 'flex' }}>
|
||||||
{Array.from({ length: weeksToShow }).map((_, weekIdx) => (
|
{Array.from({ length: weeksToShow }).map((_, weekIdx) => (
|
||||||
<div key={weekIdx} style={{ display: 'flex', flexDirection: 'column', flex: '0 0 14px' }}>
|
<div key={weekIdx} style={{ display: 'flex', flexDirection: 'column', flex: '0 0 14px' }}>
|
||||||
{/* Days (rows) */}
|
{/* Days (rows) - Using UTC for consistency with Gitea */}
|
||||||
{Array.from({ length: 7 }).map((_, dayIdx) => {
|
{Array.from({ length: 7 }).map((_, dayIdx) => {
|
||||||
// Shift the dayIdx so that Monday is the first row and Sunday is the last
|
const day = visibleDays[weekIdx * 7 + dayIdx];
|
||||||
const shiftedDayIdx = (dayIdx + 1) % 7;
|
|
||||||
const day = visibleDays[weekIdx * 7 + shiftedDayIdx];
|
|
||||||
if (!day) return <div key={dayIdx} style={{ width: 12, height: 12, margin: 1 }} />;
|
if (!day) return <div key={dayIdx} style={{ width: 12, height: 12, margin: 1 }} />;
|
||||||
|
|
||||||
const dateStr = day.toISOString().slice(0, 10);
|
const dateStr = day.toISOString().slice(0, 10);
|
||||||
const count = data[dateStr] || 0;
|
const count = data[dateStr] || 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={dateStr}
|
key={dateStr}
|
||||||
|
@@ -128,10 +128,10 @@ const averagePerDay = contributionDays > 0 ? (totalContributions / contributionD
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Collapsible Intro -->
|
<!-- Collapsible Intro
|
||||||
<div class="backdrop-blur-sm bg-white/70 dark:bg-slate-900/70 rounded-xl shadow p-6 mb-8">
|
<div class="backdrop-blur-sm bg-white/70 dark:bg-slate-900/70 rounded-xl shadow p-6 mb-8">
|
||||||
<CollapsibleIntro text={t.development.intro} client:visible />
|
<CollapsibleIntro text={t.development.intro} client:visible />
|
||||||
</div>
|
</div>-->
|
||||||
|
|
||||||
<!-- Stats Overview -->
|
<!-- Stats Overview -->
|
||||||
<div class="grid md:grid-cols-3 gap-6 mb-8">
|
<div class="grid md:grid-cols-3 gap-6 mb-8">
|
||||||
|
Reference in New Issue
Block a user