|
| 1 | +// GitHeatMap.tsx |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import Day from './Day'; |
| 4 | +import style from '../styles/git.module.scss'; |
| 5 | + |
| 6 | +type ByDate = { |
| 7 | + [date: string]: { [source: string]: number }; |
| 8 | +}; |
| 9 | + |
| 10 | +type BySourceTotal = { |
| 11 | + [source: string]: { commits: number; issues: number; merge_requests: number }; |
| 12 | +}; |
| 13 | + |
| 14 | +type ContributionsResponse = { |
| 15 | + by_date: ByDate; |
| 16 | + by_source_total: BySourceTotal; |
| 17 | +}; |
| 18 | + |
| 19 | +type SumUpDay = { |
| 20 | + date: string; |
| 21 | + count: number; |
| 22 | + sources: { [source: string]: number }; |
| 23 | +}; |
| 24 | + |
| 25 | +const colors = [ |
| 26 | + '#2b2c3d', // 0 |
| 27 | + '#6b57a1', // 1 |
| 28 | + '#936cc9', // 2 |
| 29 | + '#ab83e3', // 3 |
| 30 | + '#bb9af7', // 4 |
| 31 | + '#a07ce0', // 5 |
| 32 | + '#8566c2', // 6+ |
| 33 | +]; |
| 34 | + |
| 35 | +const GitHeatMap = () => { |
| 36 | + const [data, setData] = useState<ContributionsResponse | null>(null); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + fetch('https://git-contrib.mathis-burger.de/api/activity', { |
| 40 | + mode: 'cors', |
| 41 | + }) |
| 42 | + .then((res) => res.json()) |
| 43 | + .then((json: ContributionsResponse) => setData(json)) |
| 44 | + .catch((error) => console.error('Error fetching data:', error)); |
| 45 | + }, []); |
| 46 | + |
| 47 | + if (!data) return null; |
| 48 | + |
| 49 | + const year = new Date().getFullYear(); |
| 50 | + const firstDay = new Date(year, 0, 1); |
| 51 | + const lastDay = new Date(year, 11, 31); |
| 52 | + |
| 53 | + const days: SumUpDay[] = []; |
| 54 | + for (let d = new Date(firstDay); d <= lastDay; d.setDate(d.getDate() + 1)) { |
| 55 | + const iso = d.toISOString().slice(0, 10); |
| 56 | + const sources = data.by_date[iso] || {}; |
| 57 | + const total = Object.values(sources).reduce((a, b) => a + b, 0); |
| 58 | + days.push({ date: iso, count: total, sources }); |
| 59 | + } |
| 60 | + |
| 61 | + const total_criteria = ['commits', 'issues', 'merge_requests'] as const; |
| 62 | + |
| 63 | + const getTotal = (criterion: keyof BySourceTotal[string]) => |
| 64 | + Object.values(data.by_source_total).reduce( |
| 65 | + (acc, curr) => acc + curr[criterion], |
| 66 | + 0, |
| 67 | + ); |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className={style.overallContainer}> |
| 71 | + <div className={style.box}> |
| 72 | + {total_criteria.map((criterion) => ( |
| 73 | + <div key={criterion} className={style.elementBox}> |
| 74 | + <p>{criterion.replaceAll('_', ' ')}</p> |
| 75 | + <h2>{getTotal(criterion)}</h2> |
| 76 | + |
| 77 | + <ul |
| 78 | + style={{ |
| 79 | + listStyle: 'none', |
| 80 | + padding: 0, |
| 81 | + marginTop: '1em', |
| 82 | + fontSize: '0.9em', |
| 83 | + }} |
| 84 | + > |
| 85 | + {Object.entries(data.by_source_total).map(([source, values]) => ( |
| 86 | + <li key={source}> |
| 87 | + {source}: {values[criterion]} |
| 88 | + </li> |
| 89 | + ))} |
| 90 | + </ul> |
| 91 | + </div> |
| 92 | + ))} |
| 93 | + </div> |
| 94 | + <div className={style.chart}> |
| 95 | + {days.map((day) => ( |
| 96 | + <Day |
| 97 | + key={day.date} |
| 98 | + date={day.date} |
| 99 | + count={day.count} |
| 100 | + sources={day.sources} |
| 101 | + /> |
| 102 | + ))} |
| 103 | + </div> |
| 104 | + <div className={style.legend}> |
| 105 | + {colors.map((color, idx) => ( |
| 106 | + <div key={idx} className={style.legendItem}> |
| 107 | + <div |
| 108 | + className={style.legendColor} |
| 109 | + style={{ backgroundColor: color }} |
| 110 | + /> |
| 111 | + <span>{idx === 6 ? '6+' : idx}</span> |
| 112 | + </div> |
| 113 | + ))} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +export default GitHeatMap; |
0 commit comments