# williamzshih/instagram architecture

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

- Interactive diagram: https://gitdiagram.com/williamzshih/instagram
- Repository: https://github.com/williamzshih/instagram

## Overview

##### Main Components

A. Client-Side Layer:
- Next.js Frontend (React Components)
- Route Structure (/src/app/(routes))
- UI Components (/src/components)
- Modal System (@modal parallel route)
- State Management (React Query)
- Theme System (Dark/Light mode)

B. Server-Side Layer:
- Next.js API Routes (/src/app/api)
- Authentication System (Auth.js)
- Database Layer (Prisma + MongoDB)

C. External Services:
- Pinata IPFS (Image Storage)
- MongoDB Atlas
- Google OAuth

##### Component Relationships

**[Client Layer]**
- ↑↓
**[Next.js Server Layer]**
- ↑↓
**[External Services]**

##### Detailed Component Breakdown

Frontend (Blue Components):
- Component hierarchy from /src/components
- Shared UI components from /src/components/ui

API Layer (Green Components):
- Authentication endpoints (/api/auth)
- User management endpoints (/api/user)
- Action handlers (/src/actions)

Database Layer (Yellow Components):
- Prisma schema relationships
- Models: User, Post, Comment, Like, Bookmark, Follow

External Services (Purple Components):
- Pinata IPFS connection
- MongoDB Atlas
- Auth providers

##### Data Flow Visualization

- Client-server communication
- Authentication flow
- Image upload process
- Database operations
- Real-time updates

**[Google OAuth] ←→ [Auth.js] ←→ [Next.js API] ←→ [Database]**

**[Client] → [Next.js API] → [Pinata IPFS] → [MongoDB Storage]**

**[Client Query Cache] ←→ [Server Actions] ←→ [Database]**

##### Important Elements to Highlight

- Server Actions (/src/actions) for data mutations
- Modal system implementation (@modal route group)
- Authentication middleware
- React Query integration for state management
- Prisma ORM layer

##### Additional Notes

Architectural patterns:
- Route Groups pattern (Next.js file-system routing)
- Server Components vs Client Components
- Parallel Routes for modal implementation
- Repository pattern in database access
- Optimistic updates pattern

This system design should reflect the modern Next.js 14 architecture while showing how all the pieces work together to create a robust Instagram clone.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        direction TB
        FE["Frontend Components"]
        UI["UI Components"]:::ui
        click UI "https://github.com/williamzshih/instagram/tree/main/src/components/ui/"
        NAV["Navigation System"]:::nav
        click NAV "https://github.com/williamzshih/instagram/blob/main/src/components/DesktopNav.tsx"
        THEME["Theme System"]:::ui
        click THEME "https://github.com/williamzshih/instagram/blob/main/src/components/ThemeSwitch.tsx"
        QUERY["Query Provider"]:::state
        click QUERY "https://github.com/williamzshih/instagram/blob/main/src/components/QueryProvider.tsx"
        
        subgraph Pages
            HOME["Home Page"]
            PROFILE["Profile Pages"]:::profile
            click PROFILE "https://github.com/williamzshih/instagram/blob/main/src/app/(routes)/user/[username]/page.tsx"
            POST["Post Pages"]:::post
            click POST "https://github.com/williamzshih/instagram/blob/main/src/app/(routes)/post/[id]/page.tsx"
            CREATE["Create Post"]:::post
            click CREATE "https://github.com/williamzshih/instagram/blob/main/src/app/(routes)/create/page.tsx"
            AUTH["Auth Pages"]:::auth
            click AUTH "https://github.com/williamzshih/instagram/blob/main/src/app/(routes)/sign-in/page.tsx"
        end
        
        subgraph Components
            MODAL["Modal System"]:::modal
            click MODAL "https://github.com/williamzshih/instagram/tree/main/src/app/(routes)/@modal"
            AVATAR["User Avatars"]:::ui
            click AVATAR "https://github.com/williamzshih/instagram/blob/main/src/components/UserAvatar.tsx"
            POSTGRID["Posts Grid"]:::post
            click POSTGRID "https://github.com/williamzshih/instagram/blob/main/src/components/PostsGrid.tsx"
            PROFHEAD["Profile Header"]:::profile
            click PROFHEAD "https://github.com/williamzshih/instagram/blob/main/src/components/ProfileHeader.tsx"
        end
    end

    subgraph Server["Server Layer"]
        direction TB
        API["API Routes"]:::api
        click API "https://github.com/williamzshih/instagram/tree/main/src/app/api"
        ACTIONS["Server Actions"]:::actions
        click ACTIONS "https://github.com/williamzshih/instagram/tree/main/src/actions"
        AUTH_SYS["Auth System"]:::auth
        click AUTH_SYS "https://github.com/williamzshih/instagram/blob/main/src/auth.ts"
        MIDDLEWARE["Middleware"]:::middleware
        click MIDDLEWARE "https://github.com/williamzshih/instagram/blob/main/src/middleware.ts"
        UTILS["Utilities"]:::utils
        click UTILS "https://github.com/williamzshih/instagram/blob/main/src/lib/utils.ts"
    end

    subgraph Data["Data Layer"]
        direction TB
        DB["MongoDB + Prisma"]:::database
        click DB "https://github.com/williamzshih/instagram/blob/main/prisma/schema.prisma"
        MODELS["Data Models"]:::database
    end

    subgraph External["External Services"]
        direction TB
        IPFS["Pinata IPFS"]:::external
        MONGO_ATLAS["MongoDB Atlas"]:::external
        OAUTH["Google OAuth"]:::external
    end

    %% Connections
    FE <--> MODAL
    FE <--> NAV
    FE <--> QUERY
    QUERY <--> ACTIONS
    API <--> AUTH_SYS
    AUTH_SYS <--> OAUTH
    ACTIONS <--> DB
    API <--> DB
    DB <--> MONGO_ATLAS
    ACTIONS <--> IPFS

    %% Styles
    classDef ui fill:#e1f5fe,stroke:#01579b
    classDef nav fill:#e8f5e9,stroke:#1b5e20
    classDef state fill:#fff3e0,stroke:#e65100
    classDef profile fill:#f3e5f5,stroke:#4a148c
    classDef post fill:#e8eaf6,stroke:#1a237e
    classDef auth fill:#fce4ec,stroke:#880e4f
    classDef modal fill:#f3e5f5,stroke:#4a148c
    classDef api fill:#e8f5e9,stroke:#1b5e20
    classDef actions fill:#e0f2f1,stroke:#004d40
    classDef middleware fill:#fff3e0,stroke:#e65100
    classDef utils fill:#f5f5f5,stroke:#212121
    classDef database fill:#fff8e1,stroke:#ff6f00
    classDef external fill:#e1bee7,stroke:#4a148c
```
