DEV Community

Cover image for How I Built an Offline-First Medical AI SaaS with React & Supabase... healthchain360.com
Aditya Jhajharia
Aditya Jhajharia

Posted on

How I Built an Offline-First Medical AI SaaS with React & Supabase... healthchain360.com

"healthchain360.com"Building a SaaS is hard. Building a healthcare SaaS that handles complex AI diagnosis pipelines, multi-role access (Patients, Doctors, Caregivers), and works entirely offline? That’s a different beast altogether.
Over the past few months, I architected and built HealthChain360β€”a medical investigation engine designed to cut a patient's diagnostic odyssey from years down to weeks.

Instead of just showing off the final product, I want to break down the actual engineering architecture, the brutal edge cases of offline-first development, and why I chose the Supabase + React stack to pull it off.

πŸ›  The Tech Stack

To build a luxury-grade product at startup speed, I needed a stack that stayed out of my way but scaled infinitely:

  • Frontend: React (TypeScript) + Tailwind CSS + Framer Motion
  • Backend/Auth/Database: Supabase (PostgreSQL) + Row Level Security (RLS)
  • AI Engine: Gemini Advanced API
  • Native Bridge: Capacitor (for iOS/Android native storage) Here are the three biggest architectural hurdles I faced and how I solved them. --- ## πŸ›‘ Challenge 1: The "Ghost Cache" Offline Data Problem HealthChain360 is an offline-first application. If a patient is in a hospital basement with zero cell reception, they still need to access their medical timeline and log new symptoms. The Naive Approach: Initially, I just used standard localStorage and fired off a supabase.from().insert() call in the background. But what happens if they delete a case while offline? The local storage deletes it, but the Supabase network request fails silently. When they come back online, the sync engine pulls the "deleted" case back from the cloud. Classic CRDT ghosting. The Architecture Fix: I built a strict Queue & Tombstone system. When a user deletes a case offline, the ID is pushed to an hc_deleted_cases queue in local storage. The absolute first thing the Sync Engine does upon regaining internet access is process this queue, purging the ghosts from Supabase before it downloads any new data. --- ## 🧠 Challenge 2: AI Context Cross-Contamination HealthChain360 has a feature called the Clinical Report Analyzer, where users upload raw lab results and the AI breaks down the biomarkers. The app also supports Profile Switching (e.g., switching from your own profile to your Grandmother's "Caregiver" profile without logging out). The Bug: React state is tricky. If a user switched to their Grandmother's profile and uploaded a lab report, the component was still holding their medical history in memory. The AI was analyzing the Grandmother's bloodwork using the Grandson's medical history. A massive clinical data leak. The Fix: I had to strictly seal the boundaries of state isolation. I injected a global hc_profile_updated event listener directly into the React lifecycle of every single AI tool in the app. The exact millisecond the profile switches, the UI flushes its cache and re-fetches the target profile's data, ensuring the AI only ever receives the sandboxed medical context. --- ## πŸ”’ Challenge 3: Multi-Role Supabase RLS Healthcare data requires absolute privacy. I couldn't just rely on the frontend to filter data. Using Supabase Row Level Security (RLS), I moved the security perimeter directly to the PostgreSQL database. I wrote strict SQL policies ensuring that a user can only ever SELECT, UPDATE, or DELETE a case if the user_id matches their verified JWT auth token. Even if someone intercepted the API requests or bypassed the React frontend entirely, the Postgres database physically refuses to serve data that doesn't belong to them. --- ## πŸš€ The Result HealthChain360 is now a production-ready, highly complex SaaS that feels as fast as a local native app but has the power of a cloud-synced AI engine. I firmly believe that as Product Engineers, we shouldn't just write codeβ€”we should build revenue-generating, polished products. Build like an MNC, ship like a startup, and polish like a luxury brand. Let's Connect! If you are building a complex SaaS and need a Premium Product Engineer to architect your Next.js/React/Supabase application, I'm currently taking on new projects. πŸ”— Check out my portfolio: [Insert your personal website link here] πŸ’Ό Hire me on Upwork: [Insert your Upwork profile link here]

Top comments (0)