'use client' import HeatMap from '@uiw/react-heat-map' import { Habit } from '@/lib/types' import { getDateInTimezone } from '@/lib/utils' import { useSettings } from '@/hooks/useSettings' 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 } = useSettings() // Get start date (30 days ago) const now = getDateInTimezone(new Date(), settings.system.timezone) const startDate = now startDate.setDate(now.getDate() - 30) return (

Habit Completion Heatmap

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