# musheer0/ex-drive-web architecture

> Architecture diagram of the GitHub repository musheer0/ex-drive-web, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/musheer0/ex-drive-web
- Repository: https://github.com/musheer0/ex-drive-web

## Overview

##### Project Type and Purpose
- This is a **full-stack, React/Next.js 13** application using the new “app” router.
- It provides a cloud-drive/media-management UI (upload, browse public/private media, real-time features via sockets).
- It exposes serverless API routes (`src/app/api/*`) for token management and likely other operations.
- It integrates a database layer (`src/DB/dexi.ts`), JWT-based auth (`private.key`, token APIs), and WebSockets for real-time updates.

##### File Structure & Key Components
- **Frontend**
– `src/app` contains all page routes: dashboard, upload, media (private & public), search, dev & socket demos, onboarding, shared layout.
– `src/components` holds React UI components (auth, dashboard nav/sidebar, media scrollers, search, socket listeners, file-upload UI, generic UI primitives).
– `src/hooks` and `src/stores` implement state management, side effects, and client-side caching (dashboard, media, search, socket, upload).
- **Backend/API**
– `src/app/api/token/*/route.ts` defines serverless token create/change endpoints.
– `middleware.ts` for request preprocessing (auth, session).
– `src/DB/dexi.ts` wraps database access logic.
- **Cross-cutting**
– `src/lib` has shared utilities: API fetch wrapper, auth helpers (decode-token, get-user-info), URL links, types, general utils.
– WebSockets under `src/components/socket` and `src/hooks/use-socket.ts`.
– Static assets under `public` (manifest.json).
– Configuration: Next.js (`next.config.ts`), ESLint, Tailwind, TypeScript.

##### README for Additional Insights
- The README is minimal, but the name “ex-drive-web” implies a user-focused drive/media storage service.

##### Main Architectural Components & Interactions
a. **User’s Browser (Client)**
– Renders React pages/SSR/SSG endpoints from Next.js.
– Uses React Contexts, custom hooks to manage auth, state, theme, search, media lists, upload progress.
– Initiates WebSocket connections for real-time updates (e.g., new file notifications).
– Calls REST-style API routes under `/api/token/...` and any other future endpoints (`/api/media`, etc.).
b. **Next.js Server (App Router + API Routes)**
– Serves page requests (SSR/SSG) and processes API calls.
– Runs `middleware.ts` for authorization, session management.
– Provides the server side of the WebSocket endpoint (if implemented via Next.js or a separate WS server).
c. **Database Layer**
– The `dexi.ts` module encapsulates all read/write operations to your chosen database (e.g., DynamoDB, PostgreSQL, etc.).
– Called directly from API route handlers or server components.
d. **Authentication**
– JWT tokens signed with `private.key`.
– `/api/token/add` and `/api/token/change` endpoints generate and rotate tokens.
– Client stores token (cookie/local storage), passed on each request.
e. **Media Storage**
– Could be local filesystem or external object storage (not shown in code but often integrated in the DB or via SDK).
– Upload flows handled in `src/components/file-uploader.tsx` + `use-upload-file.tsx`.
f. **WebSocket Server**
– Real-time channel for socket events (under `src/components/socket/demo.tsx` & `use-socket.ts`).
– Broadcasts new media uploads, search updates, dashboard events.
g. **Optional External Services**
– Analytics, CDN for media, third-party auth (none explicitly shown).

##### Architectural Patterns & Principles
- **Separation of Concerns**:
– UI layer (`src/app`, `src/components`),
– Business logic/API (`src/app/api/*`, `lib`),
– Data layer (`src/DB/*`).
- **Layered Architecture**: Client ↔ Next.js (presentation & API) ↔ DB.
- **Serverless Functions**: Each API route is its own handler.
- **Context & Hooks**: Encapsulate state & side effects for reusability.
- **Modular UI**: Atomic components (ui/, media/, search/) to compose pages.
- **Security**: JWT auth, middleware gating, token rotation.

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Client Side
  subgraph "User Browser" 
    direction TB
    Pages["Page Routes (Dashboard, Upload, Media, Search, Socket, Dev, Onboard, Shared)"]:::frontend
    UIComponents["React UI Components & Hooks"]:::frontend
    WebSocketClient["WebSocket Client (use-socket, demo, listener)"]:::frontend
    AuthContext["Auth Context & Hooks"]:::frontend
    Pages -->|renders| UIComponents
    UIComponents -->|uses| AuthContext
    UIComponents -->|uses| WebSocketClient
  end

  %% Next.js Server
  subgraph "Next.js Server" 
    direction TB
    Middleware["middleware.ts\n(Auth & Session)"]:::server
    PageRender["SSR/SSG Page Rendering"]:::server
    APIHandlers["API Routes (token, media, etc.)"]:::server
    WSHandler["WebSocket Endpoint"]:::server
    Middleware --> PageRender
    Middleware --> APIHandlers
    PageRender -->|data via| APIHandlers
    APIHandlers -->|DB ops| Dexi
    WSHandler -->|DB ops| Dexi
  end

  %% Database Layer
  Dexi["Database Layer (src/DB/dexi.ts)"]:::database

  %% Media Storage
  MediaStorage["Media Storage (Object Storage/FileSystem)"]:::storage

  %% External Services (optional)
  subgraph "External Services" 
    direction TB
    CDN["CDN / Analytics"]:::external
    CDN -.-> MediaStorage
  end

  %% Flows
  Pages -->|HTTP GET| PageRender
  UIComponents -->|HTTP POST/GET JWT| APIHandlers
  UIComponents -->|WS connect| WSHandler
  APIHandlers -->|read/write| Dexi
  Dexi -->|store/retrieve| MediaStorage

  %% Click Events - Pages
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/dashboard/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/upload/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/media/[id]/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/media/public/[id]/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/search/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/socket/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/dev/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/onboard/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/shared/page.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/layout.tsx"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/globals.css"
  click Pages "https://github.com/musheer0/ex-drive-web/blob/main/src/app/(root)/layout.tsx"

  %% Click Events - API & Middleware
  click APIHandlers "https://github.com/musheer0/ex-drive-web/blob/main/src/app/api/token/add/route.ts"
  click APIHandlers "https://github.com/musheer0/ex-drive-web/blob/main/src/app/api/token/change/[id]/route.ts"
  click Middleware "https://github.com/musheer0/ex-drive-web/blob/main/src/middleware.ts"

  %% Click Events - DB
  click Dexi "https://github.com/musheer0/ex-drive-web/blob/main/src/DB/dexi.ts"

  %% Click Events - Auth Utils
  click AuthContext "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/auth.ts"
  click AuthContext "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/decode-token.ts"
  click AuthContext "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/get-user-info.ts"
  click AuthContext "https://github.com/musheer0/ex-drive-web/blob/main/private.key"

  %% Click Events - Shared Utils
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/api-fetch.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/links.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/type.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/utils.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/lib/get-token.ts"

  %% Click Events - UI Components
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/auth/user-button.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/file-uploader.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/media/infinite-media-scroll.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/media/infinite-public-media-scroll.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/media/media-preview.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/search/search-bar.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/socket/demo.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/components/socket/socket-listner.tsx"

  %% Click Events - Hooks & Stores
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-dashboard.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-media.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-search.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-search-store.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-upload-file.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-socket.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/hooks/use-user.tsx"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/stores/dashboard-store.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/stores/file-store.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/src/stores/upload-store.ts"

  %% Click Events - Config
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/next.config.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/tailwind.config.ts"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/tsconfig.json"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/eslint.config.mjs"
  click UIComponents "https://github.com/musheer0/ex-drive-web/blob/main/postcss.config.mjs"

  %% Styles
  classDef frontend fill:#D0E8FF,stroke:#0366D6,color:#0366D6
  classDef server fill:#DFFFE0,stroke:#228B22,color:#228B22
  classDef database fill:#FFE4B5,stroke:#FF8C00,color:#FF8C00
  classDef storage fill:#F0F0F0,stroke:#808080,color:#808080
  classDef external fill:#F5F5F5,stroke:#A9A9A9,color:#A9A9A9
```
