{habits.map((habit) => {
// Use computed() to derive values from closed-over cells
const isCompletedToday = computed(() =>
logs.get().some(
(log) =>
log.habitName === habit.name &&
log.date === todayDate &&
log.completed,
)
);
const streak = computed(() => {
const logList = logs.get();
let count = 0;
for (let i = 0; i < 365; i++) {
const dateToCheck = getDateDaysAgo(i);
const completed = logList.some(
(log) =>
log.habitName === habit.name &&
log.date === dateToCheck &&
log.completed,
);
if (completed) {
count++;
} else if (i === 0) {
continue; // Today not completed is ok
} else {
break; // Gap found
}
}
return count;
});
return (
{habit.icon}
{habit.name || "(unnamed)"}
Streak: {streak} days
toggleHabit.send({ habitName: habit.name })}
>
{isCompletedToday ? "✓" : "○"}
deleteHabit.send({ habit })}
>
×
{/* Last 7 days indicator */}
{[6, 5, 4, 3, 2, 1, 0].map((daysAgo) => {
const date = getDateDaysAgo(daysAgo);
const dayCompleted = computed(() =>
logs.get().some(
(log) =>
log.habitName === habit.name &&
log.date === date &&
log.completed,
)
);
return (
{date.slice(-2)}
);
})}
);
})}
{hasNoHabits
? (
No habits yet. Add one below!
)
: null}