From 33a5f26ec178883b2e811ac25829f35bd1d12200 Mon Sep 17 00:00:00 2001 From: rathi Date: Sun, 17 Nov 2024 21:01:09 -0500 Subject: [PATCH] working version 1 --- habits.db | Bin 20480 -> 20480 bytes src/App.tsx | 39 ----------------------- src/components/HabitList.tsx | 60 +++++++++++++++++++++++------------ src/types.ts | 1 - 4 files changed, 39 insertions(+), 61 deletions(-) diff --git a/habits.db b/habits.db index 182af3838250dc17a978ac5a17b80fa79a0d7ebe..9ec18cb7bdff8c160e095f877a22b92ab0571ab2 100644 GIT binary patch delta 387 zcmZozz}T>Wae_2s%tRSy#+Z!>OZa&h1Q;0jU-7@Dp4@pr0}1ggNv8{GJ_KDmC5V|M#3h1Dtsb*DSRP(E_^J! zUwEJJUfC=t(824#r^d{v$Yo?;WTIWae_3X+e8^>Mz@U#OZa)1_}?<{f9HS8e~16hW`CWZg}9PGUOml@>vL?*Kv7;&fYh48uXneeIb ziEI`WxWGGE#LiGvhM7^3&B(yWMAy(z*U*?xnwe3N9nLr6lLE('habits'); const [darkMode, setDarkMode] = useState(() => localStorage.getItem('darkMode') === 'true'); const [currentMonth, setCurrentMonth] = useState(new Date()); - const [streakGoal, setStreakGoal] = useState(7); - const [showCompletedHabits, setShowCompletedHabits] = useState(true); const daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; @@ -135,22 +133,6 @@ export default function HabitTracker() { return habits.filter(habit => habit.completedDates.includes(date)); }; - const getStreakForHabit = (habit: Habit) => { - let streak = 0; - const today = new Date(); - for (let i = 0; i < streakGoal; i++) { - const date = new Date(today); - date.setDate(today.getDate() - i); - const dateString = date.toISOString().split('T')[0]; - if (habit.completedDates.includes(dateString)) { - streak++; - } else { - break; - } - } - return streak; - }; - const handleUpdateStreak = async (id: number, newStreak: number) => { // Prevent negative streaks if (newStreak < 0) return; @@ -294,27 +276,6 @@ export default function HabitTracker() { )} -
- Streak Goal - -
-
- Show Completed Habits - setShowCompletedHabits(e.target.checked)} - className="w-4 h-4 rounded border-gray-300 dark:border-gray-600" - /> -
)} diff --git a/src/components/HabitList.tsx b/src/components/HabitList.tsx index d6a19e2..a03fd64 100644 --- a/src/components/HabitList.tsx +++ b/src/components/HabitList.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Trash2, ChevronUp, ChevronDown } from 'lucide-react'; +import { Trash2 } from 'lucide-react'; import { Habit } from '../types'; interface HabitListProps { @@ -9,9 +9,37 @@ interface HabitListProps { onToggleHabit: (id: number, date: string) => void; onUpdateHabit: (id: number, name: string) => void; onDeleteHabit: (id: number) => void; - onUpdateStreak: (id: number, streak: number) => void; } +const calculateStreak = (completedDates: string[]): number => { + if (completedDates.length === 0) return 0; + + // Sort dates in ascending order + const sortedDates = [...completedDates].sort((a, b) => + new Date(a).getTime() - new Date(b).getTime() + ); + + let currentStreak = 1; + let maxStreak = 1; + + // Go through the dates and count consecutive completions + for (let i = 1; i < sortedDates.length; i++) { + const prevDate = new Date(sortedDates[i - 1]); + const currDate = new Date(sortedDates[i]); + + // If dates are consecutive or same day, increment streak + if (currDate.getTime() - prevDate.getTime() <= 24 * 60 * 60 * 1000) { + currentStreak++; + maxStreak = Math.max(maxStreak, currentStreak); + } else { + // Reset streak counter when there's a gap + currentStreak = 1; + } + } + + return maxStreak; +}; + export function HabitList({ habits, currentWeek, @@ -19,7 +47,6 @@ export function HabitList({ onToggleHabit, onUpdateHabit, onDeleteHabit, - onUpdateStreak }: HabitListProps) { return ( @@ -34,7 +61,12 @@ export function HabitList({ ))} - + @@ -60,23 +92,9 @@ export function HabitList({ ))}
Manual Streak + Best Streak +
+ consecutive completions +
+
Actions
-
- - - {habit.manualStreak || 0} - - -
+ + {calculateStreak(habit.completedDates)} +