(userId: string)
| 34 | |
| 35 | // Download remote progress and replace local state with it |
| 36 | export async function downloadAndMerge(userId: string): Promise<boolean> { |
| 37 | const { data, error } = await supabase |
| 38 | .from("user_progress") |
| 39 | .select("*") |
| 40 | .eq("user_id", userId) |
| 41 | .single(); |
| 42 | |
| 43 | if (error || !data) { |
| 44 | // No remote data yet — push local up |
| 45 | await uploadProgress(userId); |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | // Remote is the source of truth — overwrite local state |
| 50 | saveCompleted(new Set<number>(data.completed ?? [])); |
| 51 | saveStarred(new Set<number>(data.starred ?? [])); |
| 52 | saveNotes((data.notes as Record<number, string>) ?? {}); |
| 53 | saveSolvedDates((data.solved_dates as Record<number, string>) ?? {}); |
| 54 | saveReminders((data.reminders as Record<number, Reminder>) ?? {}); |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | // Track when we last uploaded to ignore our own realtime events |
| 60 | let lastUploadAt = 0; |
no test coverage detected