# focush67/mirrit architecture

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

- Interactive diagram: https://gitdiagram.com/focush67/mirrit
- Repository: https://github.com/focush67/mirrit

## Overview

##### Main Components
- Client / Frontend:
- NextJS App: This is represented by the “app” directory where NextJS pages and layouts are defined. Break down into subcomponents like authentication (sign-in, sign-up), dashboard, routes for posts, profile, chat (both individual and group), notifications, etc.
- UI Components: Represent the reusable UI elements (found in “components”). These include buttons, modals, chat components, navigation, story components, etc.
- State Management: Show the Redux store (in “redux_store”) plus context providers (in “context” and “providers”) that manage UI state, notifications, and toast messages.

- Backend / Server Components:
- API Endpoints: Outline the “app/api” routes. These include endpoints for posts, user actions, file uploads via UploadThing, and webhooks (especially for Clerk).
- Server Actions: Include the “server_actions” module which centralizes business logic for chat, follows, groups, posts, stories, etc.
- Services Layer: Indicate the “services” directory where authentication, posts, chat, follow, notifications, and other business logic is encapsulated.
- Utility Functions: Represent “utilities” which include database operations, date formats, Pusher setup, and UploadThing helpers.

- Database & ORM:
- MongoDB: As mentioned in the README, this is your main data store.
- Prisma: The “prisma/schema.prisma” file ensures you have an ORM to interact with MongoDB.

- External Integrations:
- Clerk: For authentication and user management.
- UploadThing: For file upload handling.
- Pusher: For realtime notifications (integrated via utilities and external configuration).

##### Relationships & Interactions
- Frontend interactions:
- The NextJS client issues API calls (from “app/api”) to trigger server actions.
- UI components are organized via pages and layouts, which compose the overall user experience.
- Redux and context providers (from “redux_store” and “providers”) manage state and share data across components.

- API & Server:
- The API endpoints receive HTTP (or server action) calls from the frontend, call into the relevant services that encapsulate business logic, and then interact with the database via Prisma.
- Server Actions are used for complex operations like realtime chat, group management, and notifications.

- Database and External Services:
- Services interact with MongoDB (through Prisma) for persisting user data and social interactions.
- Realtime features are handled by sending messages through Pusher.
- Authentication workflows are routed via Clerk and validated via the “/api/webhooks/clerk” endpoint.
- File uploads are managed by UploadThing (with a dedicated core and route).

##### Architectural Patterns & Design Principles
- Separation of Concerns:
- The project separates UI presentation (pages and components) from business logic (services and server actions) and data persistence (Prisma & MongoDB).
- Modular Structure:
- Each domain (chat, posts, profile, groups, notifications) has its own set of components, API routes, and services.
- Clear Data Flow:
- Data is managed by Redux/context, transferred through API endpoints to the server, which then updates the database and triggers notifications.
- External integrations act as specialized modules (authentication, realtime updates, uploads).

##### Elements
- Use clearly labeled nodes for:
- Frontend (with sub-nodes for Authentication, Dashboard, Posts, Chat, Notifications).
- API Layer (listing endpoints for posts, user, save, uploadthing, and webhooks).
- Server Actions and Services (showing business logic such as chat, follow, posts, groups).
- Database (MongoDB and Prisma ORM).
- External Services (Clerk, UploadThing, Pusher).
- From the Client to API endpoints.
- From API endpoints to server actions and services.
- From services to the Database.
- From services to external services (for authentication, realtime updates, and file handling).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend"
        F1["NextJS Client (Pages & Layouts)"]
        F2["Authentication"]
        F3["UI Components"]
        F4["Redux Store"]
        F5["Context Providers - Context"]
        F6["Context Providers - Providers"]
    end

    %% API Layer
    subgraph "API Layer"
        B1["Posts API"]
        B2["User API"]
        B3["UploadThing API"]
        B4["Clerk Webhook"]
    end

    %% Business Logic
    subgraph "Business Logic"
        BL1["Server Actions"]
        BL2["Services Layer"]
        U1["Database Utility"]
        UT_Core["UploadThing Core"]
        UT_Util["UploadThing Utility"]
        U3["Pusher Utility"]
        U4["Pusher Key"]
    end

    %% Database & ORM
    subgraph "Database & ORM"
        D1["MongoDB"]
        D2["Prisma ORM"]
    end

    %% External Services
    subgraph "External Services"
        E1["Clerk"]
        E2["UploadThing Integration"]
        E3["Pusher"]
    end

    %% Frontend internal connections
    F1 --- F4
    F1 --- F5
    F1 --- F6

    %% Frontend -> API Layer
    F1 -->|"API_Call"| B1
    F1 -->|"API_Call"| B2
    F1 -->|"API_Call"| B3
    F2 -->|"API_Call"| B4
    F2 -->|"Auth_Request"| E1

    %% API Layer -> Business Logic
    B1 -->|"calls"| BL1
    B2 -->|"calls"| BL1
    B4 -->|"calls"| BL1
    B3 -->|"calls"| BL2

    %% Business Logic internal flow
    BL1 -->|"triggers"| BL2

    %% Business Logic -> Database & ORM
    BL2 -->|"db_operation"| U1
    U1 -->|"prisma_query"| D2
    U1 -->|"db_query"| D1

    %% Business Logic -> File Upload Operations
    BL2 -->|"upload_ops"| UT_Core
    BL2 -->|"upload_ops"| UT_Util
    UT_Core -->|"integrates"| E2
    UT_Util -->|"integrates"| E2

    %% Business Logic -> Realtime Notifications
    BL2 -->|"realtime_ops"| U3
    BL2 -->|"realtime_ops"| U4
    U3 -->|"notify"| E3
    U4 -->|"notify"| E3

    %% Click Events
    click F1 "https://github.com/focush67/mirrit/tree/version-2/app"
    click F2 "https://github.com/focush67/mirrit/tree/version-2/app/(authentication)"
    click F3 "https://github.com/focush67/mirrit/tree/version-2/components"
    click F4 "https://github.com/focush67/mirrit/tree/version-2/redux_store"
    click F5 "https://github.com/focush67/mirrit/tree/version-2/context"
    click F6 "https://github.com/focush67/mirrit/tree/version-2/providers"

    click B1 "https://github.com/focush67/mirrit/blob/version-2/app/api/posts/route.ts"
    click B2 "https://github.com/focush67/mirrit/blob/version-2/app/api/user/route.ts"
    click B3 "https://github.com/focush67/mirrit/blob/version-2/app/api/uploadthing/route.ts"
    click B4 "https://github.com/focush67/mirrit/blob/version-2/app/api/webhooks/clerk/route.ts"

    click BL1 "https://github.com/focush67/mirrit/tree/version-2/server_actions"
    click BL2 "https://github.com/focush67/mirrit/tree/version-2/services"
    click U1 "https://github.com/focush67/mirrit/blob/version-2/utilities/database.ts"
    click UT_Core "https://github.com/focush67/mirrit/blob/version-2/app/api/uploadthing/core.ts"
    click UT_Util "https://github.com/focush67/mirrit/blob/version-2/utilities/uploadthing.ts"
    click U3 "https://github.com/focush67/mirrit/blob/version-2/utilities/pusher.ts"
    click U4 "https://github.com/focush67/mirrit/blob/version-2/utilities/pusher-key.ts"

    click D2 "https://github.com/focush67/mirrit/blob/version-2/prisma/schema.prisma"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#005f99,stroke-width:2px,color:#005f99;
    classDef backend fill:#d4edda,stroke:#28a745,stroke-width:2px,color:#155724;
    classDef logic fill:#fff3cd,stroke:#ff8c00,stroke-width:2px,color:#856404;
    classDef database fill:#f8d7da,stroke:#c82333,stroke-width:2px,color:#721c24;
    classDef external fill:#d6d8db,stroke:#6c757d,stroke-width:2px,color:#343a40;
    
    class F1,F2,F3,F4,F5,F6 frontend;
    class B1,B2,B3,B4 backend;
    class BL1,BL2,U1,UT_Core,UT_Util,U3,U4 logic;
    class D1,D2 database;
    class E1,E2,E3 external;
```
