# muhammedanaskhan/communet architecture

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

- Interactive diagram: https://gitdiagram.com/muhammedanaskhan/communet
- Repository: https://github.com/muhammedanaskhan/communet

## Overview

##### Project Type & Purpose
- Full-stack social media web application (“Communet”)
- Built on Next.js 13 with Server‐Side Rendering and Server Actions (no separate Node API server)
- Authentication via Clerk, file uploads via UploadThing, persistence in MongoDB (Mongoose)
- Styled with Tailwind CSS and shadcn/ui components

##### Top‐Level Architecture Components
a. Client Browser (React)
b. Next.js Server (hosted on Vercel or similar)
- Route Groups in app/
- (auth) – onboarding, sign-in, sign-up flows
- (root) – activity feed, create/edit/search threads, profiles, thread pages
- Server Actions (lib/actions) invoked directly from components
- API Routes (app/api/)
- uploadthing – file upload handler
- clerk webhook – user/event webhooks via Svix
- Middleware (middleware.ts) – e.g., auth guards, redirects
c. External Services
- Clerk (authentication provider)
- UploadThing (file storage/CDN)
- Svix (secure webhook delivery)
d. Database
- MongoDB Atlas accessed via Mongoose (lib/mongoose.ts)
- Schemas in lib/models (User, Thread, Community)

##### Data Flows & Relationships
- User navigates to a page → Next.js SSR/SSG → calls Server Action (e.g., fetchThreadById) → Mongoose → MongoDB
- Posting threads/comments → Server Action or API route → validate with Zod → Mongoose write → MongoDB
- File upload (profile image or thread image) → Client calls UploadThing route → UploadThing service stores asset → returns URL → stored in MongoDB
- Authentication flows through Clerk SDK (client & server) → requests to Clerk APIs → Clerk issues JWT/session  → middleware verifies user
- Webhooks: Clerk events → Svix forwards to /app/api/webhook/clerk/route.ts → serverless function processes event

##### Architectural Patterns & Design Principles
- Serverless Monolith: Next.js handles UI, SSR, API routes, and Server Actions in a single deployment
- Route‐Group Organization: app/(auth) vs. app/(root) segments for clear separation of pre-auth vs. protected routes
- Component-Driven UI: atoms, cards, forms, shared layout components, and generated shadcn/ui primitives
- Schema-Driven Validation: Zod + react-hook-form for request-level validation
- Modular Data Layer: actions, models, validations under lib/

##### Detail Without Overthinking
- Every distinct component/directory: route groups, middleware, API routes, server actions, models, validation, UI component groups
- Both the UI routing layer and data‐access layer to give a holistic view

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    ClientBrowser["Client Browser"]:::external

    %% Next.js Server
    subgraph "Next.js Server" 
        direction TB
        subgraph "Route Groups"
            direction TB
            subgraph "(auth)"
                direction TB
                AuthLayout["layout.tsx"]:::internal
                Onboarding["onboarding/page.tsx"]:::internal
                SignIn["sign-in/page.tsx"]:::internal
                SignUp["sign-up/page.tsx"]:::internal
            end
            subgraph "(root)"
                direction TB
                RootLayout["layout.tsx"]:::internal
                RootPage["page.tsx"]:::internal
                RootLoading["loading.tsx"]:::internal
                Activity["activity/page.tsx"]:::internal
                CreateThread["create-thread/page.tsx"]:::internal
                Search["search/page.tsx"]:::internal
                Profile["profile/[id]/page.tsx"]:::internal
                ThreadPage["thread/[id]/page.tsx"]:::internal
            end
        end
        subgraph "Server Actions"
            direction TB
            ThreadActions["thread.actions.ts"]:::internal
            UserActions["user.actions.ts"]:::internal
        end
        subgraph "API Routes"
            direction TB
            UploadCore["core.ts"]:::internal
            UploadRoute["route.ts"]:::internal
            ClerkWebhook["clerk/route.ts"]:::internal
        end
        Middleware["middleware.ts"]:::internal
    end

    %% External Services
    Clerk["Clerk (Auth)"]:::external
    UploadThing["UploadThing"]:::external
    Svix["Svix (Webhooks)"]:::external

    %% Database
    MongoDB["MongoDB Atlas"]:::db

    %% Connections
    ClientBrowser -->|"SSR/SSG page request"| RootLayout
    ClientBrowser -->|"SSR/SSG page request"| AuthLayout
    RootLayout -->|"invokes Server Action"| ThreadActions
    ThreadActions -->|"Mongoose"| MongoDB
    UserActions -->|"Mongoose"| MongoDB
    UploadRoute -->|"handles upload"| UploadThing
    ClientBrowser -->|"file upload request"| UploadRoute
    ClientBrowser -->|"auth request"| Clerk
    NextjsServer -->|"verify JWT/session"| Clerk
    Clerk -->|"webhook event"| Svix
    Svix -->|"SVIX-secured webhook"| ClerkWebhook
    Middleware -->|"guards routes"| RootLayout

    %% Click Events
    click AuthLayout "https://github.com/muhammedanaskhan/communet/blob/main/app/(auth)/layout.tsx"
    click Onboarding "https://github.com/muhammedanaskhan/communet/blob/main/app/(auth)/onboarding/page.tsx"
    click SignIn "https://github.com/muhammedanaskhan/communet/blob/main/app/(auth)/sign-in/[[...sign-in]]/page.tsx"
    click SignUp "https://github.com/muhammedanaskhan/communet/blob/main/app/(auth)/sign-up/[[...sign-up]]/page.tsx"
    click RootLayout "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/layout.tsx"
    click RootPage "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/page.tsx"
    click RootLoading "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/loading.tsx"
    click Activity "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/activity/page.tsx"
    click CreateThread "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/create-thread/page.tsx"
    click Search "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/search/page.tsx"
    click Profile "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/profile/[id]/page.tsx"
    click ThreadPage "https://github.com/muhammedanaskhan/communet/blob/main/app/(root)/thread/[id]/page.tsx"
    click ThreadActions "https://github.com/muhammedanaskhan/communet/blob/main/lib/actions/thread.actions.ts"
    click UserActions "https://github.com/muhammedanaskhan/communet/blob/main/lib/actions/user.actions.ts"
    click UploadCore "https://github.com/muhammedanaskhan/communet/blob/main/app/api/uploadthing/core.ts"
    click UploadRoute "https://github.com/muhammedanaskhan/communet/blob/main/app/api/uploadthing/route.ts"
    click ClerkWebhook "https://github.com/muhammedanaskhan/communet/blob/main/app/api/webhook/clerk/route.ts"
    click Middleware "https://github.com/muhammedanaskhan/communet/blob/main/middleware.ts"
    click MongoDB "https://github.com/muhammedanaskhan/communet/blob/main/lib/mongoose.ts"

    %% Styles
    classDef internal fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef external fill:#d4edda,stroke:#333,stroke-width:1px
    classDef db fill:#ffe5b4,stroke:#333,stroke-width:1px

    %% Alias for Next.js Server for connections
    class NextjsServer internal
```
