# javadshoja/reddit-clone architecture

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

- Interactive diagram: https://gitdiagram.com/javadshoja/reddit-clone
- Repository: https://github.com/javadshoja/reddit-clone

## Overview

##### Main System Components

a) Frontend Layer:
- Next.js App Router (src/app directory)
- Client Components (src/components)
- Server Components
- Authentication Modal System (@authModal)

b) API Layer:
- REST API endpoints (src/app/api)
- Authentication endpoints
- CRUD operations for posts, comments, and subreddits
- File upload handling

c) Database Layer:
- PostgreSQL (Neon)
- Redis Cache (Upstash)
- Drizzle ORM Schema

d) External Services:
- Uploadthing for file storage
- Auth.js for authentication
- Neon for PostgreSQL
- Upstash for Redis

##### Component Relationships

a) Client-Server Flow:
**User Interface → API Routes → Database/Cache**

b) Authentication Flow:
**Auth Modal → Auth.js → Database**

c) Data Flow:
**Client Components → API Routes → Drizzle ORM → PostgreSQL/Redis**

##### Specific Implementation Details

a) Feature Modules:
- Subreddit management
- Post handling
- Comment system
- Voting system
- User management

b) Database Schema (based on src/db/schema):
- Users
- Posts
- Comments
- Subreddits
- Votes
- Subscriptions

##### Key Architectural Patterns to Highlight

- Next.js App Router architecture
- Server and Client Components separation
- Parallel routes for modal handling
- API route organization
- Database schema relationships
- Caching strategy with Redis

##### Implementation Notes

a) Frontend Architecture:
- Modal routing system using @authModal
- Feed system (Custom/General)
- Editor implementation
- Component hierarchy

b) Backend Architecture:
- API route structure
- Database schema relationships
- Authentication flow
- File upload handling

c) Performance Considerations:
- Redis caching layer
- Server vs. Client components
- API optimization

**┌─ User Interface Layer ─┐  ┌─ External Services ─┐**
- Pages               │  │ - Auth.js           │
- Components          │  │ - Uploadthing       │
- Client/Server Split │  │ - Neon              │
- ┘  └────────────────────┘

**┌─ API Layer ──────────┐  ┌─ Data Layer ────────┐**
- REST Endpoints     │  │ - PostgreSQL        │
- Auth Handlers      │  │ - Redis Cache       │
- Upload Handlers    │  │ - Drizzle ORM       │
- ┘  └────────────────────┘

Remember to:
- Use consistent styling
- Notes for important architectural decisions
- Data flow directions clearly
- Caching mechanisms
- Document authentication flows

This structure will provide a clear understanding of the system's architecture while maintaining the necessary level of detail for a principal software engineer to understand the complete system design.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Frontend
        Router["Next.js App Router"]
        AuthModal["Auth Modal System"]
        Components["Client Components"]
        Editor["Editor System"]
        Feed["Feed System"]
        Nav["Navigation"]
        PostUI["Post UI"]
        CommentUI["Comment UI"]
        VoteUI["Vote UI"]
    end

    subgraph API
        APIRoutes["REST API Endpoints"]
        AuthAPI["Auth Endpoints"]
        PostAPI["Post Endpoints"]
        CommentAPI["Comment Endpoints"]
        VoteAPI["Vote Endpoints"]
        SubredditAPI["Subreddit Endpoints"]
        UploadAPI["Upload Endpoints"]
    end

    subgraph Database
        PostgreSQL[(PostgreSQL)]
        Redis[(Redis Cache)]
        Schema["Drizzle ORM Schema"]
        Users[("Users")]
        Posts[("Posts")]
        Comments[("Comments")]
        Votes[("Votes")]
        Subreddits[("Subreddits")]
    end

    subgraph External
        AuthJS["Auth.js"]
        Uploadthing["Uploadthing"]
        Neon["Neon DB"]
        Upstash["Upstash Redis"]
    end

    %% Frontend Relationships
    Router --> Components
    Router --> AuthModal
    Components --> Editor
    Components --> Feed
    Components --> Nav
    Components --> PostUI
    Components --> CommentUI
    Components --> VoteUI

    %% API Relationships
    Components --> APIRoutes
    AuthModal --> AuthAPI
    PostUI --> PostAPI
    CommentUI --> CommentAPI
    VoteUI --> VoteAPI
    Editor --> UploadAPI

    %% Database Relationships
    APIRoutes --> Schema
    Schema --> PostgreSQL
    APIRoutes --> Redis
    Schema --> Users
    Schema --> Posts
    Schema --> Comments
    Schema --> Votes
    Schema --> Subreddits

    %% External Service Relationships
    AuthAPI --> AuthJS
    UploadAPI --> Uploadthing
    PostgreSQL --> Neon
    Redis --> Upstash

    %% Styling
    classDef frontend fill:#a8d5ff
    classDef api fill:#98ff98
    classDef database fill:#ffeb99
    classDef external fill:#dda0dd

    class Router,AuthModal,Components,Editor,Feed,Nav,PostUI,CommentUI,VoteUI frontend
    class APIRoutes,AuthAPI,PostAPI,CommentAPI,VoteAPI,SubredditAPI,UploadAPI api
    class PostgreSQL,Redis,Schema,Users,Posts,Comments,Votes,Subreddits database
    class AuthJS,Uploadthing,Neon,Upstash external

    %% Click Events
    click Router "https://github.com/javadshoja/reddit-clone/tree/main/src/app"
    click AuthModal "https://github.com/javadshoja/reddit-clone/tree/main/src/app/@authModal"
    click APIRoutes "https://github.com/javadshoja/reddit-clone/tree/main/src/app/api"
    click Schema "https://github.com/javadshoja/reddit-clone/tree/main/src/db/schema"
    click Redis "https://github.com/javadshoja/reddit-clone/blob/main/src/lib/redis.ts"
    click AuthJS "https://github.com/javadshoja/reddit-clone/blob/main/src/lib/auth.ts"
    click Uploadthing "https://github.com/javadshoja/reddit-clone/blob/main/src/lib/uploadthing.ts"
    click PostUI "https://github.com/javadshoja/reddit-clone/tree/main/src/components/post"
    click PostAPI "https://github.com/javadshoja/reddit-clone/tree/main/src/app/api/posts"
    click CommentUI "https://github.com/javadshoja/reddit-clone/tree/main/src/components/comment"
    click CommentAPI "https://github.com/javadshoja/reddit-clone/tree/main/src/app/api/subreddit/post/comment"
    click VoteUI "https://github.com/javadshoja/reddit-clone/tree/main/src/components/post-vote"
    click VoteAPI "https://github.com/javadshoja/reddit-clone/tree/main/src/app/api/subreddit/post/vote"
    click SubredditAPI "https://github.com/javadshoja/reddit-clone/tree/main/src/app/r"
    click AuthAPI "https://github.com/javadshoja/reddit-clone/tree/main/src/app/api/auth"
    click Feed "https://github.com/javadshoja/reddit-clone/blob/main/src/components/feed/CustomFeed.tsx"
    click Editor "https://github.com/javadshoja/reddit-clone/blob/main/src/components/editor/Editor.tsx"
    click Nav "https://github.com/javadshoja/reddit-clone/tree/main/src/components/navbar"
```
