mirror of
https://github.com/nagaoo0/HabbitGrid.git
synced 2026-01-11 23:44:55 +00:00
Bug Fix
This commit is contained in:
@@ -221,30 +221,31 @@ function mergeHabits(localHabits, remoteHabits) {
|
|||||||
return Array.from(map.values());
|
return Array.from(map.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function syncRemoteToLocal() {
|
export async function syncRemoteToLocal() {
|
||||||
const user = await getAuthUser();
|
const user = await getAuthUser();
|
||||||
if (!user) return;
|
if (!user) return;
|
||||||
const remote = await getHabits();
|
const remote = await getHabits();
|
||||||
const localHabits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
|
const localHabits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
|
||||||
|
|
||||||
// Backup local data before any destructive change
|
// Only backup on first login sync (not every refresh)
|
||||||
|
const backupFlag = 'habitgrid_backup_done';
|
||||||
|
if (!localStorage.getItem(backupFlag)) {
|
||||||
backupLocalHabits();
|
backupLocalHabits();
|
||||||
|
localStorage.setItem(backupFlag, '1');
|
||||||
|
}
|
||||||
|
|
||||||
// If both local and remote have data, merge and update both
|
// If both local and remote have data, merge and update both
|
||||||
if (localHabits.length && remote.length) {
|
if (localHabits.length && remote.length) {
|
||||||
const merged = mergeHabits(localHabits, remote);
|
const merged = mergeHabits(localHabits, remote);
|
||||||
localStorage.setItem('habitgrid_data', JSON.stringify(merged));
|
localStorage.setItem('habitgrid_data', JSON.stringify(merged));
|
||||||
// Optionally, upsert merged to remote as well
|
|
||||||
await supabase.from('habits').upsert(merged, { onConflict: 'id' });
|
await supabase.from('habits').upsert(merged, { onConflict: 'id' });
|
||||||
} else if (!remote.length && localHabits.length) {
|
} else if (!remote.length && localHabits.length) {
|
||||||
// Remote empty, local has data: push local to remote
|
|
||||||
await supabase.from('habits').upsert(localHabits, { onConflict: 'id' });
|
await supabase.from('habits').upsert(localHabits, { onConflict: 'id' });
|
||||||
localStorage.setItem('habitgrid_data', JSON.stringify(localHabits));
|
localStorage.setItem('habitgrid_data', JSON.stringify(localHabits));
|
||||||
} else if (remote.length && !localHabits.length) {
|
} else if (remote.length && !localHabits.length) {
|
||||||
// Local empty, remote has data: pull remote to local
|
|
||||||
localStorage.setItem('habitgrid_data', JSON.stringify(remote));
|
localStorage.setItem('habitgrid_data', JSON.stringify(remote));
|
||||||
} // else both empty: do nothing
|
}
|
||||||
|
|
||||||
// Notify UI to reload if listening
|
|
||||||
window.dispatchEvent(new CustomEvent('habitgrid-sync-updated'));
|
window.dispatchEvent(new CustomEvent('habitgrid-sync-updated'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user