# sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk architecture

> Architecture diagram of the GitHub repository sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk
- Repository: https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk

## Overview

##### Project type and purpose
- This is a full-stack, AI-powered Reddit clone built on Next.js 15, Sanity CMS, Clerk Auth, and OpenAI.
- It offers community creation, posting, commenting, voting, real-time updates, and AI content moderation.

##### File structure
- **app/**: Next.js 15 App Router routes & layouts (admin vs. user-facing).
- **action/**: Server Actions for CRUD (posts, comments, votes, reports).
- **components/**: UI React components (posts, comments, header, sidebar, UI primitives).
- **sanity/**: Sanity Studio config, schemas, and typed client logic.
- **lib/**: Shared utilities & base URL definitions.
- **hooks/**: Client-only React hooks.
- **middleware.ts**: Auth/session middleware.
- **public/** & **images/**: Static assets.
- **tools/**: CLI and prompt builders for admin tools.
- **config files**: next.config.ts, tsconfig.json, postcss.config.mjs, eslint.config.mjs.

##### Additional insights from README
- Highlights Next.js Server Components & Actions, Turbopack, Tailwind CSS, Radix UI.
- Authentication delegated to Clerk.
- Content storage & real-time updates via Sanity.
- AI moderation uses OpenAI API during content creation workflows.

##### Main components and their interactions
a. **Frontend (Next.js App Router)**
- Routes under `/app` handle page rendering.
- Server Components fetch data via Sanity client & Clerk.
- Client Components for interactivity (buttons, forms, hooks).
- UI styled with Tailwind + Radix UI primitives.
b. **Server Actions (action/ folder)**
- Entry points for create/update/delete events.
- Validate user (via Clerk session), then call Sanity client libraries.
- Trigger AI moderation by calling OpenAI before persisting.
c. **Sanity CMS**
- Headless CMS holds posts, comments, subreddits, users, votes.
- Studio under `/sanity` for admin tooling.
- Real-time updates via Sanity “live” client.
d. **Clerk Auth Service**
- Manages user signup, signin, sessions.
- Protects server actions via middleware.
e. **OpenAI API**
- Content moderation endpoint.
- Called from server actions before content persistence.
f. **Client-side state & real-time**
- Hooks (use-mobile, live subscriptions).
- Optimistic UI updates on votes/comments.

##### Architectural patterns and design principles
- **App Router / Server Components**: clear separation of server vs. client rendering.
- **Server Actions**: centralize data mutations, enforce auth & moderation.
- **Headless CMS**: decoupled content storage, real-time event streams.
- **Component-driven UI**: reusable, isolated UI pieces in `/components`.

##### Detail
- Sanity schemas vs. Studio vs. client APIs.
- Middleware for auth checks.
- Don’t omit the content moderation step.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    Browser["Browser (User)"]:::external
    subgraph "Presentation Layer"
        direction TB
        NextApp["Next.js App Router"]:::frontend
        subgraph "Admin UI"
            direction TB
            AdminRoutes["app/(admin) routes & layouts"]:::frontend
            AdminStudioPage["Studio Entrypoint"]:::frontend
        end
        subgraph "User UI"
            direction TB
            UserRoutes["app/(app) routes & layouts"]:::frontend
            CommunityPages["Community & Post Pages"]:::frontend
        end
        UIComponents["Reusable UI Components"]:::ui
        Hooks["Client-side Hooks & Subscriptions"]:::ui
        ServerComp["Server Components (Data Fetching)"]:::server
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        Middleware["middleware.ts"]:::server
        ServerActions["Server Actions (CRUD & Moderation)"]:::server
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        SanityClient["Sanity Client & Typed APIs"]:::db
        SanitySchemas["Sanity Schemas"]:::db
        SanityStudio["Sanity Studio (Admin Tooling)"]:::db
        SanityDB["Sanity Datastore"]:::db
    end

    %% External Services & Tools
    subgraph "External Services & Tools"
        direction TB
        Clerk["Clerk Auth Service"]:::external
        OpenAI["OpenAI API"]:::external
        Tools["CLI Tools & Prompt Builders"]:::util
    end

    %% Connections
    Browser --> NextApp
    NextApp --> ServerComp
    NextApp --> UIComponents
    NextApp --> Hooks
    AdminRoutes --> NextApp
    AdminStudioPage --> AdminRoutes
    UserRoutes --> NextApp
    CommunityPages --> UserRoutes

    ServerComp -->|fetch posts/comments| SanityClient
    ServerComp -->|validate session| Clerk
    ServerComp -->|protected by| Middleware
    ServerComp -->|invoke| ServerActions

    ServerActions -->|auth check| Clerk
    ServerActions -->|AI moderation| OpenAI
    ServerActions -->|persist data| SanityClient

    SanityClient -->|queries/mutations| SanityDB
    SanityDB -->|real-time pushes| SanityClient
    SanityDB -->|admin UI| SanityStudio

    Tools --> SanitySchemas

    %% Click Events
    click AdminRoutes "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/app/(admin)/"
    click AdminStudioPage "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/blob/main/app/(admin)/studio/[[...tool]]/page.tsx"
    click UserRoutes "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/app/(app)/"
    click UIComponents "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/components/"
    click Hooks "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/hooks/"
    click Middleware "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/blob/main/middleware.ts"
    click ServerActions "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/action/"
    click SanityClient "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/sanity/lib/"
    click SanitySchemas "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/sanity/schemaTypes/"
    click SanityStudio "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/blob/main/app/(admin)/studio/[[...tool]]/page.tsx"
    click OpenAI "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/blob/main/lib/utils.ts"
    click Tools "https://github.com/sonnysangha/reddit-ai-clone-nextjs-15-ai-agents-sanity-clerk/tree/main/tools/"

    %% Styles
    classDef frontend fill:#cfe2ff,stroke:#084298,color:#084298
    classDef ui fill:#e2f0d9,stroke:#33691e,color:#33691e
    classDef server fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    classDef db fill:#f8d7da,stroke:#842029,color:#842029
    classDef external fill:#fff3cd,stroke:#664d03,color:#664d03
    classDef util fill:#e7e6ff,stroke:#3f2b96,color:#3f2b96
```
