Percist local habits to remote db

This commit is contained in:
2025-10-18 13:56:01 +02:00
parent 4d82d4c4b7
commit 9675f42ffc

View File

@@ -208,15 +208,10 @@ export async function syncLocalToRemoteIfNeeded() {
const user = await getAuthUser();
if (!user) return;
const already = localStorage.getItem(SYNC_FLAG);
const { data: remote, error } = await supabase.from('habits').select('id').limit(1);
if (error) return;
if (!already || (remote || []).length === 0) {
// Always upsert all local habits to Supabase after login
let habits = local.getHabits();
if (habits.length === 0) return localStorage.setItem(SYNC_FLAG, new Date().toISOString());
habits = ensureUUIDs(habits);
// Persist back to local so IDs match remote after upsert
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
const rows = habits.map(h => ({
id: h.id,
@@ -233,7 +228,6 @@ export async function syncLocalToRemoteIfNeeded() {
}));
await supabase.from('habits').upsert(rows, { onConflict: 'id' });
localStorage.setItem(SYNC_FLAG, new Date().toISOString());
}
}