|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useMemo, useState } from 'react'; |
| 4 | +import CalendarHeatmap from 'react-calendar-heatmap'; |
| 5 | +import 'react-calendar-heatmap/dist/styles.css'; |
| 6 | +import { Tooltip } from 'react-tooltip'; |
| 7 | +import 'react-tooltip/dist/react-tooltip.css'; |
| 8 | +import { fetchDailyStats } from '@/services/heatmap/dailyStat.service'; |
| 9 | + |
| 10 | +type HeatmapValue = { date: string; count: number }; |
| 11 | + |
| 12 | +function endOfWeek(date = new Date()) { |
| 13 | + const d = new Date(date); |
| 14 | + d.setDate(d.getDate() + (6 - d.getDay())); |
| 15 | + return d; |
| 16 | +} |
| 17 | + |
| 18 | +type Props = { uid?: string }; |
| 19 | + |
| 20 | +export default function GrassHeatmap({ uid }: Props) { |
| 21 | + const endDate = useMemo(() => endOfWeek(new Date()), []); |
| 22 | + const startDate = useMemo(() => { |
| 23 | + const d = new Date(endDate); |
| 24 | + d.setFullYear(d.getFullYear() - 1); |
| 25 | + d.setDate(d.getDate() + 1); |
| 26 | + return d; |
| 27 | + }, [endDate]); |
| 28 | + |
| 29 | + const [values, setValues] = useState< |
| 30 | + { date: string; total: number; tilCount?: number; todoDoneCount?: number }[] |
| 31 | + >([]); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (!uid) return; |
| 35 | + (async () => { |
| 36 | + const stats = await fetchDailyStats(uid); |
| 37 | + setValues(stats); |
| 38 | + })(); |
| 39 | + }, [uid]); |
| 40 | + |
| 41 | + const byDate = useMemo(() => { |
| 42 | + const m = new Map< |
| 43 | + string, |
| 44 | + { tilCount: number; todoDoneCount: number; total: number } |
| 45 | + >(); |
| 46 | + for (const s of values) { |
| 47 | + m.set(s.date, { |
| 48 | + tilCount: s.tilCount ?? 0, |
| 49 | + todoDoneCount: s.todoDoneCount ?? 0, |
| 50 | + total: s.total ?? 0, |
| 51 | + }); |
| 52 | + } |
| 53 | + return m; |
| 54 | + }, [values]); |
| 55 | + |
| 56 | + const heatmapValues: HeatmapValue[] = useMemo( |
| 57 | + () => values.map((s) => ({ date: s.date, count: s.total ?? 0 })), |
| 58 | + [values] |
| 59 | + ); |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <div className="overflow-x-auto"> |
| 64 | + <div className="min-w-max"> |
| 65 | + <CalendarHeatmap |
| 66 | + startDate={startDate} |
| 67 | + endDate={endDate} |
| 68 | + values={heatmapValues} |
| 69 | + gutterSize={2} |
| 70 | + showWeekdayLabels |
| 71 | + classForValue={(value) => { |
| 72 | + if (!value) return 'grass-empty'; |
| 73 | + if (value.count >= 4) return 'grass-4'; |
| 74 | + if (value.count === 3) return 'grass-3'; |
| 75 | + if (value.count === 2) return 'grass-2'; |
| 76 | + return 'grass-1'; |
| 77 | + }} |
| 78 | + tooltipDataAttrs={(value) => { |
| 79 | + if (!value?.date) |
| 80 | + return { 'data-tooltip-id': '', 'data-tooltip-html': '' }; |
| 81 | + const d = byDate.get(value.date); |
| 82 | + |
| 83 | + const til = d?.tilCount ?? 0; |
| 84 | + const todo = d?.todoDoneCount ?? 0; |
| 85 | + const total = d?.total ?? 0; |
| 86 | + |
| 87 | + return { |
| 88 | + 'data-tooltip-id': 'grass-tip', |
| 89 | + // HTML 툴팁 (작은 박스) |
| 90 | + 'data-tooltip-html': |
| 91 | + total === 0 |
| 92 | + ? `<div style="font-size:12px"><b>${value.date}</b><br/>기록 없음</div>` |
| 93 | + : `<div style="font-size:12px"> |
| 94 | + <b>${value.date}</b><br/> |
| 95 | + 📘 TIL: ${til}개<br/> |
| 96 | + ✅ Plan: ${todo}개<br/> |
| 97 | + 🔥 합계: ${total}개 |
| 98 | + </div>`, |
| 99 | + }; |
| 100 | + }} |
| 101 | + /> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + {/* 툴팁 컴포넌트 */} |
| 106 | + <Tooltip |
| 107 | + id="grass-tip" |
| 108 | + place="top" |
| 109 | + className="!rounded-lg !bg-black/80 !px-3 !py-2 !text-xs !text-white" |
| 110 | + /> |
| 111 | + {/* 범례 */} |
| 112 | + <div className="mt-4 flex items-center justify-end gap-2 text-xs text-slate-500"> |
| 113 | + <span>Less</span> |
| 114 | + <span className="h-3 w-3 rounded-sm bg-slate-100" /> |
| 115 | + <span className="h-3 w-3 rounded-sm bg-emerald-200" /> |
| 116 | + <span className="h-3 w-3 rounded-sm bg-emerald-400" /> |
| 117 | + <span className="h-3 w-3 rounded-sm bg-emerald-600" /> |
| 118 | + <span className="h-3 w-3 rounded-sm bg-emerald-800" /> |
| 119 | + <span>More</span> |
| 120 | + </div> |
| 121 | + </> |
| 122 | + ); |
| 123 | +} |
0 commit comments