# nutri-vision/frontend architecture

> Architecture diagram of the GitHub repository nutri-vision/frontend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nutri-vision/frontend
- Repository: https://github.com/nutri-vision/frontend

## Overview

##### Project type and purpose
- From the file tree and README, this is a full-stack, medical-grade, PWA nutrition dashboard built on Next.js 14.
- Its purpose is to let users securely log food (text, image, voice, barcode), analyze nutrition against medical conditions, and view AI-powered health insights via a responsive, HIPAA-compliant interface.

##### File structure
Top-level breakdown:
- app/ – Next.js App Router pages and layouts (Server & Client Components)
- components/ – Reusable React UI pieces (including custom health flows and main dashboard widgets)
- components/ui/ – Shadcn/ui–based atomic UI primitives
- hooks/ – Custom React hooks (use-mobile, use-toast)
- lib/ – Utility functions (e.g., encryption, date formatting)
- public/ – Static assets and PWA manifest
- styles/ – Global CSS (Tailwind) and configuration
- config files – next.config.mjs, tailwind.config.ts, tsconfig.json, postcss.config.mjs
- package.json and locks – Defines dependencies: React, Next.js, Tailwind, Recharts, React Hook Form, crypto-js, speech, Radix UI, etc.

##### README for additional insights
- Frontend stack: Next.js 14 (App Router & Server Components), TypeScript, Tailwind, Shadcn/ui, Recharts, React Hook Form.
- Security: NextAuth for auth, AES-256, end-to-end encryption, zero-knowledge.
- External integrations: OpenAI, Vision API, Speech API, Nutrition API, Drug Interaction API, FHIR server, HL7.
- Data stores: Relational DB (DATABASE_URL), Redis cache, third-party healthcare services.
- PWA: Offline, service worker, push notifications.

a. Main components
- Client (Browser or Mobile)
– PWA shell (Service Worker, IndexedDB cache)
– Next.js React UI (app router, layouts, pages)
– UI Components (dashboard, onboarding, logging flows)
- Next.js Server (Vercel Node Server or Self-hosted)
– Server Components & API routes (NextAuth, server-side data fetching)
– Authentication & Authorization layer (NextAuth + encryption)
– Business Logic layer (Onboarding, Logging, Dashboard, Profile)
- Data Layer
– Primary Database (e.g., Postgres)
– Redis cache for session & alerts
– File store for uploaded images
- External Services
– AI Services: OpenAI (NLP), Vision API (image recognition), Speech API (voice logging)
– Medical APIs: Nutrition API, Drug Interaction API
– Healthcare Integration: FHIR Server, HL7 endpoints
- Monitoring & Support
– Sentry for errors, Web Vitals for performance, security scanners

b. Relationships & interactions
- Browser ←→ Next.js Server: SSR/SSG data requests, hydration, PWA sync
- Next.js Server → Database/Redis: CRUD for user profiles, logs, sessions
- Next.js Server → External AI & Medical APIs: meal recognition, interaction checks, recommendations
- Next.js Server → FHIR / HL7: provider data exchange
- Service Worker ↔ IndexedDB: offline reads/writes and background sync

c. Architectural patterns & principles
- Separation of concerns: UI layer vs. server/business logic vs. data store
- App Router with Server & Client Components for optimized data fetching
- PWA offline-first pattern with service workers
- Zero-knowledge encryption boundary around sensitive data
- Micro-layering: Authentication service, Logging service, Analytics service, Profile service

d. Key technologies & frameworks
- Next.js 14 App Router
- TypeScript, Tailwind CSS, Shadcn/ui, Recharts, React Hook Form
- NextAuth for security
- Redis for caching
- OpenAI, Vision, Speech SDKs
- FHIR & HL7 for healthcare

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend
    subgraph "Frontend Layer"
        direction TB
        User["User (Browser/Mobile)"]:::frontend
        PWA["PWA Shell"]:::frontend
        SW["Service Worker"]:::frontend
        IDB["IndexedDB Cache"]:::frontend
        NextUI["Next.js React UI"]:::frontend
        UIComp["UI Components"]:::frontend
    end

    %% Backend
    subgraph "Backend Layer"
        direction TB
        API["Next.js Server API"]:::backend
        Auth["Authentication & Authorization"]:::backend
        BL["Business Logic"]:::backend
        Utils["Utility Library (lib/utils.ts)"]:::backend
    end

    %% Data Stores
    subgraph "Data Layer"
        direction TB
        DB["Primary Database (Postgres)"]:::data
        Cache["Redis Cache"]:::data
        FS["File Store (Images)"]:::data
    end

    %% External Services
    subgraph "External Integrations"
        direction TB
        OpenAI["OpenAI API"]:::external
        Vision["Vision API"]:::external
        Speech["Speech API"]:::external
        NutritionAPI["Nutrition API"]:::external
        DrugAPI["Drug Interaction API"]:::external
        FHIR["FHIR Server"]:::external
        HL7["HL7 Endpoint"]:::external
    end

    %% Monitoring
    subgraph "Monitoring & Support"
        direction TB
        Sentry["Sentry"]:::monitoring
        WebVitals["Web Vitals"]:::monitoring
    end

    %% Flows
    User --> PWA
    PWA --> SW
    SW <--> IDB
    User --> NextUI
    NextUI --> API
    API --> Auth
    Auth --> Cache
    API --> BL
    BL --> Utils
    API --> DB
    API --> Cache
    API --> FS
    API --> OpenAI
    API --> Vision
    API --> Speech
    API --> NutritionAPI
    API --> DrugAPI
    API --> FHIR
    API --> HL7
    NextUI --> WebVitals
    API --> Sentry

    %% Click Events - Frontend components mapping
    click NextUI "https://github.com/nutri-vision/frontend/tree/main/app/"
    click PWA "https://github.com/nutri-vision/frontend/blob/main/public/manifest.json"
    click NextUI "https://github.com/nutri-vision/frontend/blob/main/styles/globals.css"
    click NextUI "https://github.com/nutri-vision/frontend/blob/main/app/globals.css"
    click NextUI "https://github.com/nutri-vision/frontend/blob/main/tailwind.config.ts"
    click Auth "https://github.com/nutri-vision/frontend/blob/main/app/auth/page.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/auth-flow.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/auth-provider.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/app/onboarding/page.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/health-onboarding.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/welcome-screen.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/food-input-tabs.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/app/dashboard/page.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/dashboard-layout.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/main-dashboard.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/nutrition-chart.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/health-insights.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/app/profile/page.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/health-profile.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/app/settings/page.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/user-settings.tsx"
    click UIComp "https://github.com/nutri-vision/frontend/tree/main/components/ui/"
    click Utils "https://github.com/nutri-vision/frontend/blob/main/lib/utils.ts"
    click UIComp "https://github.com/nutri-vision/frontend/blob/main/components/theme-provider.tsx"
    click IDE "https://github.com/nutri-vision/frontend/blob/main/hooks/use-mobile.tsx"
    click IDE "https://github.com/nutri-vision/frontend/blob/main/hooks/use-toast.tsx"

    %% Styles
    classDef frontend fill:#CFE2FF,stroke:#084298
    classDef backend fill:#D1E7DD,stroke:#0F5132
    classDef data fill:#FFF3CD,stroke:#664d03
    classDef external fill:#E2D7FF,stroke:#5E00B8
    classDef monitoring fill:#F0F0F0,stroke:#888888
```
