Skip to content

Commit c1c89de

Browse files
committed
fix: improve reliability of the color palette retrieved from github
1 parent e07b179 commit c1c89de

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/svg.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ type FrameState = {
7373
async function fetchGithubContributionsGraphQL(
7474
userName: string,
7575
githubToken: string,
76-
): Promise<({ color: string; contributionCount: number } | null)[][]> {
76+
): Promise<
77+
({ level: 0 | 1 | 2 | 3 | 4; contributionCount: number } | null)[][]
78+
> {
7779
const query = `
7880
query($userName:String!) {
7981
user(login: $userName){
8082
contributionsCollection {
8183
contributionCalendar {
8284
weeks {
8385
contributionDays {
84-
color
86+
contributionLevel
8587
contributionCount
8688
}
8789
}
@@ -113,19 +115,28 @@ async function fetchGithubContributionsGraphQL(
113115
// Format the contribution days into a 2D array of objects (weeks x days)
114116
const weeks =
115117
json.data.user.contributionsCollection.contributionCalendar.weeks;
116-
const colors: ({ color: string; contributionCount: number } | null)[][] = [];
118+
const levels: ({
119+
level: 0 | 1 | 2 | 3 | 4;
120+
contributionCount: number;
121+
} | null)[][] = [];
117122
for (let c = 0; c < weeks.length; c++) {
118-
colors[c] = [];
123+
levels[c] = [];
119124
const days = weeks[c].contributionDays;
120125
for (let r = 0; r < days.length; r++) {
121-
colors[c][r] = {
122-
color: days[r].color,
126+
levels[c][r] = {
127+
level:
128+
(days[r].contributionLevel === "FOURTH_QUARTILE" && 4) ||
129+
(days[r].contributionLevel === "THIRD_QUARTILE" && 3) ||
130+
(days[r].contributionLevel === "SECOND_QUARTILE" && 2) ||
131+
(days[r].contributionLevel === "FIRST_QUARTILE" && 1) ||
132+
0,
123133
contributionCount: days[r].contributionCount,
124134
};
125135
}
126136
}
127-
return colors;
137+
return levels;
128138
}
139+
129140
/**
130141
* Checks if a circle and a rectangle are colliding.
131142
*
@@ -365,11 +376,10 @@ export async function generateSVG(
365376
const day = (colorDays[c] && colorDays[c][r]) || null;
366377
if (!day) continue; // skip bricks for missing days
367378

368-
let dayColorIndex = GITHUB_LIGHT.indexOf(day.color.toLowerCase());
369379
bricks.push({
370380
x: c * (BRICK_SIZE + BRICK_GAP) + PADDING,
371381
y: r * (BRICK_SIZE + BRICK_GAP) + PADDING,
372-
colorClass: dayColorIndex !== -1 ? `c${dayColorIndex}` : "c0",
382+
colorClass: `c${day.level}`,
373383
status: "visible",
374384
hasCommit: day.contributionCount > 0,
375385
});

0 commit comments

Comments
 (0)