mirror of
https://github.com/nagaoo0/HabbitGrid.git
synced 2026-01-11 23:44:55 +00:00
fix local and remote inconsistency
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, { useMemo, useEffect } from 'react';
|
import React, { useMemo, useEffect } from 'react';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { getColorIntensity, isToday, formatDate, getWeekdayLabel, getFrozenDays } from '../lib/utils-habit';
|
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 HabitGrid = ({ habit, onUpdate, fullView = false }) => {
|
||||||
const frozenDays = getFrozenDays(habit.completions);
|
const frozenDays = getFrozenDays(habit.completions);
|
||||||
@@ -38,9 +38,11 @@ const HabitGrid = ({ habit, onUpdate, fullView = false }) => {
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleCellClick = (date) => {
|
const handleCellClick = async (date) => {
|
||||||
const dateStr = formatDate(date);
|
const dateStr = formatDate(date);
|
||||||
// Optimistic local update
|
// 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 habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
|
||||||
const idx = habits.findIndex(h => h.id === habit.id);
|
const idx = habits.findIndex(h => h.id === habit.id);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
@@ -50,7 +52,8 @@ const HabitGrid = ({ habit, onUpdate, fullView = false }) => {
|
|||||||
habits[idx].completions = completions;
|
habits[idx].completions = completions;
|
||||||
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
|
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
|
||||||
}
|
}
|
||||||
toggleCompletion(habit.id, dateStr); // background sync
|
}
|
||||||
|
await toggleCompletion(habit.id, dateStr);
|
||||||
onUpdate();
|
onUpdate();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ function getFreezeIcon() {
|
|||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { getColorIntensity, isToday, formatDate } from '../lib/utils-habit';
|
import { getColorIntensity, isToday, formatDate } from '../lib/utils-habit';
|
||||||
import { getFrozenDays } 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';
|
import { toast } from './ui/use-toast';
|
||||||
|
|
||||||
const MiniGrid = ({ habit, onUpdate }) => {
|
const MiniGrid = ({ habit, onUpdate }) => {
|
||||||
@@ -69,7 +69,9 @@ const MiniGrid = ({ habit, onUpdate }) => {
|
|||||||
const dateStr = formatDate(date);
|
const dateStr = formatDate(date);
|
||||||
const isTodayCell = isToday(date);
|
const isTodayCell = isToday(date);
|
||||||
const wasCompleted = habit.completions.includes(dateStr);
|
const wasCompleted = habit.completions.includes(dateStr);
|
||||||
// Optimistic local update
|
// 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 habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
|
||||||
const idx = habits.findIndex(h => h.id === habit.id);
|
const idx = habits.findIndex(h => h.id === habit.id);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
@@ -79,7 +81,8 @@ const MiniGrid = ({ habit, onUpdate }) => {
|
|||||||
habits[idx].completions = completions;
|
habits[idx].completions = completions;
|
||||||
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
|
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
|
||||||
}
|
}
|
||||||
toggleCompletion(habit.id, dateStr); // background sync
|
}
|
||||||
|
await toggleCompletion(habit.id, dateStr);
|
||||||
onUpdate();
|
onUpdate();
|
||||||
// Only show encouragement toast if validating (adding) today's dot
|
// Only show encouragement toast if validating (adding) today's dot
|
||||||
if (isTodayCell && !wasCompleted) {
|
if (isTodayCell && !wasCompleted) {
|
||||||
|
|||||||
Reference in New Issue
Block a user