-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea0203d
commit dea2b30
Showing
12 changed files
with
231 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client' | ||
|
||
import { useHabits } from "@/hooks/useHabits"; | ||
import { habitsAtom, settingsAtom } from "@/lib/atoms"; | ||
import { Habit } from "@/lib/types"; | ||
import { useAtom } from "jotai"; | ||
import { DateTime } from "luxon"; | ||
|
||
|
||
|
||
type CompletionCache = { | ||
[dateKey: string]: { // dateKey format: "YYYY-MM-DD" | ||
[habitId: string]: number // number of completions on that date | ||
} | ||
} | ||
|
||
|
||
export default function DebugPage() { | ||
const [habits] = useAtom(habitsAtom); | ||
const [settings] = useAtom(settingsAtom); | ||
|
||
function buildCompletionCache(habits: Habit[], timezone: string): CompletionCache { | ||
const cache: CompletionCache = {}; | ||
|
||
habits.forEach(habit => { | ||
habit.completions.forEach(utcTimestamp => { | ||
// Convert UTC timestamp to local date string in specified timezone | ||
const localDate = DateTime | ||
.fromISO(utcTimestamp) | ||
.setZone(timezone) | ||
.toFormat('yyyy-MM-dd'); | ||
|
||
if (!cache[localDate]) { | ||
cache[localDate] = {}; | ||
} | ||
|
||
// Increment completion count for this habit on this date | ||
cache[localDate][habit.id] = (cache[localDate][habit.id] || 0) + 1; | ||
}); | ||
}); | ||
|
||
return cache; | ||
} | ||
|
||
function getCompletedHabitsForDate( | ||
habits: Habit[], | ||
date: DateTime, | ||
timezone: string, | ||
completionCache: CompletionCache | ||
): Habit[] { | ||
const dateKey = date.setZone(timezone).toFormat('yyyy-MM-dd'); | ||
const dateCompletions = completionCache[dateKey] || {}; | ||
|
||
return habits.filter(habit => { | ||
const completionsNeeded = habit.targetCompletions || 1; | ||
const completionsAchieved = dateCompletions[habit.id] || 0; | ||
return completionsAchieved >= completionsNeeded; | ||
}); | ||
} | ||
|
||
const habitCache = buildCompletionCache(habits.habits, settings.system.timezone); | ||
|
||
return ( | ||
<div className="p-4"> | ||
<h1 className="text-xl font-bold mb-4">Debug Page</h1> | ||
<div className="bg-gray-100 p-4 rounded break-all"> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,35 @@ | ||
import { Badge } from '@/components/ui/badge' | ||
import { Habit } from '@/lib/types' | ||
import { isHabitDue, getCompletionsForDate } from '@/lib/utils' | ||
import { Badge } from "@/components/ui/badge" | ||
import { useAtom } from 'jotai' | ||
import { completedHabitsMapAtom, habitsAtom, habitsByDateFamily } from '@/lib/atoms' | ||
import { getTodayInTimezone } from '@/lib/utils' | ||
import { useHabits } from '@/hooks/useHabits' | ||
import { settingsAtom } from '@/lib/atoms' | ||
|
||
interface CompletionCountBadgeProps { | ||
habits: Habit[] | ||
selectedDate: luxon.DateTime | ||
timezone: string | ||
type: 'tasks' | 'habits' | ||
type: 'habits' | 'tasks' | ||
date?: string | ||
} | ||
|
||
export function CompletionCountBadge({ habits, selectedDate, timezone, type }: CompletionCountBadgeProps) { | ||
const filteredHabits = habits.filter(habit => { | ||
const isTask = type === 'tasks' | ||
if ((habit.isTask === isTask) && isHabitDue({ | ||
habit, | ||
timezone, | ||
date: selectedDate | ||
})) { | ||
const completions = getCompletionsForDate({ habit, date: selectedDate, timezone }) | ||
return completions >= (habit.targetCompletions || 1) | ||
} | ||
return false | ||
}).length | ||
export default function CompletionCountBadge({ | ||
type, | ||
date | ||
}: CompletionCountBadgeProps) { | ||
const [settings] = useAtom(settingsAtom) | ||
const [completedHabitsMap] = useAtom(completedHabitsMapAtom) | ||
const targetDate = date || getTodayInTimezone(settings.system.timezone) | ||
const [dueHabits] = useAtom(habitsByDateFamily(targetDate)) | ||
|
||
const totalHabits = habits.filter(habit => | ||
(habit.isTask === (type === 'tasks')) && | ||
isHabitDue({ | ||
habit, | ||
timezone, | ||
date: selectedDate | ||
}) | ||
const completedCount = completedHabitsMap.get(targetDate)?.filter(h => | ||
type === 'tasks' ? h.isTask : !h.isTask | ||
).length || 0 | ||
|
||
const totalCount = dueHabits.filter(h => | ||
type === 'tasks' ? h.isTask : !h.isTask | ||
).length | ||
|
||
return ( | ||
<Badge variant="secondary"> | ||
{`${filteredHabits}/${totalHabits} Completed`} | ||
{`${completedCount}/${totalCount} Completed`} | ||
</Badge> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.