# kirillpitomets/infinite-chat architecture

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

- Interactive diagram: https://gitdiagram.com/kirillpitomets/infinite-chat
- Repository: https://github.com/kirillpitomets/infinite-chat

## Overview

1. **Top-level shape**
   - This is a **Next.js App Router** application.
   - `src/app` defines the route tree, with a public root page and an authenticated **account area** under `src/app/account`.
   - The account area includes a chat product surface plus sibling sections for **events, meetings, and music**, all wrapped by `src/app/account/layout.tsx`.

2. **Primary product subsystem: chat**
   - The largest subsystem is `src/features/chat`, which contains the chat UI, message composer, inbox, headers, typing banner, and chat room pages.
   - The feature is split by responsibility: `chat`, `message`, `realtime`, `ui`, `pages`, and `hooks`.
   - This is the main end-user workflow: inbox selection → chat room → message list/input → live updates.

3. **UI composition boundary**
   - `src/shared/components` provides reusable building blocks used across the app: sidebar, dialogs, loaders, upload button, search input, typing indicator, preview file UI, notifications, and icon set.
   - The chat feature composes these shared primitives rather than owning all visual elements itself.
   - `src/shared/providers.tsx` and context files suggest the app is wrapped in global providers for user, presence, and notifications.

4. **Server/domain layer**
   - `src/server` is the backend domain layer, organized by business capability:
     - `chat`
     - `message`
     - `user`
     - `presence`
     - `fileStorage`
   - Each domain has DTO mappers and typed controller contracts, which indicates a deliberate separation between persistence models and API payloads.
   - `src/server/index.ts` and `src/server/api/index.ts` likely compose these domain services into the app’s typed server API surface.

5. **API boundary and routing**
   - The repository uses a **catch-all API route** at `src/app/api/[[...slugs]]/route.ts`, which acts as a single HTTP entry point for the typed server API.
   - Dedicated routes also exist for:
     - `src/app/api/realtime/route.ts` for realtime transport
     - `src/app/api/uploadthing/route.ts` and `core.ts` for file upload integration
   - This means client code talks to a small number of Next.js route handlers, while the real domain logic lives in `src/server`.

6. **Persistence layer**
   - Data persistence is handled by **Prisma**:
     - `prisma/schema.prisma`
     - many migration files showing an evolving chat schema
   - The schema history indicates core tables for users, chats, messages, attachments, and chat-user relations.
   - Notable evolution includes:
     - user auth IDs and avatar URLs
     - chat image URLs
     - message soft-delete support
     - attachment modeling and type cleanup
     - read-state defaults on the chat-user join model

7. **Realtime architecture**
   - Realtime is a first-class subsystem:
     - client hooks: `useRealtimeChat`, `useRealtimeInbox`, `useRealtimeTyping`
     - server: `src/server/api/presence/presence.service.ts`
     - client transport helpers: `src/shared/lib/realtime-client.ts`, `src/shared/lib/realtime.ts`
   - The presence layer uses Redis via `src/shared/lib/redis.ts`, which suggests ephemeral state such as online users and typing indicators is not stored in the main relational database.
   - The realtime flow appears to power inbox refreshes, live message updates, typing status, and user presence.

8. **Messaging and attachment flow**
   - Messaging is handled by `src/server/api/message` plus message-specific hooks in `src/features/chat/message/api`.
   - Attachments are modeled separately and transformed through DTO helpers such as `toMessageAttachments.ts`.
   - File upload goes through **UploadThing** and then into the message model, so the data flow is: user selects files → upload service stores them → message payload references attachment metadata → UI renders attachments via shared preview components.

9. **Client state and interaction model**
   - The app relies on a mix of React hooks and context for cross-cutting UI state:
     - `CurrentUserContext`
     - `UserPresenceContext`
     - `NotificationManager`
   - Feature hooks handle common interaction concerns such as typing indicators, debouncing, file previews, auth redirects, and message context menus.
   - This suggests a mostly client-driven chat experience with server mutations and realtime synchronization.

10. **Shared contract and validation layer**
   - `src/shared/schemes` contains schema definitions for chat, chat preview, message, and user data.
   - `src/shared/lib/eden.ts` and the DTO/type files indicate the frontend and backend share typed contracts rather than relying on loose JSON shapes.
   - `src/shared/types` and `src/shared/utils` contain cross-feature utilities used to keep the app consistent across routes and domains.

11. **Request/user boundary**
   - `src/server/middlewares/userContextMiddleware.ts` and `src/proxy.ts` indicate request-level user context and route protection.
   - `src/shared/hooks/useAuthRedirect.ts` suggests the UI redirects unauthenticated users away from protected account routes.
   - In diagram terms, there is a clear boundary between public pages and authenticated account-only functionality.

12. **How the main data flows connect**
   - **Chat browsing flow:** app route → chat page → inbox query → chat details/messages from server API → rendered through chat feature UI.
   - **Send message flow:** composer UI → message mutation hook → server message service → Prisma write → realtime broadcast/presence update.
   - **Attachment flow:** upload UI → UploadThing route/core → stored file metadata → message/attachment record → attachment rendering in message list.
   - **Presence flow:** client realtime hooks ↔ realtime route/service ↔ Redis-backed presence state ↔ UI indicators.

13. **Overall architectural style**
   - The repo follows a **feature-sliced frontend** over a **domain-organized server** inside a single Next.js codebase.
   - It is not just a UI app; it combines routing, server domain logic, database persistence, realtime signaling, and file upload handling.
   - For an architecture diagram, the most important boundaries are: **App Router shell**, **chat feature UI**, **typed server API**, **Prisma database**, **Redis-backed realtime/presence**, and **UploadThing-based file storage**.

Main entry points: [`src/server`](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server) (Server API), [`src/features/chat`](https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat) (Chat feature), [`src/shared/lib`](https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/lib) (Client libs), [`prisma/schema.prisma`](https://github.com/kirillpitomets/infinite-chat/blob/main/prisma/schema.prisma) (Prisma DB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["Next app"]
  node_node_root["App Router<br/>Next.js shell"]
  node_node_account["Account area<br/>protected routes"]
  node_node_account_layout["Account layout<br/>route shell<br/>[layout.tsx]"]
  node_node_api["API routes<br/>route handlers"]
  node_node_chat_page["Chat routes<br/>route pages"]
end

subgraph group_group_chat["Chat feature"]
  node_node_chat_feature["Chat feature<br/>frontend feature"]
  node_node_chat_ui["Chat UI<br/>composed views"]
  node_node_chat_message["Message flow<br/>message feature"]
  node_node_chat_realtime["Realtime hooks<br/>client sync"]
  node_node_chat_pages["Chat pages<br/>page states"]
end

subgraph group_group_server["Server domain"]
  node_node_server_root["Server API<br/>typed backend"]
  node_node_server_chat["Chat service<br/>domain service"]
  node_node_server_message["Message service<br/>domain service"]
  node_node_server_presence["Presence service<br/>realtime domain"]
  node_node_server_user["User service<br/>domain service"]
  node_node_server_storage["File storage<br/>attachment pipeline"]
end

subgraph group_group_shared["Shared layer"]
  node_node_shared_ui["Shared UI<br/>reusable components"]
  node_node_shared_state["Contexts<br/>global state"]
  node_node_shared_lib["Client libs<br/>client utilities"]
  node_node_shared_contracts["Shared contracts<br/>schemas"]
end

subgraph group_group_data["Data/integration"]
  node_node_prisma[("Prisma DB<br/>relational persistence<br/>[schema.prisma]")]
end

node_node_root -->|"routes"| node_node_account
node_node_root -->|"mounts"| node_node_api
node_node_account -->|"wraps"| node_node_account_layout
node_node_account -->|"contains"| node_node_chat_page
node_node_chat_page -->|"renders"| node_node_chat_feature
node_node_chat_feature -->|"composes"| node_node_chat_ui
node_node_chat_feature -->|"orchestrates"| node_node_chat_message
node_node_chat_feature -->|"subscribes"| node_node_chat_realtime
node_node_chat_feature -->|"uses"| node_node_chat_pages
node_node_chat_ui -->|"reuses"| node_node_shared_ui
node_node_chat_ui -->|"reads"| node_node_shared_state
node_node_chat_message -->|"validates"| node_node_shared_contracts
node_node_chat_message -->|"calls"| node_node_shared_lib
node_node_chat_realtime -->|"connects"| node_node_shared_lib
node_node_api -->|"dispatches"| node_node_server_root
node_node_server_root -->|"includes"| node_node_server_chat
node_node_server_root -->|"includes"| node_node_server_message
node_node_server_root -->|"includes"| node_node_server_presence
node_node_server_root -->|"includes"| node_node_server_user
node_node_server_root -->|"includes"| node_node_server_storage
node_node_server_chat -->|"maps"| node_node_shared_contracts
node_node_server_message -->|"maps"| node_node_shared_contracts
node_node_server_presence -->|"uses"| node_node_shared_lib
node_node_server_message -->|"persists"| node_node_prisma
node_node_server_chat -->|"persists"| node_node_prisma
node_node_server_user -->|"persists"| node_node_prisma
node_node_server_storage -->|"references"| node_node_prisma
node_node_shared_state -->|"backs"| node_node_shared_lib

click node_node_root "https://github.com/kirillpitomets/infinite-chat/tree/main/src/app"
click node_node_account "https://github.com/kirillpitomets/infinite-chat/tree/main/src/app/account"
click node_node_account_layout "https://github.com/kirillpitomets/infinite-chat/blob/main/src/app/account/layout.tsx"
click node_node_api "https://github.com/kirillpitomets/infinite-chat/tree/main/src/app/api"
click node_node_chat_page "https://github.com/kirillpitomets/infinite-chat/tree/main/src/app/account/chat"
click node_node_chat_feature "https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat"
click node_node_chat_ui "https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/ui"
click node_node_chat_message "https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/message"
click node_node_chat_realtime "https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/realtime"
click node_node_chat_pages "https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/pages"
click node_node_server_root "https://github.com/kirillpitomets/infinite-chat/tree/main/src/server"
click node_node_server_chat "https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/chat"
click node_node_server_message "https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/message"
click node_node_server_presence "https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/presence"
click node_node_server_user "https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/user"
click node_node_server_storage "https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/fileStorage"
click node_node_shared_ui "https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/components"
click node_node_shared_state "https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/context"
click node_node_shared_lib "https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/lib"
click node_node_shared_contracts "https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/schemes"
click node_node_prisma "https://github.com/kirillpitomets/infinite-chat/blob/main/prisma/schema.prisma"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_root,node_node_account,node_node_account_layout,node_node_api,node_node_chat_page toneBlue
class node_node_chat_feature,node_node_chat_ui,node_node_chat_message,node_node_chat_realtime,node_node_chat_pages toneAmber
class node_node_server_root,node_node_server_chat,node_node_server_message,node_node_server_presence,node_node_server_user,node_node_server_storage toneMint
class node_node_shared_ui,node_node_shared_state,node_node_shared_lib,node_node_shared_contracts toneRose
class node_node_prisma toneIndigo
```

## Components

### Next app

App Router routes, account shell, and API entrypoints.

- [App Router](https://github.com/kirillpitomets/infinite-chat/tree/main/src/app): Next.js shell · Top-level routing for public and authenticated areas. · `src/app`
- [Account area](https://github.com/kirillpitomets/infinite-chat/tree/main/src/app/account): protected routes · Authenticated section containing chat plus sibling product pages. · `src/app/account`
- [Account layout](https://github.com/kirillpitomets/infinite-chat/blob/main/src/app/account/layout.tsx): route shell · Shared wrapper for account subroutes. · `src/app/account/layout.tsx`
- [API routes](https://github.com/kirillpitomets/infinite-chat/tree/main/src/app/api): route handlers · Catch-all typed API plus realtime and upload endpoints. · `src/app/api`
- [Chat routes](https://github.com/kirillpitomets/infinite-chat/tree/main/src/app/account/chat): route pages · Chat entry and room pages under the account area. · `src/app/account/chat`

### Chat feature

Primary user workflow for inbox, rooms, messaging, and realtime updates.

- [Chat feature](https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat): frontend feature · End-user chat experience composed from inbox, room, message, and realtime pieces. · `src/features/chat`
- [Chat UI](https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/ui): composed views · Layout, inbox, header, message list, input, typing banner, and page states. · `src/features/chat/ui`
- [Message flow](https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/message): message feature · Message querying, sending, editing, deleting, status updates, and attachment shaping. · `src/features/chat/message`
- [Realtime hooks](https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/realtime): client sync · Inbox, chat, and typing subscriptions for live updates. · `src/features/chat/realtime`
- [Chat pages](https://github.com/kirillpitomets/infinite-chat/tree/main/src/features/chat/pages): page states · Room and empty-state screens for the chat surface. · `src/features/chat/pages`

### Server domain

Typed backend services and domain contracts.

- [Server API](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server): typed backend · Composition root for domain services and API surface. · `src/server`
- [Chat service](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/chat): domain service · Chat domain logic and DTO mapping. · `src/server/api/chat`
- [Message service](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/message): domain service · Message domain logic and DTO mapping. · `src/server/api/message`
- [Presence service](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/presence): realtime domain · Presence and typing state backed by ephemeral storage. · `src/server/api/presence`
- [User service](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/user): domain service · User domain and controller contracts. · `src/server/api/user`
- [File storage](https://github.com/kirillpitomets/infinite-chat/tree/main/src/server/api/fileStorage): attachment pipeline · Attachment DTO transformation and upload integration support. · `src/server/api/fileStorage`

### Shared layer

Reusable UI, hooks, contexts, contracts, and client libraries.

- [Shared UI](https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/components): reusable components · Sidebar, dialogs, loaders, inputs, typing indicator, previews, and icon set. · `src/shared/components`
- [Contexts](https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/context): global state · Current user, presence, and notifications providers. · `src/shared/context`
- [Client libs](https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/lib): client utilities · Eden client, realtime helpers, Redis access, and auth/navigation hooks. · `src/shared/lib`
- [Shared contracts](https://github.com/kirillpitomets/infinite-chat/tree/main/src/shared/schemes): schemas · Frontend/backend schemas and shared types for chat, message, user, and preview data. · `src/shared/schemes`

### Data/integration

Persistence and external service boundaries.

- [Prisma DB](https://github.com/kirillpitomets/infinite-chat/blob/main/prisma/schema.prisma): relational persistence · Schema and migrations for users, chats, messages, attachments, and read state. · `prisma/schema.prisma`

## Connections

- App Router → Account area: routes · App Router exposes authenticated account section.
- App Router → API routes: mounts · App Router exposes HTTP entrypoints.
- Account area → Account layout: wraps · Shared layout applies across account pages.
- Account area → Chat routes: contains · Account area includes chat route tree.
- Chat routes → Chat feature: renders · Chat routes are implemented by the chat feature.
- Chat feature → Chat UI: composes · Primary UI is assembled from chat-specific views.
- Chat feature → Message flow: orchestrates · Feature coordinates message actions and rendering.
- Chat feature → Realtime hooks: subscribes · Feature consumes live inbox/chat/typing updates.
- Chat feature → Chat pages: uses · Feature provides empty and room page states.
- Chat UI → Shared UI: reuses · Chat UI builds on shared primitives.
- Chat UI → Contexts: reads · UI consumes user, presence, and notification context.
- Message flow → Shared contracts: validates · Message models align with shared schemas.
- Message flow → Client libs: calls · Message hooks use client-side API and helper libraries.
- Realtime hooks → Client libs: connects · Realtime hooks use shared transport helpers.
- API routes → Server API: dispatches · Route handlers delegate into typed server composition.
- Server API → Chat service: includes · Server root composes chat domain.
- Server API → Message service: includes · Server root composes message domain.
- Server API → Presence service: includes · Server root composes presence domain.
- Server API → User service: includes · Server root composes user domain.
- Server API → File storage: includes · Server root composes file storage integration.
- Chat service → Shared contracts: maps · Chat DTOs and controllers align to shared contracts.
- Message service → Shared contracts: maps · Message DTOs and controllers align to shared contracts.
- Presence service → Client libs: uses · Presence relies on Redis and realtime helpers.
- Message service → Prisma DB: persists · Messages are stored through Prisma.
- Chat service → Prisma DB: persists · Chats and join state are stored through Prisma.
- User service → Prisma DB: persists · Users are stored through Prisma.
- File storage → Prisma DB: references · Attachment metadata is associated with persisted message records.
- Contexts → Client libs: backs · Contexts are driven by shared client utilities and providers.
