# divyamdotfoo/earthweinherit architecture

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

- Interactive diagram: https://gitdiagram.com/divyamdotfoo/earthweinherit
- Repository: https://github.com/divyamdotfoo/earthweinherit

## Overview

##### Main Components
- Client / Web Browser: The end-user’s interface that loads the Next.js front-end.
- Frontend (Next.js Application): Built using the new Next.js “app” directory. This includes:
– Page components (e.g., src/app/page.tsx, src/app/chat/page.tsx, src/app/chat/[id]/page.tsx)
– Layouts, global styles (src/app/layout.tsx, globals.css)
– UI components like chat UI, navbar, sidebar, charts, and various custom components within src/components.
– Hooks (e.g., use-in-view, use-scroll-bottom) to manage client-side behaviors.
- API Routes: Server-side endpoints implemented in the Next.js app under src/app/api/ (for chat, error handling, history, wakeup). These serve as serverless functions handling client requests and interfacing with backend resources.
- Backend / Database (Supabase): The project integrates with Supabase (files under src/supabase) providing:
– Database connection (db.ts)
– Various helper queries and operations such as search, statistics, text/image embedding, and querying climate data.
- Utility & Helper Libraries: Found in the src/lib directory, these provide supporting functions like resource management, pagination (totalPages), and other utilities.

##### Relationships and Interactions Between Components
– The Client interacts with the Frontend which renders pages and components.
– Frontend pages (chat and others) make calls to API routes to fetch or update data — for instance, obtaining answers to climate questions.
– API routes interact with the Supabase backend to retrieve data, record chat histories, perform searches, and manage AI-related queries.
– Utility functions across both the front-end components and API routes help standardize operations and data formatting.
– The project follows a separation of concerns with a clear division between UI rendering, API processing, and backend data management.

##### Important Architectural Patterns or Principles
– Full-Stack Separation: Clear split between client (Next.js pages/components) and server logic (API routes).
– Serverless Architecture: API routes defined under src/app/api follow a serverless paradigm, executing on-demand.
– Modular and Reusable Components: The components and hooks directories show an emphasis on reusable UI elements and client-side behaviors.
– External Integration: Supabase is used as a BaaS solution, showing the design’s intent to use managed cloud services for data storage and queries.

##### Relevant Technologies, Frameworks, or Libraries
– Next.js for the web application framework, utilizing the new “app” directory.
– Tailwind CSS and PostCSS for styling (evidenced by tailwind.config.ts, postcss.config.mjs).
– Supabase for database operations and cloud functions.
– TypeScript (tsconfig.json) for type safety.
– Various libraries and custom hooks for UI interactivity.

##### Instructing the Principal Software Engineer
· From Client → Next.js Frontend (user requests and responses).
· From Frontend → API Endpoints (AJAX/fetch calls for chat queries and data management).
· From API Endpoints → Supabase (database interactions for queries, chat histories, and statistics).
– Optionally annotate the interactions to reference architectural patterns, such as “Serverless API”, “Component-based UI”, and “Cloud Database Integration”.

##### Capturing Essential Architectural Elements
– Ensure you separate as many components as possible. Each file/directory grouping (e.g., UI components, API endpoints, Supabase integrations) should be clearly represented to demonstrate both the breadth and depth of the system.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        C1["Client Web Browser"]:::client
    end

    %% Frontend Layer (Next.js UI)
    subgraph "Frontend Layer (Next.js UI)"
        P1["Page: src/app/page.tsx"]:::frontend
        CP["Chat Page: src/app/chat/page.tsx"]:::frontend
        CIP["Chat Id Page: src/app/chat/[id]/page.tsx"]:::frontend
        CL["Loading: src/app/chat/[id]/loading.tsx"]:::frontend
        CLay["Chat Layout: src/app/chat/layout.tsx"]:::frontend
        NF["NotFound: src/app/not-found.tsx"]:::frontend

        L["Layout: src/app/layout.tsx"]:::frontend
        G["Globals: src/app/globals.css"]:::frontend

        CU["Chat UI: src/components/chat.tsx"]:::frontend
        NB["Navbar: src/components/navbar.tsx"]:::frontend
        SB["Sidebar: src/components/sidebar.tsx"]:::frontend
        CH["Chart: src/components/charts/carbon-temp-sea-ice.tsx"]:::frontend
        CUdir["UI Components: src/components/ui"]:::frontend

        HI["useInView Hook: src/hooks/use-in-view.tsx"]:::frontend
        HS["useScrollBottom Hook: src/hooks/use-scroll-bottom.tsx"]:::frontend
    end

    %% API Layer (Serverless Endpoints)
    subgraph "API Layer (Serverless Endpoints)"
        CA["Chat API: /api/chat (src/app/api/chat/route.ts)"]:::api
        EA["Error API: /api/error (src/app/api/error/route.ts)"]:::api
        HA["History API: /api/history (src/app/api/history/route.ts)"]:::api
        WA["Wakeup API: /api/wakeup (src/app/api/wakeup/route.ts)"]:::api
    end

    %% Backend Layer (Supabase)
    subgraph "Backend Layer (Supabase)"
        DB["Database: db.ts"]:::backend
        EI["Embed Image: embedImage.ts"]:::backend
        ET["Embed Text: embedText.ts"]:::backend
        Q["Queries: queries.ts"]:::backend
        S["Search: search.ts"]:::backend
        ST["Statistics: statistics.ts"]:::backend
        T["Types: types.ts"]:::backend
    end

    %% Utilities & Helpers
    subgraph "Utilities & Helpers"
        R["Resources: resources.ts"]:::utils
        TP["TotalPages: totalPages.ts"]:::utils
        U["Utils: utils.ts"]:::utils
    end

    %% Connections
    C1 -->|"request"| P1
    %% Frontend page to API call
    CU -->|"fetchData"| CA

    %% API Endpoints call Database
    CA -->|"queryDB"| DB
    EA -->|"queryDB"| DB
    HA -->|"queryDB"| DB
    WA -->|"queryDB"| DB

    %% API and Frontend use Utilities
    CA ---|"helper"| R
    HI ---|"helper"| R

    %% Additional internal Frontend relations
    L --- G

    %% Styles
    classDef client fill:#f7c,stroke:#333,stroke-width:2px;
    classDef frontend fill:#ccf,stroke:#333,stroke-width:2px;
    classDef api fill:#cfc,stroke:#333,stroke-width:2px;
    classDef backend fill:#ffc,stroke:#333,stroke-width:2px;
    classDef utils fill:#eee,stroke:#333,stroke-width:2px;

    %% Click Events for Frontend
    click P1 "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/page.tsx"
    click CP "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/chat/page.tsx"
    click CIP "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/chat/[id]/page.tsx"
    click CL "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/chat/[id]/loading.tsx"
    click CLay "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/chat/layout.tsx"
    click NF "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/not-found.tsx"
    click L "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/layout.tsx"
    click G "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/globals.css"
    click CU "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/components/chat.tsx"
    click NB "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/components/navbar.tsx"
    click SB "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/components/sidebar.tsx"
    click CH "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/components/charts/carbon-temp-sea-ice.tsx"
    click CUdir "https://github.com/divyamdotfoo/earthweinherit/tree/main/src/components/ui"
    click HI "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/hooks/use-in-view.tsx"
    click HS "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/hooks/use-scroll-bottom.tsx"

    %% Click Events for API
    click CA "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/api/chat/route.ts"
    click EA "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/api/error/route.ts"
    click HA "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/api/history/route.ts"
    click WA "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/app/api/wakeup/route.ts"

    %% Click Events for Backend / Supabase
    click DB "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/db.ts"
    click EI "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/embedImage.ts"
    click ET "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/embedText.ts"
    click Q "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/queries.ts"
    click S "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/search.ts"
    click ST "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/statistics.ts"
    click T "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/supabase/types.ts"

    %% Click Events for Utilities & Helpers
    click R "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/lib/resources.ts"
    click TP "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/lib/totalPages.ts"
    click U "https://github.com/divyamdotfoo/earthweinherit/blob/main/src/lib/utils.ts"
```
