# mrmoneyinthebank/zetamax architecture

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

- Interactive diagram: https://gitdiagram.com/mrmoneyinthebank/zetamax
- Repository: https://github.com/mrmoneyinthebank/zetamax

## Overview

##### Project Type and Purpose
- Project Type: Full-stack interactive game application.
- Purpose: ZetaMax is an interactive mathematics game designed to help users practice and improve their math skills via a real-time, engaging, and timer-driven gaming experience.
- Key Technologies: Next.js (React for frontend), Convex (for real-time backend and database management), Clerk (for authentication), TailwindCSS (with shadcn/ui for styling), and TypeScript.

##### Main Components
a. Frontend (Next.js Application)
- Components: Separate by directories such as /app, /components (which further break down into ui, custom, clerk, hover components), /contexts, /hooks, etc.
- Pages/Layouts: Represented by the Next.js routing structure in /src/app (pages, sign-in, sign-up, globals, etc.).
b. Backend / Real-time Database Layer (Convex)
- Files found in /convex (database schema, mutation/query functions, and generated types/API).
- API functions like addUserScore.ts, createUser.ts, etc., that map out backend logic.
c. Authentication Service (Clerk)
- Integration present in multiple parts of the project (evidenced by /src/app/api/webhooks, /components/clerk-components, and /src/webhooks/clerk).
- User management and authentication logic, including webhook handling.
d. Utility and State Management:
- Custom React hooks (in /src/hooks) for game state, timers, and local scores.
- Context providers (in /src/contexts) for managing state like localScoresContext and userContext.
- Utility functions under /src/lib and configuration under /src/utils.
e. Webhooks:
- Handling for external requests (e.g., sending events from Clerk) which is found under /src/webhooks.

##### Relationships and Interactions
- The Frontend (Next.js app) interacts with the backend components of Convex for real-time data (score tracking, persistent storage) using APIs defined in the convex folder.
- Authentication through Clerk is integrated into both the frontend components (see sign-in/sign-up pages and clerk-components) and backend (via webhook handlers).
- Custom hooks and contexts manage and propagate the state, coordinating between user interactions, game logic, and real-time database updates.
- Webhooks serve as integration points to interact with external services (e.g., Clerk events) and update both frontend state and backend records.

##### Architectural Patterns / Design Principles
- Full-stack separation: Clear demarcation between frontend (Next.js / React UI) and backend services (Convex functions).
- Modular design: Components are grouped by functionality (UI, game-specific logic, authentication).
- Real-time synchronization: Convex ensures the game scores and user data are updated in real time.
- Use of custom hooks and contexts for state management abstracts business logic away from UI components.
- External service integration: Clerk handles authentication while webhooks ensure the proper handling of external events.

##### Putting It All Together
- A “Client” block representing browsers running the Next.js React app.
- Inside the Client block, subdivide areas for UI Components, State Management (Hooks & Contexts), and API Interface.
- A separate “Backend” block showing the Convex layer, with links to specific functions (user scoring, data fetching, data model).
- From Client UI actions to Backend API requests.
- From Clerk’s authentication events to both backend processing (webhooks) and UI updates.
- From Convex updates back to Client state updates via the custom hooks.
- Every component is annotated with details (like the libraries used and their purposes) to allow for easy identification by other engineers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Client (Frontend)"
        direction TB
        PL["Pages & Layouts"]:::frontend
        subgraph "UI Components"
            direction TB
            UICore["UI Core"]:::frontend
            CustomUI["Custom Components"]:::frontend
            UIHover["Hover Components"]:::frontend
        end
        subgraph "State Management"
            direction TB
            Hooks["React Hooks"]:::util
            Contexts["Context Providers"]:::util
        end
    end

    %% Backend Layer
    subgraph "Backend"
        direction TB
        Convex["Convex (API & Database)"]:::backend
        subgraph "Backend Utilities"
            direction TB
            LibHelpers["Library Helpers"]:::util
            EnvConfig["Env Config"]:::util
        end
    end

    %% Authentication (Clerk)
    subgraph "Authentication (Clerk)"
        direction TB
        ClerkAPI["Clerk API"]:::auth
        ClerkUI["Clerk UI"]:::auth
        ClerkHandlers["Clerk Handlers"]:::auth
        ClerkUtils["Clerk Utils"]:::auth
    end

    %% External Webhooks
    subgraph "External Webhooks"
        direction TB
        APIWebhooks["API Webhooks"]:::external
        ExtWebhooks["External Webhooks"]:::external
    end

    %% Relationships and Data Flow
    PL -->|"renders"| UICore
    PL -->|"renders"| CustomUI
    PL -->|"renders"| UIHover
    UICore -->|"interaction"| Hooks
    CustomUI -->|"interaction"| Hooks
    UIHover -->|"interaction"| Hooks
    Hooks -->|"updates"| Convex
    Convex -->|"real-time"| Hooks
    PL -->|"authFlow"| ClerkUI
    ClerkUI -->|"authRequest"| ClerkAPI
    ClerkAPI -->|"authUpdate"| Hooks
    ClerkAPI -->|"userUpdate"| Convex
    ClerkUtils -->|"configures"| ClerkAPI
    APIWebhooks -->|"event"| ClerkHandlers
    ExtWebhooks -->|"event"| ClerkHandlers
    ClerkHandlers -->|"backendSync"| Convex
    LibHelpers -->|"supports"| Convex
    EnvConfig -->|"configures"| Convex

    %% Click Events
    click PL "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/app"
    click UICore "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/components/ui"
    click CustomUI "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/components/custom-components"
    click UIHover "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/components/hover-components"
    click Hooks "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/hooks"
    click Contexts "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/contexts"
    click Convex "https://github.com/mrmoneyinthebank/zetamax/tree/main/convex"
    click LibHelpers "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/lib"
    click EnvConfig "https://github.com/mrmoneyinthebank/zetamax/blob/main/src/utils/env/env.ts"
    click ClerkAPI "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/app/api/webhooks/clerk"
    click ClerkUI "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/components/clerk-components"
    click ClerkHandlers "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/webhooks/clerk"
    click ClerkUtils "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/utils/clerk"
    click APIWebhooks "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/app/api/webhooks"
    click ExtWebhooks "https://github.com/mrmoneyinthebank/zetamax/tree/main/src/webhooks"

    %% Styles
    classDef frontend fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef backend fill:#ffcdd2,stroke:#b71c1c,stroke-width:2px;
    classDef auth fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef util fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
    classDef external fill:#d1c4e9,stroke:#512da8,stroke-width:2px;
```
