# samjamead/better-faster architecture

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

- Interactive diagram: https://gitdiagram.com/samjamead/better-faster
- Repository: https://github.com/samjamead/better-faster

## Overview

##### Project Type and Purpose
- This is a full-stack, React-based web application built on Next.js (App Router).
- It provides an interface for managing “drills,” “rounds,” and “tests” (likely golf practice data), backed by Supabase (Postgres) for persistence and authentication.

##### High-Level Architecture Components
a. Client Layer (Browser)
- Next.js pages & React components under `app/` and `components/`
- UI modules: drills, rounds, tests, login, protected layout
- Uses Server Components (for data fetching) and Client Components (forms, tables)
b. Next.js Server Layer
- App Router handlers (server components + layouts)
- API routes in `api/get-rounds.ts` and `api/upsert-rounds.ts`
- `middleware.ts` for auth checks on protected routes
c. Business Logic & Data Access
- Supabase client in `lib/supabase/server.ts` for server-side DB calls
- Supabase middleware in `lib/supabase/middleware.ts` for session handling
- Utility functions in `lib/utils.ts`
- Types in `lib/types.ts` and `types/` for strong typing
d. Database
- Postgres managed by Supabase
- Schema files in `z_database-schema/` define tables: drills, drill_shots, etc.
e. Deployment & Infrastructure
- Hosted on Vercel (Next.js platform)
- Static assets under `public/`
- Config files: `next.config.ts`, `postcss.config.mjs`

##### Component Interactions & Data Flow
- User interacts in browser → Next.js App Router serves React UI (SSR / streaming)
- On data fetch: Server Components call Supabase via `server.ts` → Postgres
- For mutations:
- Client Components invoke React Server Actions or call Next.js API routes → Supabase
- API routes in `api/` folder read/write via Supabase client
- Authentication:
- Sign-in flow uses Supabase Auth; token persisted in cookies by `middleware.ts`
- Protected pages under `app/(protected)/` check session in middleware

##### Architectural Patterns & Design Principles
- Separation of concerns: UI components vs. data-access layers vs. API handlers
- Next.js App Router conventions (layouts, server/client components)
- Modular feature folders: drills, rounds, tests each have forms, tables, routes
- Type safety: TypeScript, shared types between front/end and back/end
- Reusable UI primitives under `components/ui/` (buttons, cards, tables)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Browser (Client)" 
        direction TB
        subgraph "Drills UI" 
            direction TB
            DrillsList["Drills Page"]:::ui
            DrillsType["Drill Type Page"]:::ui
            DrillsNewType["New Drill Type Page"]:::ui
            DrillsNewDrill["New Drill Instance Page"]:::ui
        end
        subgraph "Rounds UI" 
            direction TB
            RoundsList["Rounds Page"]:::ui
            RoundsNew["New Round Page"]:::ui
            RoundBuilder["Round Builder Page"]:::ui
        end
        subgraph "Tests UI" 
            direction TB
            TestsList["Tests Page"]:::ui
            TestsType["Test Type Page"]:::ui
            TestsNew["New Test Page"]:::ui
        end
        LoginPage["Login Page"]:::ui
    end

    %% Next.js Server Layer
    subgraph "Next.js Server" 
        direction TB
        subgraph "App Router" 
            direction TB
            AR_Core["Core Layout & Page"]:::server
            ProtectedLayout["Protected Layout"]:::server
        end
        Middleware["Middleware"]:::server
        subgraph "API Routes" 
            direction TB
            API_Get["GET Rounds"]:::serverless
            API_Upsert["UPSERT Rounds"]:::serverless
        end
        subgraph "Lib Layer" 
            direction TB
            SB_Server["Supabase Client"]:::backend
            SB_Middleware["Supabase Middleware"]:::backend
            Utils["Utilities"]:::backend
            Types["Shared Types"]:::backend
        end
    end

    %% External Services
    SupabaseDB["Supabase Postgres"]:::external
    SupabaseAuth["Supabase Auth Service"]:::external
    Vercel["Vercel Hosting/CDN"]:::external
    Config["Static Assets & Config"]:::config

    %% Connections
    DrillsList -->|"page request"| AR_Core
    DrillsType -->|"page request"| AR_Core
    DrillsNewType -->|"page request"| AR_Core
    DrillsNewDrill -->|"page request"| AR_Core

    RoundsList -->|"page request"| AR_Core
    RoundsNew -->|"page request"| AR_Core
    RoundBuilder -->|"page request"| AR_Core

    TestsList -->|"page request"| AR_Core
    TestsType -->|"page request"| AR_Core
    TestsNew -->|"page request"| AR_Core

    LoginPage -->|"page request"| AR_Core

    AR_Core -->|"auth check"| Middleware
    ProtectedLayout -->|"auth check"| Middleware

    AR_Core -->|"Server Component fetch"| SB_Server
    API_Get -->|"fetch rounds"| SB_Server
    API_Upsert -->|"upsert round"| SB_Server

    Middleware -->|"validate session"| SB_Middleware

    SB_Server -->|"SQL queries"| SupabaseDB
    SB_Middleware -->|"session validate"| SupabaseAuth
    SupabaseAuth -->|"token via cookie"| Middleware

    DrillsList -->|"static assets"| Config
    RoundsList -->|"static assets"| Config
    TestsList -->|"static assets"| Config

    AR_Core --> Vercel
    API_Get --> Vercel
    API_Upsert --> Vercel

    %% Click Events - Client Layer
    click DrillsList "https://github.com/samjamead/better-faster/blob/main/app/(protected)/drills/page.tsx"
    click DrillsType "https://github.com/samjamead/better-faster/blob/main/app/(protected)/drills/[type]/page.tsx"
    click DrillsNewType "https://github.com/samjamead/better-faster/blob/main/app/(protected)/drills/[type]/new/page.tsx"
    click DrillsNewDrill "https://github.com/samjamead/better-faster/blob/main/app/(protected)/drills/[type]/new/[drill]/page.tsx"

    click RoundsList "https://github.com/samjamead/better-faster/blob/main/app/(protected)/rounds/page.tsx"
    click RoundsNew "https://github.com/samjamead/better-faster/blob/main/app/(protected)/rounds/new/page.tsx"
    click RoundBuilder "https://github.com/samjamead/better-faster/blob/main/app/(protected)/round-builder/page.tsx"
    click components/rounds/rounds-table.tsx "https://github.com/samjamead/better-faster/blob/main/components/rounds/rounds-table.tsx"
    click components/rounds/round-form.tsx "https://github.com/samjamead/better-faster/blob/main/components/rounds/round-form.tsx"
    click components/rounds/rounds-table-columns.tsx "https://github.com/samjamead/better-faster/blob/main/components/rounds/rounds-table-columns.tsx"

    click TestsList "https://github.com/samjamead/better-faster/blob/main/app/(protected)/tests/page.tsx"
    click TestsType "https://github.com/samjamead/better-faster/blob/main/app/(protected)/tests/[type]/page.tsx"
    click TestsNew "https://github.com/samjamead/better-faster/blob/main/app/(protected)/tests/[type]/new/page.tsx"
    click components/tests/tests-table.tsx "https://github.com/samjamead/better-faster/blob/main/components/tests/tests-table.tsx"
    click components/tests/tests-table-columns.tsx "https://github.com/samjamead/better-faster/blob/main/components/tests/tests-table-columns.tsx"
    click components/tests/putting-test-form.tsx "https://github.com/samjamead/better-faster/blob/main/components/tests/putting-test-form.tsx"
    click components/tests/wedge-test-form.tsx "https://github.com/samjamead/better-faster/blob/main/components/tests/wedge-test-form.tsx"
    click components/tests/actions.ts "https://github.com/samjamead/better-faster/blob/main/components/tests/actions.ts"
    click components/tests/log-test-button.tsx "https://github.com/samjamead/better-faster/blob/main/components/tests/log-test-button.tsx"

    click LoginPage "https://github.com/samjamead/better-faster/blob/main/app/login/page.tsx"
    click components/sidebar/auth-buttons.tsx "https://github.com/samjamead/better-faster/blob/main/components/sidebar/auth-buttons.tsx"
    click middleware "https://github.com/samjamead/better-faster/blob/main/middleware.ts"

    %% Click Events - Server Layer
    click AR_Core "https://github.com/samjamead/better-faster/blob/main/app/layout.tsx"
    click ProtectedLayout "https://github.com/samjamead/better-faster/blob/main/app/(protected)/layout.tsx"
    click API_Get "https://github.com/samjamead/better-faster/blob/main/api/get-rounds.ts"
    click API_Upsert "https://github.com/samjamead/better-faster/blob/main/api/upsert-rounds.ts"
    click SB_Server "https://github.com/samjamead/better-faster/blob/main/lib/supabase/server.ts"
    click SB_Middleware "https://github.com/samjamead/better-faster/blob/main/lib/supabase/middleware.ts"
    click Utils "https://github.com/samjamead/better-faster/blob/main/lib/utils.ts"
    click Types "https://github.com/samjamead/better-faster/blob/main/lib/types.ts"
    click Types "https://github.com/samjamead/better-faster/blob/main/types/tanstack-table.d.ts"

    %% Click Events - External & Config
    click Config "https://github.com/samjamead/better-faster/tree/main/public/"
    click Config "https://github.com/samjamead/better-faster/blob/main/next.config.ts"
    click Config "https://github.com/samjamead/better-faster/blob/main/postcss.config.mjs"
    click Config "https://github.com/samjamead/better-faster/blob/main/tsconfig.json"
    click SupabaseDB "https://github.com/samjamead/better-faster/blob/main/z_database-schema/drills.sql"
    click SupabaseDB "https://github.com/samjamead/better-faster/blob/main/z_database-schema/drill_shots.sql"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085;
    classDef server fill:#d4edda,stroke:#155724,color:#155724;
    classDef serverless fill:#d1ecf1,stroke:#0c5460,color:#0c5460;
    classDef backend fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef external fill:#fff3cd,stroke:#856404,color:#856404;
    classDef config fill:#f8d7da,stroke:#721c24,color:#721c24;
```
