# arthurdedeus/r2 architecture

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

- Interactive diagram: https://gitdiagram.com/arthurdedeus/r2
- Repository: https://github.com/arthurdedeus/r2

## Overview

##### Main components
- Frontend (User Interface): The pages and components under src/app (for pages, layouts, and error handling) and src/components (for reusable UI elements).
- API Endpoints: Located under src/app/api, these endpoints (e.g., for “habit-groups” and “habits”) serve as back-end routes for handling data operations.
- Business Logic & Libraries: The src/lib directory includes CRUD operations, Supabase integration (server-side and database types), and utility functions.
- Styles & Configurations: Global styling and configuration files like tailwind.config.ts, postcss.config.mjs, and Next.js config files.
- External Services/Third Party: Third-party packages (e.g., Next.js, Supabase, tailwind) and potential external APIs (as inferred from Supabase integration for database operations).

##### Relationships and interactions
- Users (clients) interact with the Next.js application hosted on a server.
- The Frontend renders pages and components that call API endpoints when data needs to be fetched, updated, or deleted.
- The API routes (such as those in src/app/api/habits and habit-groups) invoke functions from the business logic (in src/lib) to communicate with the database via Supabase.
- Utility functions encapsulate common functionality and support both API route handlers and UI logic.
- Configuration files ensure proper building, linting, styling, and deployment (e.g., Vercel deployment).

##### Important architectural patterns and design principles
- Separation of Concerns: Frontend components (UI) are separated from back-end API logic.
- Modular Organization: Clear file structure divides the application into focused areas, such as API endpoints, client components, and libraries.
- Component Reusability: Shared UI components (in src/components and src/app/habits/components) promote reuse across pages.
- Client-Server Interaction: API routes act as interfaces for CRUD operations, following RESTful design.

##### Relevant technologies and frameworks
- Next.js for both page routing (via the App Router) and API route handling.
- Supabase for database interactions.
- Tailwind CSS and PostCSS for styling.
- TypeScript for type safety (as seen by the tsconfig.json and .ts/.tsx files).
- ESLint for code quality (see eslint.config.mjs).
- Vercel deployment practices, as recommended by the README.

- From the “Client (Browser)” to “Frontend UI” and onward to “API Endpoints”.
- From “API Endpoints” invoking “Business Logic” which then communicates with “Database/External Service”.
- Interactions such as HTTP request/response cycles between client and server, internal module interactions (for instance, a call from an API endpoint to a Supabase helper in src/lib).

## Diagram (Mermaid)

```mermaid
graph TB
    %% Nodes
    Browser("Browser (Client)"):::client

    subgraph "Next.js Application Server"
        subgraph "Frontend UI"
            subgraph "Pages & Layouts"
                P1("Home Page (src/app/page.tsx)"):::frontend
                L1("Layout (src/app/layout.tsx)"):::frontend
                P2("Habits Page (src/app/habits/page.tsx)"):::frontend
                E1("Habits Error (src/app/habits/error.tsx)"):::frontend
            end
            subgraph "Reusable UI Components"
                HT("Habits Table (src/app/habits/components/habits-table.tsx)"):::frontend
                MS("Month Selector (src/app/habits/components/month-selector.tsx)"):::frontend
                WN("Week Navigation (src/app/habits/components/week-navigation.tsx)"):::frontend
                SC("Select Component (src/components/select.tsx)"):::frontend
                UB("UI Button (src/components/ui/button.tsx)"):::frontend
            end
        end
        subgraph "API Endpoints"
            HG("Habit Groups API (src/app/api/habit-groups/route.ts)"):::api
            HA("Habits API (src/app/api/habits/[id]/route.ts)"):::api
        end
    end

    subgraph "Business Logic & Supabase Integration"
        subgraph "CRUD & Utilities"
            CRUD("CRUD Operations (src/lib/crud.ts)"):::logic
            Utils("Utility Functions (src/lib/utils.ts)"):::logic
        end
        subgraph "Supabase Integration"
            SS("Supabase Server (src/lib/supabase/server.ts)"):::logic
            ST("Supabase Types (src/lib/supabase/database.types.ts)"):::logic
        end
    end

    subgraph "Database/External Service"
        DB(("Supabase Database")):::db
    end

    subgraph "Configuration/Build"
        GS("Globals (src/app/globals.css)"):::config
        TW("Tailwind Config (tailwind.config.ts)"):::config
        PC("PostCSS Config (postcss.config.mjs)"):::config
        NC("Next.js Config (next.config.ts)"):::config
        TS("TypeScript Config (tsconfig.json)"):::config
    end

    %% Connections
    Browser -->|"HTTP_Request"| P1
    P2 -->|"triggers"| HG
    P2 -->|"triggers"| HA
    HG -->|"calls"| CRUD
    HA -->|"calls"| CRUD
    CRUD -->|"invokes"| SS
    SS -->|"queries"| DB
    GS ---|"build/styling"| L1

    %% Click Events
    click P1 "https://github.com/arthurdedeus/r2/blob/main/src/app/page.tsx"
    click L1 "https://github.com/arthurdedeus/r2/blob/main/src/app/layout.tsx"
    click P2 "https://github.com/arthurdedeus/r2/blob/main/src/app/habits/page.tsx"
    click E1 "https://github.com/arthurdedeus/r2/blob/main/src/app/habits/error.tsx"
    click HT "https://github.com/arthurdedeus/r2/blob/main/src/app/habits/components/habits-table.tsx"
    click MS "https://github.com/arthurdedeus/r2/blob/main/src/app/habits/components/month-selector.tsx"
    click WN "https://github.com/arthurdedeus/r2/blob/main/src/app/habits/components/week-navigation.tsx"
    click SC "https://github.com/arthurdedeus/r2/blob/main/src/components/select.tsx"
    click UB "https://github.com/arthurdedeus/r2/blob/main/src/components/ui/button.tsx"
    click HG "https://github.com/arthurdedeus/r2/blob/main/src/app/api/habit-groups/route.ts"
    click HA "https://github.com/arthurdedeus/r2/blob/main/src/app/api/habits/[id]/route.ts"
    click CRUD "https://github.com/arthurdedeus/r2/blob/main/src/lib/crud.ts"
    click Utils "https://github.com/arthurdedeus/r2/blob/main/src/lib/utils.ts"
    click SS "https://github.com/arthurdedeus/r2/blob/main/src/lib/supabase/server.ts"
    click ST "https://github.com/arthurdedeus/r2/blob/main/src/lib/supabase/database.types.ts"
    click GS "https://github.com/arthurdedeus/r2/blob/main/src/app/globals.css"
    click TW "https://github.com/arthurdedeus/r2/blob/main/tailwind.config.ts"
    click PC "https://github.com/arthurdedeus/r2/blob/main/postcss.config.mjs"
    click NC "https://github.com/arthurdedeus/r2/blob/main/next.config.ts"
    click TS "https://github.com/arthurdedeus/r2/blob/main/tsconfig.json"

    %% Styles
    classDef client fill:#fef3bd,stroke:#d4a373,stroke-width:2px;
    classDef frontend fill:#cce5ff,stroke:#66b3ff,stroke-width:2px;
    classDef api fill:#d4edda,stroke:#66cdaa,stroke-width:2px;
    classDef logic fill:#fff3cd,stroke:#f0ad4e,stroke-width:2px;
    classDef db fill:#f8d7da,stroke:#e4606d,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef config fill:#e2e3e5,stroke:#adb5bd,stroke-width:2px;
```
