'use client' import HeatMap from '@uiw/react-heat-map' import { Habit } from '@/lib/types' import { getNow } from '@/lib/utils' import { useAtom } from 'jotai' import { settingsAtom } from '@/lib/atoms' interface HabitHeatmapProps { habits: Habit[] } export default function HabitHeatmap({ habits }: HabitHeatmapProps) { // Aggregate all habit completions into a count per day const completionCounts = habits.reduce((acc: { [key: string]: number }, habit) => { habit.completions.forEach(date => { // Convert date format from ISO (YYYY-MM-DD) to YYYY/MM/DD for the heatmap const formattedDate = date.replace(/-/g, '/') acc[formattedDate] = (acc[formattedDate] || 0) + 1 }) return acc }, {}) // Convert to the format expected by the heatmap const value = Object.entries(completionCounts).map(([date, count]) => ({ date, count })) const [settings] = useAtom(settingsAtom) // Get start date (30 days ago) const now = getNow({ timezone: settings.system.timezone }) const startDate = now.minus({ days: 30 }).toJSDate() return (

Habit Completion Heatmap

{ return ( {`${data.date}: ${data.count || 0} habits completed`} ); }} />
) }