# meghrajjare5273/legalmind architecture

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

- Interactive diagram: https://gitdiagram.com/meghrajjare5273/legalmind
- Repository: https://github.com/meghrajjare5273/legalmind

## Overview

##### Project type and purpose
- This is a full-stack Next.js (React + Node.js) application that provides user authentication, protected dashboards, analytics, a chat service, contract review, and research tooling.
- Key features: sign-in/up flows, server-side API endpoints (e.g. chat, RAG, contract analysis), database persistence via Prisma, theming, UI components.

##### File structure
- Top-level:
- prisma/schema.prisma – database schema
- src/app – Next.js App Router:
– (auth) and (protected) route groups
– API routes under src/app/api (analyze-contract, auth, chat, rag)
– global layout (app/layout.tsx), CSS (globals.css)
- src/components – UI primitives and feature-specific components (auth, landing, protected services, theme toggle, UI controls)
- src/lib – helper modules (auth client, Prisma client, types, utils)
- src/contexts & src/hooks – React context for session, custom hooks
- middleware.ts – global request handling (auth, redirects)
- Config files: next.config.ts, tsconfig.json, eslint, postcss

##### README for additional insights
- Confirms Next.js App Router usage, deployment via Vercel, optimized font loading.

a. Main components
1. Client / Browser (React in the browser)
2. Next.js Server (Node.js SSR & API layer)
3. Database (PostgreSQL or MySQL via Prisma ORM)
4. External AI Services (e.g. OpenAI for Chat & RAG)
5. Identity Provider (NextAuth or custom JWT flow)
6. CDN / Static Assets (Vercel / public folder)
b. Relationships & data flow
- Browser ↔ Next.js Server: page navigation (SSR/ISR), client-side fetching (React hooks)
- Next.js Server → Database: Prisma queries (user sessions, chat history, analytics data)
- Next.js Server → External AI Services: REST/gRPC calls from API routes (chat, analyze-contract, RAG)
- Static files served from CDN / public folder
- Middleware handles authentication & route protection before hitting server components
c. Architectural patterns & design principles
- App Router with nested layouts → logical separation of “(auth)” vs “(protected)” routes
- MVC‐style controllers: API routes act as controllers, Prisma as the model, React components as the view
- Shared UI component library under src/components/ui → DRY, design system
- Context/Hook pattern for global session & toast notifications
- Separation: frontend (pages + components) vs backend (API routes + lib + prisma)
d. Key technologies/frameworks
- Next.js (App Router, server‐side rendering, API routes)
- React + TypeScript
- Prisma ORM + a relational database
- NextAuth or custom auth handlers in src/lib/auth
- Tailwind/PostCSS for styling, Sonner for toasts
- Vercel for deployment & CDN

##### Essential architectural elements
- Middleware.ts intercepting and routing auth.
- Prisma’s role in all DB interactions.
- How shared UI components feed into each page.
- Don’t overcomplicate – break into logical subsystems and show their interactions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client / Browser"
        direction TB
        ReactApp["React Frontend"]:::frontend
        click ReactApp "https://github.com/meghrajjare5273/legalmind/tree/main/src/app"
        Components["UI Components"]:::frontend
        click Components "https://github.com/meghrajjare5273/legalmind/tree/main/src/components"
        Contexts["Contexts & Hooks"]:::frontend
        click Contexts "https://github.com/meghrajjare5273/legalmind/tree/main/src/contexts"
        click Contexts "https://github.com/meghrajjare5273/legalmind/tree/main/src/hooks"
        SharedUI["Shared UI / Theming"]:::frontend
        click SharedUI "https://github.com/meghrajjare5273/legalmind/tree/main/src/components/ui"
        ThemeProvider["Theme Provider"]:::frontend
        click ThemeProvider "https://github.com/meghrajjare5273/legalmind/blob/main/src/components/theme-provider.tsx"
        ThemeToggle["Theme Toggle"]:::frontend
        click ThemeToggle "https://github.com/meghrajjare5273/legalmind/blob/main/src/components/theme-toggle.tsx"
    end

    %% Server Layer
    subgraph "Server (Next.js)"
        direction TB
        Middleware["middleware.ts"]:::backend
        click Middleware "https://github.com/meghrajjare5273/legalmind/blob/main/middleware.ts"

        subgraph "App Router Layouts"
            direction TB
            GlobalLayout["layout.tsx & globals.css"]:::backend
            click GlobalLayout "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/layout.tsx"
            click GlobalLayout "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/globals.css"
            HomePage["Home Page"]:::frontend
            click HomePage "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/page.tsx"
        end

        subgraph "Auth Routes"
            direction TB
            AuthLayout["(auth) layout"]:::backend
            click AuthLayout "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(auth)/layout.tsx"
            SignIn["Sign-In Page"]:::frontend
            click SignIn "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(auth)/sign-in/page.tsx"
            SignUp["Sign-Up Page"]:::frontend
            click SignUp "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(auth)/sign-up/page.tsx"
        end

        subgraph "Protected Routes"
            direction TB
            ProtectedLayout["(protected) layout"]:::backend
            click ProtectedLayout "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(protected)/layout.tsx"
            Dashboard["Dashboard Page"]:::frontend
            click Dashboard "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(protected)/dashboard/page.tsx"
            Services["Services Directory"]:::frontend
            click Services "https://github.com/meghrajjare5273/legalmind/tree/main/src/app/(protected)/services"
            AnalyticsPage["Analytics Page"]:::frontend
            click AnalyticsPage "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(protected)/analytics/page.tsx"
            Settings["Settings Page"]:::frontend
            click Settings "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(protected)/settings/page.tsx"
        end

        subgraph "API Routes"
            direction TB
            AuthAPI["Auth API"]:::backend
            click AuthAPI "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/api/auth/[...all]/route.ts"
            ChatAPI["Chat API"]:::backend
            click ChatAPI "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/api/chat/[sessionId]/route.ts"
            ChatSessionsAPI["Chat Sessions API"]:::backend
            click ChatSessionsAPI "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/api/chat/sessions/route.ts"
            RAGAPI["RAG API"]:::backend
            click RAGAPI "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/api/rag/route.ts"
            ContractAPI["Analyze-Contract API"]:::backend
            click ContractAPI "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/api/analyze-contract/route.ts"
        end

        subgraph "Libs & Utils"
            direction TB
            PrismaClient["Prisma Client"]:::database
            click PrismaClient "https://github.com/meghrajjare5273/legalmind/blob/main/src/lib/prisma.ts"
            AuthLib["Auth Logic"]:::backend
            click AuthLib "https://github.com/meghrajjare5273/legalmind/blob/main/src/lib/auth.ts"
            AuthClient["Auth Client"]:::backend
            click AuthClient "https://github.com/meghrajjare5273/legalmind/blob/main/src/lib/auth-client.ts"
            Utils["Utilities"]:::backend
            click Utils "https://github.com/meghrajjare5273/legalmind/blob/main/src/lib/utils.ts"
            Types["Types"]:::backend
            click Types "https://github.com/meghrajjare5273/legalmind/blob/main/src/lib/types.ts"
        end
    end

    %% Database & External
    Database["Database (Prisma ORM)"]:::database
    click Database "https://github.com/meghrajjare5273/legalmind/blob/main/prisma/schema.prisma"
    ExternalAI["OpenAI GPT-4"]:::external
    StaticAssets["Static Assets / CDN"]:::external
    click StaticAssets "https://github.com/meghrajjare5273/legalmind/tree/main/public/"

    %% Feature-specific UI
    ChatFeature["Chat Feature UI"]:::frontend
    click ChatFeature "https://github.com/meghrajjare5273/legalmind/tree/main/src/components/protected/chat"
    ContractReviewUI["ContractReview Component"]:::frontend
    click ContractReviewUI "https://github.com/meghrajjare5273/legalmind/blob/main/src/components/protected/contract/ContractReview.tsx"
    ContractReviewPage["Contract Review Page"]:::frontend
    click ContractReviewPage "https://github.com/meghrajjare5273/legalmind/blob/main/src/app/(protected)/services/contract-review/page.tsx"

    %% Connections
    ReactApp --> Components
    ReactApp --> Contexts
    ReactApp --> SharedUI
    SharedUI --> ThemeProvider
    SharedUI --> ThemeToggle
    ReactApp --> HomePage
    ReactApp --> AuthAPI
    ReactApp --> ChatAPI
    ReactApp --> RAGAPI
    ReactApp --> ContractAPI
    ReactApp --> AnalyticsPage

    GlobalLayout --> HomePage
    Middleware --> GlobalLayout
    Middleware --> AuthLayout
    Middleware --> ProtectedLayout
    ReactApp --> StaticAssets

    AuthLayout --> SignIn
    AuthLayout --> SignUp

    Services --> ChatFeature
    ChatFeature --> ChatAPI

    ContractReviewPage --> ContractReviewUI
    ContractReviewUI --> ContractAPI

    AuthAPI --> AuthLib
    AuthAPI --> PrismaClient

    ChatAPI --> ExternalAI
    RAGAPI --> ExternalAI
    ContractAPI --> ExternalAI

    ChatAPI --> PrismaClient
    ChatSessionsAPI --> PrismaClient
    ContractAPI --> PrismaClient
    AnalyticsPage --> PrismaClient

    PrismaClient --> Database

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#004085
    classDef backend fill:#D4EDDA,stroke:#155724
    classDef database fill:#FFF3CD,stroke:#856404
    classDef external fill:#E2E3E5,stroke:#6C757D
```
