Update for Sally

Added Freeze Day
This commit is contained in:
2025-10-15 13:50:20 +02:00
parent 6dbb690e3d
commit f830e4fccf
4 changed files with 106 additions and 43 deletions

View File

@@ -65,12 +65,15 @@ export const toggleCompletion = (habitId, dateStr) => {
});
};
import { getFrozenDays } from './utils-habit.js';
const calculateStreaks = (completions) => {
if (completions.length === 0) {
return { currentStreak: 0, longestStreak: 0 };
}
const sortedDates = completions
// Only use frozen days for streak calculation
const frozenDays = getFrozenDays(completions);
const allValid = Array.from(new Set([...completions, ...frozenDays]));
const sortedDates = allValid
.map(d => new Date(d))
.sort((a, b) => b - a);
@@ -88,15 +91,12 @@ const calculateStreaks = (completions) => {
if (mostRecent.getTime() === today.getTime() || mostRecent.getTime() === yesterday.getTime()) {
currentStreak = 1;
for (let i = 1; i < sortedDates.length; i++) {
const current = new Date(sortedDates[i]);
current.setHours(0, 0, 0, 0);
const previous = new Date(sortedDates[i - 1]);
previous.setHours(0, 0, 0, 0);
const diffDays = Math.floor((previous - current) / (1000 * 60 * 60 * 24));
if (diffDays === 1) {
currentStreak++;
tempStreak++;
@@ -112,9 +112,7 @@ const calculateStreaks = (completions) => {
current.setHours(0, 0, 0, 0);
const previous = new Date(sortedDates[i - 1]);
previous.setHours(0, 0, 0, 0);
const diffDays = Math.floor((previous - current) / (1000 * 60 * 60 * 24));
if (diffDays === 1) {
tempStreak++;
longestStreak = Math.max(longestStreak, tempStreak);
@@ -124,9 +122,8 @@ const calculateStreaks = (completions) => {
}
longestStreak = Math.max(longestStreak, currentStreak, 1);
return { currentStreak, longestStreak };
};
}
export const exportData = () => {
const habits = getHabits();

View File

@@ -39,4 +39,38 @@ export const getColorIntensity = (completions, dateStr) => {
export const getWeekdayLabel = (dayIndex) => {
const labels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
return labels[dayIndex];
};
};
// Returns array of frozen days (date strings) for a given completions array
export function getFrozenDays(completions) {
// Map: month string -> frozen day string
const frozenDays = [];
const completedSet = new Set(completions);
// Sort completions for easier lookup
const sorted = [...completions].sort();
// Track frozen per month
const frozenPerMonth = {};
// To find missed days, scan a range of dates
if (completions.length === 0) return [];
const minDate = new Date(sorted[0]);
const maxDate = new Date(sorted[sorted.length - 1]);
for (let d = new Date(minDate); d <= maxDate; d.setDate(d.getDate() + 1)) {
const dateStr = formatDate(d);
if (completedSet.has(dateStr)) continue; // skip completed days
// Check neighbors
const prevDate = new Date(d); prevDate.setDate(prevDate.getDate() - 1);
const nextDate = new Date(d); nextDate.setDate(nextDate.getDate() + 1);
const prevDateStr = formatDate(prevDate);
const nextDateStr = formatDate(nextDate);
// Only freeze if both neighbors are completed
const monthKey = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`;
if (
completedSet.has(prevDateStr) &&
completedSet.has(nextDateStr) &&
!frozenPerMonth[monthKey]
) {
frozenDays.push(dateStr);
frozenPerMonth[monthKey] = true;
}
}
return frozenDays;
}