mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-20 22:24:28 +01:00
performance optimization via atoms (#108)
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
ContextMenuItem,
|
||||
ContextMenuTrigger,
|
||||
} from "@/components/ui/context-menu"
|
||||
import { cn, getHabitFreq } from '@/lib/utils'
|
||||
import { cn } from '@/lib/utils'
|
||||
import Link from 'next/link'
|
||||
import { useState } from 'react'
|
||||
import { useAtom } from 'jotai'
|
||||
@@ -57,7 +57,7 @@ const ItemSection = ({
|
||||
settings,
|
||||
setBrowserSettings,
|
||||
}: ItemSectionProps) => {
|
||||
const { completeHabit, undoComplete, saveHabit } = useHabits();
|
||||
const { completeHabit, undoComplete, saveHabit, habitFreqMap } = useHabits();
|
||||
const [_, setPomo] = useAtom(pomodoroAtom);
|
||||
|
||||
if (items.length === 0) {
|
||||
@@ -117,8 +117,8 @@ const ItemSection = ({
|
||||
}
|
||||
|
||||
// Then by frequency (daily first)
|
||||
const aFreq = getHabitFreq(a);
|
||||
const bFreq = getHabitFreq(b);
|
||||
const aFreq = habitFreqMap.get(a.id) || 'daily';
|
||||
const bFreq = habitFreqMap.get(b.id) || 'daily';
|
||||
const freqOrder = ['daily', 'weekly', 'monthly', 'yearly'];
|
||||
if (freqOrder.indexOf(aFreq) !== freqOrder.indexOf(bFreq)) {
|
||||
return freqOrder.indexOf(aFreq) - freqOrder.indexOf(bFreq);
|
||||
@@ -213,7 +213,7 @@ const ItemSection = ({
|
||||
</ContextMenuItem>
|
||||
{habit.isTask && (
|
||||
<ContextMenuItem onClick={() => {
|
||||
saveHabit({...habit, frequency: d2t({ dateTime: getNow({ timezone: settings.system.timezone })})})
|
||||
saveHabit({ ...habit, frequency: d2t({ dateTime: getNow({ timezone: settings.system.timezone }) }) })
|
||||
}}>
|
||||
<Calendar className="mr-2 h-4 w-4" />
|
||||
<span>Move to Today</span>
|
||||
@@ -243,9 +243,9 @@ const ItemSection = ({
|
||||
{completionsToday}/{target}
|
||||
</span>
|
||||
)}
|
||||
{getHabitFreq(habit) !== 'daily' && (
|
||||
{habitFreqMap.get(habit.id) !== 'daily' && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{getHabitFreq(habit)}
|
||||
{habitFreqMap.get(habit.id)}
|
||||
</Badge>
|
||||
)}
|
||||
<span className="flex items-center">
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
import { Habit } from '@/lib/types'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { d2s, getNow, t2d, getCompletedHabitsForDate } from '@/lib/utils'
|
||||
import { d2s, getNow, t2d } from '@/lib/utils' // Removed getCompletedHabitsForDate
|
||||
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts'
|
||||
import { useAtom } from 'jotai'
|
||||
import { settingsAtom, hasTasksAtom } from '@/lib/atoms'
|
||||
import { settingsAtom, hasTasksAtom, completedHabitsMapAtom } from '@/lib/atoms' // Added completedHabitsMapAtom
|
||||
|
||||
interface HabitStreakProps {
|
||||
habits: Habit[]
|
||||
@@ -14,6 +14,8 @@ interface HabitStreakProps {
|
||||
export default function HabitStreak({ habits }: HabitStreakProps) {
|
||||
const [settings] = useAtom(settingsAtom)
|
||||
const [hasTasks] = useAtom(hasTasksAtom)
|
||||
const [completedHabitsMap] = useAtom(completedHabitsMapAtom) // Use the atom
|
||||
|
||||
// Get the last 7 days of data
|
||||
const dates = Array.from({ length: 7 }, (_, i) => {
|
||||
const d = getNow({ timezone: settings.system.timezone });
|
||||
@@ -21,20 +23,17 @@ export default function HabitStreak({ habits }: HabitStreakProps) {
|
||||
}).reverse()
|
||||
|
||||
const completions = dates.map(date => {
|
||||
const completedHabits = getCompletedHabitsForDate({
|
||||
habits: habits.filter(h => !h.isTask),
|
||||
date: t2d({ timestamp: date, timezone: settings.system.timezone }),
|
||||
timezone: settings.system.timezone
|
||||
});
|
||||
const completedTasks = getCompletedHabitsForDate({
|
||||
habits: habits.filter(h => h.isTask),
|
||||
date: t2d({ timestamp: date, timezone: settings.system.timezone }),
|
||||
timezone: settings.system.timezone
|
||||
});
|
||||
// Get completed habits for the date from the map
|
||||
const completedOnDate = completedHabitsMap.get(date) || [];
|
||||
|
||||
// Filter the completed list to count habits and tasks
|
||||
const completedHabitsCount = completedOnDate.filter(h => !h.isTask).length;
|
||||
const completedTasksCount = completedOnDate.filter(h => h.isTask).length;
|
||||
|
||||
return {
|
||||
date,
|
||||
habits: completedHabits.length,
|
||||
tasks: completedTasks.length
|
||||
habits: completedHabitsCount,
|
||||
tasks: completedTasksCount
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user