From af9525897c347cfebb04f9649445518f21378b9c Mon Sep 17 00:00:00 2001 From: JavidSumra Date: Sat, 14 Dec 2024 12:37:17 +0530 Subject: [PATCH 1/6] Fix Calendar View Issue --- components/contributors/ActivityCalendarGitHub.tsx | 5 +++-- lib/api.ts | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/components/contributors/ActivityCalendarGitHub.tsx b/components/contributors/ActivityCalendarGitHub.tsx index e185a632..69eec927 100644 --- a/components/contributors/ActivityCalendarGitHub.tsx +++ b/components/contributors/ActivityCalendarGitHub.tsx @@ -24,6 +24,7 @@ export default function ActivityCalendarGit({ const { theme } = useTheme(); const getCalendarData = (year: number) => { + console.log(year); const currentYear = year; let dates = []; let date = new Date(`01-01-${year}`); @@ -73,7 +74,7 @@ export default function ActivityCalendarGit({ const [year, setYear] = useState(0); const [isOpen, setIsOpen] = useState(false); const [activityData, setActivityData] = useState({}); - + // console.log(getCalendarData(year)); return (
{isBrowser && ( @@ -82,7 +83,7 @@ export default function ActivityCalendarGit({ { const date = new Date(activity.time).toISOString().split("T")[0]; + if (!acc[date]) { acc[date] = { count: 0, @@ -261,9 +263,10 @@ function getCalendarData(activity: Activity[]) { }, {} as Record, ); - return [...Array(365)].map((_, i) => { + + return [...Array(Object.keys(calendarData).length)].map((_, i) => { // Current Date - i - const iReverse = 365 - i; + const iReverse = Object.keys(calendarData).length - i; const date = new Date( new Date().getTime() - iReverse * 24 * 60 * 60 * 1000, ); From ad61cbfdfd14c7d564f55fa5eee6ddf4c86ac79f Mon Sep 17 00:00:00 2001 From: JavidSumra Date: Sat, 14 Dec 2024 12:45:46 +0530 Subject: [PATCH 2/6] Remove Unnecessary Console --- components/contributors/ActivityCalendarGitHub.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/components/contributors/ActivityCalendarGitHub.tsx b/components/contributors/ActivityCalendarGitHub.tsx index 69eec927..5d417e1c 100644 --- a/components/contributors/ActivityCalendarGitHub.tsx +++ b/components/contributors/ActivityCalendarGitHub.tsx @@ -24,7 +24,6 @@ export default function ActivityCalendarGit({ const { theme } = useTheme(); const getCalendarData = (year: number) => { - console.log(year); const currentYear = year; let dates = []; let date = new Date(`01-01-${year}`); From b281c73f32d66316f391a88e4bff22231a204ac9 Mon Sep 17 00:00:00 2001 From: JavidSumra Date: Sat, 14 Dec 2024 17:15:25 +0530 Subject: [PATCH 3/6] Fix Rendering Logic --- .../contributors/ActivityCalendarGitHub.tsx | 56 +++++++------------ 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/components/contributors/ActivityCalendarGitHub.tsx b/components/contributors/ActivityCalendarGitHub.tsx index 5d417e1c..f7abb706 100644 --- a/components/contributors/ActivityCalendarGitHub.tsx +++ b/components/contributors/ActivityCalendarGitHub.tsx @@ -70,7 +70,7 @@ export default function ActivityCalendarGit({ const yearDiff = Number(new Date().getFullYear()) - getFirstContribYear(); const yearsList = lastNYears(yearDiff); - const [year, setYear] = useState(0); + const [year, setYear] = useState(new Date().getFullYear()); const [isOpen, setIsOpen] = useState(false); const [activityData, setActivityData] = useState({}); // console.log(getCalendarData(year)); @@ -78,42 +78,24 @@ export default function ActivityCalendarGit({
{isBrowser && (
- {year === 0 ? ( - (data) => { - setIsOpen(true); - setActivityData(data); - }, - }} - labels={{ - totalCount: "{{count}} contributions in the last year", - }} - /> - ) : ( - (data) => { - setIsOpen(true); - setActivityData(data); - }, - }} - /> - )} + (data) => { + setIsOpen(true); + setActivityData(data); + }, + }} + labels={{ + totalCount: "{{count}} contributions in {{year}}", + }} + /> Date: Mon, 16 Dec 2024 13:58:16 +0530 Subject: [PATCH 4/6] Fix Last year data view --- .../contributors/ActivityCalendarGitHub.tsx | 66 +++++++++++++------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/components/contributors/ActivityCalendarGitHub.tsx b/components/contributors/ActivityCalendarGitHub.tsx index f7abb706..1acc59d0 100644 --- a/components/contributors/ActivityCalendarGitHub.tsx +++ b/components/contributors/ActivityCalendarGitHub.tsx @@ -24,31 +24,57 @@ export default function ActivityCalendarGit({ const { theme } = useTheme(); const getCalendarData = (year: number) => { - const currentYear = year; let dates = []; - let date = new Date(`01-01-${year}`); - date.setDate(date.getDate() + 1); - while (date.getFullYear() === currentYear) { + let currentYear: number; + + if (year === 0) { + currentYear = new Date().getFullYear(); + let date = new Date(); + date.setDate(date.getDate() - 365); + + for (let i = 0; i < 365; i++) { + dates.push({ + date: new Date(date).toISOString().split("T")[0], + count: 0, + level: 0, + }); + date.setDate(date.getDate() + 1); + } + } else { + currentYear = year; + let date = new Date(`01-01-${year}`); + date.setDate(date.getDate() + 1); + + while (date.getFullYear() === currentYear) { + dates.push({ + date: new Date(date).toISOString().split("T")[0], + count: 0, + level: 0, + }); + date.setDate(date.getDate() + 1); + } dates.push({ date: new Date(date).toISOString().split("T")[0], count: 0, level: 0, }); - date.setDate(date.getDate() + 1); } - dates.push({ - date: new Date(date).toISOString().split("T")[0], - count: 0, - level: 0, - }); - let calDates = calendarData.filter( - (d: any) => d.date.slice(0, 4) === String(currentYear), - ); + let calDates = calendarData.filter((d: any) => { + if (year === 0) { + return dates.some((dateObj) => dateObj.date === d.date); + } else { + return d.date.slice(0, 4) === String(currentYear); + } + }); - for (let i = 0; i < dates.length; i++) - for (let j = 0; j < calDates.length; j++) - if (dates[i].date === calDates[j].date) dates[i] = calDates[j]; + for (let i = 0; i < dates.length; i++) { + for (let j = 0; j < calDates.length; j++) { + if (dates[i].date === calDates[j].date) { + dates[i] = calDates[j]; + } + } + } return dates; }; @@ -70,10 +96,9 @@ export default function ActivityCalendarGit({ const yearDiff = Number(new Date().getFullYear()) - getFirstContribYear(); const yearsList = lastNYears(yearDiff); - const [year, setYear] = useState(new Date().getFullYear()); + const [year, setYear] = useState(0); const [isOpen, setIsOpen] = useState(false); const [activityData, setActivityData] = useState({}); - // console.log(getCalendarData(year)); return (
{isBrowser && ( @@ -93,7 +118,10 @@ export default function ActivityCalendarGit({ }, }} labels={{ - totalCount: "{{count}} contributions in {{year}}", + totalCount: + year === 0 + ? "{{count}} contributions in the last year" + : "{{count}} contributions in {{year}}", }} /> From d1126947d44ec40f2fcf9074708a47dc14610ed8 Mon Sep 17 00:00:00 2001 From: JavidSumra Date: Fri, 20 Dec 2024 16:26:30 +0530 Subject: [PATCH 5/6] Fix Logic to Show Data --- components/contributors/ActivityCalendarGitHub.tsx | 4 ++++ lib/api.ts | 9 ++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/components/contributors/ActivityCalendarGitHub.tsx b/components/contributors/ActivityCalendarGitHub.tsx index 1acc59d0..d7fd9838 100644 --- a/components/contributors/ActivityCalendarGitHub.tsx +++ b/components/contributors/ActivityCalendarGitHub.tsx @@ -14,6 +14,9 @@ export default function ActivityCalendarGit({ // following when attempted to render on server side. // // calcTextDimensions() requires browser APIs + calendarData = calendarData.sort( + (a: any, b: any) => new Date(a.date).getTime() - new Date(b.date).getTime(), + ); const [isBrowser, setIsBrowser] = useState(false); useEffect(() => { setIsBrowser( @@ -94,6 +97,7 @@ export default function ActivityCalendarGit({ }; const yearDiff = Number(new Date().getFullYear()) - getFirstContribYear(); + console.log(getFirstContribYear()); const yearsList = lastNYears(yearDiff); const [year, setYear] = useState(0); diff --git a/lib/api.ts b/lib/api.ts index 4fe5b028..008329cb 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -264,12 +264,11 @@ function getCalendarData(activity: Activity[]) { {} as Record, ); - return [...Array(Object.keys(calendarData).length)].map((_, i) => { + const dateKeyArr = Object.keys(calendarData); + return [...Array(dateKeyArr.length)].map((_, i) => { // Current Date - i - const iReverse = Object.keys(calendarData).length - i; - const date = new Date( - new Date().getTime() - iReverse * 24 * 60 * 60 * 1000, - ); + const date = new Date(dateKeyArr[i]); + // yyyy-mm-dd const dateString = `${date.getFullYear()}-${padZero( date.getMonth() + 1, From 3c5c10283bb6e3849172f7506026ba9d1ec645dd Mon Sep 17 00:00:00 2001 From: JavidSumra Date: Fri, 20 Dec 2024 16:39:45 +0530 Subject: [PATCH 6/6] Remove console --- components/contributors/ActivityCalendarGitHub.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/components/contributors/ActivityCalendarGitHub.tsx b/components/contributors/ActivityCalendarGitHub.tsx index d7fd9838..36c6ebc8 100644 --- a/components/contributors/ActivityCalendarGitHub.tsx +++ b/components/contributors/ActivityCalendarGitHub.tsx @@ -97,7 +97,6 @@ export default function ActivityCalendarGit({ }; const yearDiff = Number(new Date().getFullYear()) - getFirstContribYear(); - console.log(getFirstContribYear()); const yearsList = lastNYears(yearDiff); const [year, setYear] = useState(0);