fix: current streak should not reset to zero before today's check-in

currentStreak() was starting its backward walk from today, so if the
user hadn't checked in yet today the first lookup missed and it
returned 0 — even with a perfect streak on previous days.

Now when today's entry is absent and the goal is still active, the
count starts from yesterday instead. Once the goal window has closed
the original end-of-window anchor is preserved.
This commit is contained in:
Spencer Flagg 2026-04-25 13:29:43 +02:00
parent fade4c3ae5
commit 392bd6416e

View file

@ -56,9 +56,15 @@ export function completedSet(completions, user, goalId) {
*/
export function currentStreak(completedSetForUser, goal, refISO = todayISO()) {
const start = goal.start_date;
const end = clampWindowEnd(goal, refISO);
let end = clampWindowEnd(goal, refISO);
if (refISO < start) return 0;
// If today isn't completed yet and the goal is still active,
// start from yesterday — the day isn't over so don't break the streak.
if (end === refISO && !completedSetForUser.has(end)) {
end = addDays(end, -1);
}
let cur = end;
let n = 0;
while (cur >= start && completedSetForUser.has(cur)) {