Skip to content

Commit 01fb848

Browse files
Abhay056kasyaarkid15r
authored
Added Light Theme Support for Contribution HeatMap (#2072)
* Added Light Theme Support for Contribution HeatMap * Added Light Theme Support for Contribution HeatMap (Updated) * Added Light Theme Support for Contribution HeatMap (Updated) * Update color scheme for the heatmap * Update code --------- Co-authored-by: Kate Golovanova <[email protected]> Co-authored-by: Arkadii Yakovets <[email protected]> Co-authored-by: Arkadii Yakovets <[email protected]>
1 parent dfa3429 commit 01fb848

File tree

4 files changed

+75
-41
lines changed

4 files changed

+75
-41
lines changed
284 KB
Loading

frontend/src/app/members/[memberKey]/page.tsx

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import Image from 'next/image'
1010
import Link from 'next/link'
1111
import { useParams } from 'next/navigation'
12+
import { useTheme } from 'next-themes'
1213
import React, { useState, useEffect, useRef } from 'react'
1314
import { handleAppError, ErrorDisplay } from 'app/global-error'
1415
import { GET_USER_DATA } from 'server/queries/userQueries'
@@ -139,37 +140,59 @@ const UserDetailsPage: React.FC = () => {
139140
{ icon: faCodeMerge, value: user?.contributionsCount || 0, unit: 'Contribution' },
140141
]
141142

142-
const Heatmap = () => (
143-
<div className="flex flex-col gap-4">
144-
<div className="overflow-hidden rounded-lg bg-white shadow-xl dark:bg-gray-800">
145-
<div className="relative">
146-
<canvas ref={canvasRef} style={{ display: 'none' }} aria-hidden="true"></canvas>
147-
{imageLink ? (
148-
<div className="h-40 bg-[#10151c]">
149-
<Image
150-
width={100}
151-
height={100}
152-
src={imageLink || '/placeholder.svg'}
153-
className="h-full w-full object-cover object-[54%_60%]"
154-
alt="Contribution Heatmap"
155-
/>
156-
</div>
157-
) : (
158-
<div className="relative h-40 items-center justify-center bg-[#10151c]">
159-
<Image
160-
height={100}
161-
width={100}
162-
src="/img/heatmapBackground.png"
163-
className="heatmap-background-loader h-full w-full border-none object-cover object-[54%_60%]"
164-
alt="Heatmap Background"
165-
/>
166-
<div className="heatmap-loader"></div>
167-
</div>
168-
)}
143+
const Heatmap = () => {
144+
const canvasRef = useRef<HTMLCanvasElement | null>(null)
145+
const { resolvedTheme } = useTheme()
146+
const isDarkMode = (resolvedTheme ?? 'light') === 'dark'
147+
148+
useEffect(() => {
149+
if (canvasRef.current && data?.years?.length) {
150+
drawContributions(canvasRef.current, {
151+
data,
152+
username,
153+
themeName: isDarkMode ? 'dark' : 'light',
154+
})
155+
const imageURL = canvasRef.current.toDataURL()
156+
setImageLink(imageURL)
157+
}
158+
}, [isDarkMode])
159+
160+
return (
161+
<div className="flex flex-col gap-4">
162+
<div className="overflow-hidden rounded-lg bg-white dark:bg-gray-800">
163+
<div className="relative">
164+
<canvas ref={canvasRef} style={{ display: 'none' }} aria-hidden="true"></canvas>
165+
{imageLink ? (
166+
<div className="h-40">
167+
<Image
168+
width={100}
169+
height={100}
170+
src={imageLink}
171+
className="h-full w-full object-cover object-[54%_60%]"
172+
alt="Contribution Heatmap"
173+
/>
174+
</div>
175+
) : (
176+
<div className="relative h-40 items-center justify-center">
177+
<Image
178+
height={100}
179+
width={100}
180+
src={
181+
isDarkMode
182+
? '/img/heatmap-background-dark.png'
183+
: '/img/heatmap-background-light.png'
184+
}
185+
className="heatmap-background-loader h-full w-full border-none object-cover object-[54%_60%]"
186+
alt="Heatmap Background"
187+
/>
188+
<div className="heatmap-loader"></div>
189+
</div>
190+
)}
191+
</div>
169192
</div>
170193
</div>
171-
</div>
172-
)
194+
)
195+
}
173196

174197
const UserSummary = () => (
175198
<div className="mt-4 flex items-center">

frontend/src/utils/helpers/githubHeatmap.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,27 @@ export const fetchHeatmapData = async (username: string): Promise<HeatmapRespons
9090
// The code below is a modified version of 'github-contributions-canvas'
9191
// https://www.npmjs.com/package/github-contributions-canvas?activeTab=code
9292

93+
// Defined Light and Dark Themes
9394
const themes = {
94-
blue: {
95-
background: '#10151C',
96-
text: '#FFFFFF',
97-
meta: '#A6B1C1',
98-
grade4: '#5F87A8',
99-
grade3: '#46627B',
100-
grade2: '#314257',
95+
dark: {
96+
background: '#1F2937',
97+
grade0: '#2C3A4D',
10198
grade1: '#394d65',
102-
grade0: '#202A37',
99+
grade2: '#314257',
100+
grade3: '#46627B',
101+
grade4: '#5F87A8',
102+
meta: '#A6B1C1',
103+
text: '#FFFFFF',
104+
},
105+
light: {
106+
background: '#F3F4F6', // lighter background for light mode
107+
grade0: '#E7E7E6',
108+
grade1: '#8BA7C0',
109+
grade2: '#6C8EAB',
110+
grade3: '#5C7BA2',
111+
grade4: '#567498',
112+
meta: '#666666',
113+
text: '#333333',
103114
},
104115
}
105116

@@ -174,9 +185,7 @@ const yearHeight = textHeight + (boxWidth + boxMargin) * 8 + canvasMargin
174185
const scaleFactor = getPixelRatio()
175186

176187
function getTheme(opts: Options): Theme {
177-
const { themeName } = opts
178-
const name = themeName ?? 'blue'
179-
return themes[name] ?? themes.blue
188+
return themes[opts.themeName ?? 'dark']
180189
}
181190

182191
function getDateInfo(data: DataStruct, date: string) {
@@ -277,7 +286,7 @@ function drawMetaData(ctx: CanvasRenderingContext2D, opts: DrawMetadataOptions)
277286
}
278287

279288
export function drawContributions(canvas: HTMLCanvasElement, opts: Options) {
280-
const { data } = opts
289+
const { data, themeName } = opts
281290
let headerOffset = 0
282291
if (!opts.skipHeader) {
283292
headerOffset = headerHeight
@@ -302,6 +311,7 @@ export function drawContributions(canvas: HTMLCanvasElement, opts: Options) {
302311
width,
303312
height,
304313
data,
314+
themeName,
305315
})
306316
}
307317

@@ -314,6 +324,7 @@ export function drawContributions(canvas: HTMLCanvasElement, opts: Options) {
314324
offsetX,
315325
offsetY,
316326
data,
327+
themeName,
317328
})
318329
})
319330
}

0 commit comments

Comments
 (0)