Skip to content

Commit 5ceb811

Browse files
authored
refactor(dashboard): extract heatmap intensity utility (JhaSourav07#606)
## Description Extracted the heatmap intensity-to-class mapping into a standalone utility and added unit tests for all intensity levels including out-of-range values. Fixes JhaSourav07#363 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [ ] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 6e09d42 + 4c393ee commit 5ceb811

3 files changed

Lines changed: 45 additions & 17 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { getIntensityColor } from './heatmapUtils';
3+
4+
describe('getIntensityColor', () => {
5+
it('returns correct color for intensity 0', () => {
6+
expect(getIntensityColor(0)).toBe('bg-gray-200 dark:bg-[#161616]');
7+
});
8+
9+
it('returns correct color for intensity 1', () => {
10+
expect(getIntensityColor(1)).toBe('bg-gray-400 dark:bg-zinc-700');
11+
});
12+
13+
it('returns correct color for intensity 2', () => {
14+
expect(getIntensityColor(2)).toBe('bg-gray-500 dark:bg-zinc-500');
15+
});
16+
17+
it('returns correct color for intensity 3', () => {
18+
expect(getIntensityColor(3)).toBe('bg-gray-700 dark:bg-zinc-300');
19+
});
20+
21+
it('returns correct color for intensity 4', () => {
22+
expect(getIntensityColor(4)).toBe('bg-black dark:bg-white');
23+
});
24+
25+
it('returns default color for out-of-range intensity', () => {
26+
expect(getIntensityColor(999)).toBe('bg-gray-200 dark:bg-[#161616]');
27+
});
28+
});

components/dashboard/Heatmap.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useEffect, useRef, useState } from 'react';
44
import { motion, AnimatePresence } from 'framer-motion';
55
import { ActivityData } from '@/types/dashboard';
6+
import { getIntensityColor } from './heatmapUtils';
67

78
const CELL = 14;
89
const GAP = 3;
@@ -37,23 +38,6 @@ export default function Heatmap({ data }: { data: ActivityData[] }) {
3738
return () => observer.disconnect();
3839
}, [naturalWidth]);
3940

40-
const getIntensityColor = (intensity: number) => {
41-
switch (intensity) {
42-
case 0:
43-
return 'bg-gray-200 dark:bg-[#161616]';
44-
case 1:
45-
return 'bg-gray-400 dark:bg-zinc-700';
46-
case 2:
47-
return 'bg-gray-500 dark:bg-zinc-500';
48-
case 3:
49-
return 'bg-gray-700 dark:bg-zinc-300';
50-
case 4:
51-
return 'bg-black dark:bg-white';
52-
default:
53-
return 'bg-gray-200 dark:bg-[#161616]';
54-
}
55-
};
56-
5741
const handleMouseEnter = (e: React.MouseEvent<HTMLDivElement>, day: ActivityData) => {
5842
const rect = e.currentTarget.getBoundingClientRect();
5943
setTooltip({
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const getIntensityColor = (intensity: number): string => {
2+
switch (intensity) {
3+
case 0:
4+
return 'bg-gray-200 dark:bg-[#161616]';
5+
case 1:
6+
return 'bg-gray-400 dark:bg-zinc-700';
7+
case 2:
8+
return 'bg-gray-500 dark:bg-zinc-500';
9+
case 3:
10+
return 'bg-gray-700 dark:bg-zinc-300';
11+
case 4:
12+
return 'bg-black dark:bg-white';
13+
default:
14+
return 'bg-gray-200 dark:bg-[#161616]';
15+
}
16+
};

0 commit comments

Comments
 (0)