Skip to content

Add optional Supabase cloud sync with offline-first architecture#4

Open
hsalaman1 wants to merge 1 commit intomainfrom
claude/plan-observation-data-sync-eqN4e
Open

Add optional Supabase cloud sync with offline-first architecture#4
hsalaman1 wants to merge 1 commit intomainfrom
claude/plan-observation-data-sync-eqN4e

Conversation

@hsalaman1
Copy link
Copy Markdown
Owner

Summary

This PR adds optional cloud synchronization to the ABA data collection app using Supabase, while maintaining full offline-first functionality. All writes go to local IndexedDB first for instant UI responsiveness, then are automatically synced to the cloud when online.

Key Changes

  • Offline-first sync engine (src/services/sync.ts): Implements bidirectional sync with automatic push/pull, debounced flushing, and network-aware scheduling. Dirty rows are tracked locally and synced when the network is available or on manual trigger.

  • Supabase integration (src/services/supabase.ts, src/services/auth.ts): Optional Supabase client initialization from environment variables. If not configured, the app continues to work entirely offline with no UI changes.

  • Database schema updates (src/db/database.ts): Added sync metadata fields (_dirty, _deleted, _syncedAt, ownerId) to all synced tables via Dexie v4 migration. Automatic hooks tag every create/update with _dirty=1 so the sync service knows what to push.

  • Cloud schema (supabase/migrations/001_init.sql): Created 6 tables mirroring the local schema with row-level security policies ensuring each user only sees their own data. JSONB columns store nested arrays to avoid maintaining parallel relational tables.

  • Authentication UI (src/pages/SyncPage.tsx): New page for sign-up/sign-in with sync status display (pending uploads, last sync time, network status, error messages). Manual sync trigger and sign-out button.

  • Sync status subscription (src/hooks/useSyncStatus.ts): React hook for UI components to subscribe to sync state changes without polling.

  • Navigation integration (src/App.tsx): Added Sync nav item with badge showing pending upload count (only visible when Supabase is configured).

  • Soft delete support: Updated pages to use softDelete() instead of hard deletes, and filter out _deleted rows from queries. Tombstones are synced to the cloud then hard-deleted locally.

Notable Implementation Details

  • Last-write-wins conflict resolution: Pull operations skip local rows newer than the remote version.
  • Debounced pending count: UI badge updates efficiently via Dexie hooks with 200ms debounce.
  • Network-aware scheduling: Sync pauses when offline, resumes on reconnect or window focus.
  • Pre-auth data handling: Rows created before sign-in are automatically tagged with the user's ID and marked dirty on first sync.
  • Graceful degradation: App remains fully functional if Supabase is not configured; sync UI is simply hidden.

https://claude.ai/code/session_011BCeQtwoemDz6uQ6HKQhAT

Adds an offline-first sync layer so clients, sessions, behavioral
observations, treatment plans, and the related tables persist to
Supabase in addition to local IndexedDB. Existing local data and
fictitious test data upload automatically on first sign-in.

- Dexie schema bumped to v4 with sync metadata (ownerId, _dirty,
  _deleted, _syncedAt) and indexes; hooks auto-tag every write.
- src/services/sync.ts implements outbox push + pull + tombstones
  with last-write-wins on updatedAt.
- Email/password auth via Supabase, optional: app keeps working
  local-only when env vars are unset.
- New /sync page + bottom nav entry with pending/online indicator.
- Reads filter out tombstones; deletes call softDelete().
- supabase/migrations/001_init.sql defines tables + RLS scoped by
  owner_id; supabase/README.md covers project setup.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
aba-data-collection-app Ready Ready Preview, Comment Apr 25, 2026 4:47pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants