From 76fcc641251e2d7f7c70b2df5ffc07c7db873415 Mon Sep 17 00:00:00 2001 From: count0 Date: Fri, 17 Oct 2025 23:24:05 +0200 Subject: [PATCH] fix local and remote inconsistency --- src/components/HabitGrid.jsx | 27 +++++++++++++++------------ src/components/MiniGrid.jsx | 25 ++++++++++++++----------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/components/HabitGrid.jsx b/src/components/HabitGrid.jsx index db1480c..35b8615 100644 --- a/src/components/HabitGrid.jsx +++ b/src/components/HabitGrid.jsx @@ -1,7 +1,7 @@ import React, { useMemo, useEffect } from 'react'; import { motion } from 'framer-motion'; import { getColorIntensity, isToday, formatDate, getWeekdayLabel, getFrozenDays } from '../lib/utils-habit'; -import { toggleCompletion } from '../lib/datastore'; +import { toggleCompletion, getAuthUser } from '../lib/datastore'; const HabitGrid = ({ habit, onUpdate, fullView = false }) => { const frozenDays = getFrozenDays(habit.completions); @@ -38,19 +38,22 @@ const HabitGrid = ({ habit, onUpdate, fullView = false }) => { } }, []); - const handleCellClick = (date) => { + const handleCellClick = async (date) => { const dateStr = formatDate(date); - // Optimistic local update - const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]'); - const idx = habits.findIndex(h => h.id === habit.id); - if (idx !== -1) { - const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : []; - const cidx = completions.indexOf(dateStr); - if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr); - habits[idx].completions = completions; - localStorage.setItem('habitgrid_data', JSON.stringify(habits)); + // Only do optimistic localStorage write if logged in (remote-first). In local-only mode, let datastore handle it to avoid double-toggle. + const user = await getAuthUser(); + if (user) { + const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]'); + const idx = habits.findIndex(h => h.id === habit.id); + if (idx !== -1) { + const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : []; + const cidx = completions.indexOf(dateStr); + if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr); + habits[idx].completions = completions; + localStorage.setItem('habitgrid_data', JSON.stringify(habits)); + } } - toggleCompletion(habit.id, dateStr); // background sync + await toggleCompletion(habit.id, dateStr); onUpdate(); }; diff --git a/src/components/MiniGrid.jsx b/src/components/MiniGrid.jsx index 2e06923..9dcb653 100644 --- a/src/components/MiniGrid.jsx +++ b/src/components/MiniGrid.jsx @@ -40,7 +40,7 @@ function getFreezeIcon() { import { motion } from 'framer-motion'; import { getColorIntensity, isToday, formatDate } from '../lib/utils-habit'; import { getFrozenDays } from '../lib/utils-habit'; -import { toggleCompletion } from '../lib/datastore'; +import { toggleCompletion, getAuthUser } from '../lib/datastore'; import { toast } from './ui/use-toast'; const MiniGrid = ({ habit, onUpdate }) => { @@ -69,17 +69,20 @@ const MiniGrid = ({ habit, onUpdate }) => { const dateStr = formatDate(date); const isTodayCell = isToday(date); const wasCompleted = habit.completions.includes(dateStr); - // Optimistic local update - const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]'); - const idx = habits.findIndex(h => h.id === habit.id); - if (idx !== -1) { - const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : []; - const cidx = completions.indexOf(dateStr); - if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr); - habits[idx].completions = completions; - localStorage.setItem('habitgrid_data', JSON.stringify(habits)); + // Only optimistic write if logged in; in local-only mode, datastore handles it to avoid double-toggle + const user = await getAuthUser(); + if (user) { + const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]'); + const idx = habits.findIndex(h => h.id === habit.id); + if (idx !== -1) { + const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : []; + const cidx = completions.indexOf(dateStr); + if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr); + habits[idx].completions = completions; + localStorage.setItem('habitgrid_data', JSON.stringify(habits)); + } } - toggleCompletion(habit.id, dateStr); // background sync + await toggleCompletion(habit.id, dateStr); onUpdate(); // Only show encouragement toast if validating (adding) today's dot if (isTodayCell && !wasCompleted) {