# jeangq24/mototrack architecture

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

- Interactive diagram: https://gitdiagram.com/jeangq24/mototrack
- Repository: https://github.com/jeangq24/mototrack

## Overview

1. **Overall shape**
   MotoTrack is a **Next.js 15 App Router** application with a thin server/API layer and a client-heavy dashboard UI. It is built around a **Supabase/PostgreSQL backend** for persistence, authentication, and row-level security, with a **PWA/offline-first layer** for local capture and later sync.

2. **Primary user-facing surfaces**
   The main screens are:
   - `app/page.tsx`: the authenticated home/dashboard
   - `app/login/page.tsx`: sign-in entry point
   - `app/auth/callback/route.ts`: auth return handler
   The UI is organized into feature components such as `VehicleManager`, `PocketsManager`, `ExpenseForm`, `ExpenseHistory`, `UpcomingServices`, `HistoryView`, `StatsBar`, and `OfflineSyncProvider`. These indicate a dashboard composed of self-contained panels rather than a monolithic page.

3. **Domain areas / bounded contexts**
   The repo is split into clear business modules:
   - **Vehicles**: vehicle profiles and mileage-related tracking
   - **Services**: categorized rides/jobs/services and upcoming scheduled work
   - **Expenses**: operating costs like fuel and food
   - **Maintenance rules/logs**: mileage-based maintenance thresholds and event logging
   - **Pockets**: goal-based allocations/budgets that reduce available balance
   These are exposed both in the UI and via corresponding API routes, which helps keep each concern isolated.

4. **Server/API boundary**
   The `app/api/**/route.ts` files define REST-like handlers for each domain:
   - `/api/vehicles`
   - `/api/services`
   - `/api/expenses`
   - `/api/pockets`
   - `/api/maintenance-rules`
   - nested maintenance endpoints such as `/api/vehicles/[id]/maintenance` and `/api/maintenance-rules/[id]/log`
   This indicates the app uses Next.js route handlers as the server boundary, likely mediating access to Supabase and centralizing validation/business rules before data is persisted.

5. **Data access and state orchestration**
   The `app/hooks/useVehicles.ts`, `useServices.ts`, `useExpenses.ts`, `usePockets.ts`, and `useMaintenance.ts` hooks are the client-side data orchestration layer. They likely encapsulate fetching, caching, mutation, and refresh logic for the UI components, keeping the components themselves focused on rendering and user interaction.

6. **Business logic layer**
   The `lib/maintenance.ts` file is the clearest sign of a dedicated domain logic layer. It likely computes maintenance status from odometer and rule definitions, producing states such as “Seguro”, “Próximo”, or “Vencido” as described in the README. `app/utils/syncManager.ts` appears to coordinate offline queueing and replay, sitting between the UI and API layer.

7. **Authentication and session management**
   Authentication is Supabase-based and appears to use **email/OTP login**. The support files in `utils/supabase/` split the auth integration by runtime:
   - `client.ts` for browser-side Supabase access
   - `server.ts` for server-side access in route handlers/pages
   - `middleware.ts` for request-time session handling
   The root `middleware.ts` likely protects authenticated routes and keeps the session in sync across server-rendered requests.

8. **Persistence model and security**
   Persistence is backed by Supabase/PostgreSQL, with the schema provisioned by `supabase_init_production.sql`. The README emphasizes **RLS (Row Level Security)**, which means data isolation is enforced at the database layer per user. The architecture therefore relies on both application-level route handlers and database-level authorization.

9. **Offline-first / sync subsystem**
   MotoTrack includes a local-first layer:
   - `public/sw.js` registers the service worker
   - `public/manifest.json` enables installable PWA behavior
   - `app/components/OfflineSyncProvider.tsx` and `app/utils/syncManager.ts` likely coordinate queued writes
   This means user actions can be captured offline, stored temporarily, and replayed to Supabase when connectivity returns. That offline queue is a distinct subsystem bridging the client UI and the remote API.

10. **Mobile-first UI composition**
   The component set and README point to a dashboard optimized for one-handed, high-frequency use. Forms and widgets are broken into small interaction units (`ExpenseForm`, `RegisterForm`, `PocketsWidget`, etc.), while global styling is handled in `app/globals.css`. The architecture is intentionally feature-driven and interaction-centric rather than page-heavy.

11. **Data flow at a high level**
   The main flow is:
   user action in a component → hook or local sync manager → Next.js API route or offline queue → Supabase/PostgreSQL → updated state returned to hooks/components.
   Auth flows through Supabase middleware/callback first, then data access is scoped to the authenticated user via RLS. Offline writes bypass immediate network dependency and are later reconciled through the sync layer.

12. **Operational/runtime boundaries**
   The repo is a standard Node/Next.js application at runtime, with Supabase as the external platform dependency. There is no separate backend service or job worker in the tree; scheduled/derived behavior is computed in the app and persisted via API routes. That makes the diagram simpler: one Next.js frontend/server app, one Supabase backend, plus a browser service worker for offline support.

13. **What to show in an architecture diagram**
   A good diagram should include:
   - the **browser/PWA client**
   - **Next.js app layer** with pages, components, hooks, and route handlers
   - **Supabase auth + Postgres** as the backend system of record
   - **service worker / offline sync queue** as the resilience path
   - the main domain modules: vehicles, services, expenses, maintenance rules, and pockets
   - RLS as the security boundary between the app and the database

Main entry points: [`app/page.tsx`](https://github.com/jeangq24/mototrack/blob/main/app/page.tsx) (Dashboard), [`app/api/vehicles/route.ts`](https://github.com/jeangq24/mototrack/blob/main/app/api/vehicles/route.ts) (API Routes), [`app/hooks/useVehicles.ts`](https://github.com/jeangq24/mototrack/blob/main/app/hooks/useVehicles.ts) (Data Hooks), [`utils/supabase/client.ts`](https://github.com/jeangq24/mototrack/blob/main/utils/supabase/client.ts) (Supabase Clients)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser / PWA"]
  node_vehicles_ui["Vehicle UI<br/>feature component<br/>[VehicleManager.tsx]"]
  node_services_ui["Service UI<br/>feature component"]
  node_expenses_ui["Expense UI<br/>feature component<br/>[ExpenseForm.tsx]"]
  node_history_ui["History View<br/>feature component<br/>[HistoryView.tsx]"]
  node_pockets_ui["Pockets UI<br/>feature component<br/>[PocketsManager.tsx]"]
  node_stats_ui["Stats Bar<br/>feature component<br/>[StatsBar.tsx]"]
  node_offline_provider["Offline Sync<br/>provider"]
  node_pwa_shell["PWA Shell<br/>client runtime<br/>[manifest.json]"]
  node_service_worker["Service Worker<br/>offline runtime<br/>[sw.js]"]
end

subgraph group_app["Next.js App"]
  node_page_home["Dashboard<br/>app page<br/>[page.tsx]"]
  node_page_login["Login<br/>auth page<br/>[page.tsx]"]
  node_auth_callback["Auth Callback<br/>route handler<br/>[route.ts]"]
  node_sync_manager["Sync Manager<br/>client sync<br/>[syncManager.ts]"]
  node_use_hooks["Data Hooks<br/>client hooks<br/>[useVehicles.ts]"]
  node_api_routes["API Routes<br/>server boundary<br/>[route.ts]"]
  node_maintenance_logic["Maintenance Logic<br/>domain logic<br/>[maintenance.ts]"]
  node_supabase_clients["Supabase Clients<br/>integration layer<br/>[client.ts]"]
  node_middleware["Auth Middleware<br/>request middleware<br/>[middleware.ts]"]
end

subgraph group_backend["Supabase Backend"]
  node_db_schema[("Postgres Schema<br/>database schema")]
  node_rls{{"Row-Level Security<br/>data security"}}
end

node_page_home -->|"renders"| node_vehicles_ui
node_page_home -->|"renders"| node_services_ui
node_page_home -->|"renders"| node_expenses_ui
node_page_home -->|"renders"| node_pockets_ui
node_page_home -->|"renders"| node_stats_ui
node_page_home -->|"renders"| node_history_ui
node_page_login -->|"returns to"| node_auth_callback
node_auth_callback -->|"uses"| node_supabase_clients
node_use_hooks -->|"fetches"| node_api_routes
node_vehicles_ui -->|"uses"| node_use_hooks
node_services_ui -->|"uses"| node_use_hooks
node_expenses_ui -->|"uses"| node_use_hooks
node_pockets_ui -->|"uses"| node_use_hooks
node_offline_provider -->|"coordinates"| node_sync_manager
node_sync_manager -->|"replays"| node_api_routes
node_api_routes -->|"calls"| node_maintenance_logic
node_api_routes -->|"accesses"| node_supabase_clients
node_middleware -->|"shares session"| node_supabase_clients
node_supabase_clients -->|"writes/reads"| node_db_schema
node_db_schema -->|"enforced by"| node_rls
node_pwa_shell -->|"enables"| node_service_worker
node_offline_provider -->|"supports"| node_service_worker
node_page_home -->|"wraps with"| node_offline_provider
node_page_home -->|"authenticates via"| node_supabase_clients
node_api_routes -->|"persists to"| node_db_schema
node_maintenance_logic -->|"reads rules"| node_db_schema

click node_page_home "https://github.com/jeangq24/mototrack/blob/main/app/page.tsx"
click node_page_login "https://github.com/jeangq24/mototrack/blob/main/app/login/page.tsx"
click node_auth_callback "https://github.com/jeangq24/mototrack/blob/main/app/auth/callback/route.ts"
click node_vehicles_ui "https://github.com/jeangq24/mototrack/blob/main/app/components/VehicleManager.tsx"
click node_services_ui "https://github.com/jeangq24/mototrack/blob/main/app/components/UpcomingServices.tsx"
click node_expenses_ui "https://github.com/jeangq24/mototrack/blob/main/app/components/ExpenseForm.tsx"
click node_history_ui "https://github.com/jeangq24/mototrack/blob/main/app/components/HistoryView.tsx"
click node_pockets_ui "https://github.com/jeangq24/mototrack/blob/main/app/components/PocketsManager.tsx"
click node_stats_ui "https://github.com/jeangq24/mototrack/blob/main/app/components/StatsBar.tsx"
click node_offline_provider "https://github.com/jeangq24/mototrack/blob/main/app/components/OfflineSyncProvider.tsx"
click node_sync_manager "https://github.com/jeangq24/mototrack/blob/main/app/utils/syncManager.ts"
click node_use_hooks "https://github.com/jeangq24/mototrack/blob/main/app/hooks/useVehicles.ts"
click node_api_routes "https://github.com/jeangq24/mototrack/blob/main/app/api/vehicles/route.ts"
click node_maintenance_logic "https://github.com/jeangq24/mototrack/blob/main/lib/maintenance.ts"
click node_supabase_clients "https://github.com/jeangq24/mototrack/blob/main/utils/supabase/client.ts"
click node_middleware "https://github.com/jeangq24/mototrack/blob/main/middleware.ts"
click node_db_schema "https://github.com/jeangq24/mototrack/blob/main/supabase_init_production.sql"
click node_rls "https://github.com/jeangq24/mototrack/blob/main/supabase_init_production.sql"
click node_pwa_shell "https://github.com/jeangq24/mototrack/blob/main/public/manifest.json"
click node_service_worker "https://github.com/jeangq24/mototrack/blob/main/public/sw.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_vehicles_ui,node_services_ui,node_expenses_ui,node_history_ui,node_pockets_ui,node_stats_ui,node_offline_provider,node_pwa_shell,node_service_worker toneBlue
class node_page_home,node_page_login,node_auth_callback,node_sync_manager,node_use_hooks,node_api_routes,node_maintenance_logic,node_supabase_clients,node_middleware toneAmber
class node_db_schema,node_rls toneMint
```

## Components

### Browser / PWA

User-facing app, installable shell, and offline capture layer.

- [Vehicle UI](https://github.com/jeangq24/mototrack/blob/main/app/components/VehicleManager.tsx): feature component · Manages vehicle profiles and mileage-oriented interactions. · `app/components/VehicleManager.tsx`
- [Service UI](https://github.com/jeangq24/mototrack/blob/main/app/components/UpcomingServices.tsx): feature component · Displays upcoming services and service history. · `app/components/UpcomingServices.tsx`
- [Expense UI](https://github.com/jeangq24/mototrack/blob/main/app/components/ExpenseForm.tsx): feature component · Captures and reviews operating expenses. · `app/components/ExpenseForm.tsx`
- [History View](https://github.com/jeangq24/mototrack/blob/main/app/components/HistoryView.tsx): feature component · Aggregates historical activity into a single view. · `app/components/HistoryView.tsx`
- [Pockets UI](https://github.com/jeangq24/mototrack/blob/main/app/components/PocketsManager.tsx): feature component · Manages goal-based allocations and budget pockets. · `app/components/PocketsManager.tsx`
- [Stats Bar](https://github.com/jeangq24/mototrack/blob/main/app/components/StatsBar.tsx): feature component · Summarizes key dashboard metrics. · `app/components/StatsBar.tsx`
- [Offline Sync](https://github.com/jeangq24/mototrack/blob/main/app/components/OfflineSyncProvider.tsx): provider · Coordinates queued writes and replay when connectivity returns. · `app/components/OfflineSyncProvider.tsx`
- [PWA Shell](https://github.com/jeangq24/mototrack/blob/main/public/manifest.json): client runtime · Installable app manifest and service worker support. · `public/manifest.json`
- [Service Worker](https://github.com/jeangq24/mototrack/blob/main/public/sw.js): offline runtime · Caches assets and supports offline interaction. · `public/sw.js`

### Next.js App

Pages, route handlers, feature components, hooks, and shared app logic.

- [Dashboard](https://github.com/jeangq24/mototrack/blob/main/app/page.tsx): app page · Authenticated home screen and main entry point. · `app/page.tsx`
- [Login](https://github.com/jeangq24/mototrack/blob/main/app/login/page.tsx): auth page · Sign-in entry point for email/OTP auth. · `app/login/page.tsx`
- [Auth Callback](https://github.com/jeangq24/mototrack/blob/main/app/auth/callback/route.ts): route handler · Handles Supabase auth return and session completion. · `app/auth/callback/route.ts`
- [Sync Manager](https://github.com/jeangq24/mototrack/blob/main/app/utils/syncManager.ts): client sync · Offline queueing and synchronization orchestration. · `app/utils/syncManager.ts`
- [Data Hooks](https://github.com/jeangq24/mototrack/blob/main/app/hooks/useVehicles.ts): client hooks · Fetch and mutate domain data for vehicles, services, expenses, pockets, and maintenance. · `app/hooks/useVehicles.ts`
- [API Routes](https://github.com/jeangq24/mototrack/blob/main/app/api/vehicles/route.ts): server boundary · Next.js route handlers for domain CRUD and maintenance actions. · `app/api/vehicles/route.ts`
- [Maintenance Logic](https://github.com/jeangq24/mototrack/blob/main/lib/maintenance.ts): domain logic · Computes maintenance status and rule-based thresholds. · `lib/maintenance.ts`
- [Supabase Clients](https://github.com/jeangq24/mototrack/blob/main/utils/supabase/client.ts): integration layer · Browser, server, and middleware Supabase adapters. · `utils/supabase/client.ts`
- [Auth Middleware](https://github.com/jeangq24/mototrack/blob/main/middleware.ts): request middleware · Protects authenticated routes and keeps sessions in sync. · `middleware.ts`

### Supabase Backend

Authentication, database, and row-level security.

- [Postgres Schema](https://github.com/jeangq24/mototrack/blob/main/supabase_init_production.sql): database schema · Supabase/PostgreSQL schema and production initialization. · `supabase_init_production.sql`
- [Row-Level Security](https://github.com/jeangq24/mototrack/blob/main/supabase_init_production.sql): data security · Database-level user isolation and authorization boundary. · `supabase_init_production.sql`

## Connections

- Dashboard → Vehicle UI: renders · Dashboard composes the vehicle management surface.
- Dashboard → Service UI: renders · Dashboard composes the service surfaces.
- Dashboard → Expense UI: renders · Dashboard composes expense capture and review.
- Dashboard → Pockets UI: renders · Dashboard includes pocket allocation management.
- Dashboard → Stats Bar: renders · Dashboard shows summary metrics.
- Dashboard → History View: renders · Dashboard includes consolidated history.
- Login → Auth Callback: returns to · Login flow completes through the callback route.
- Auth Callback → Supabase Clients: uses · Callback finalizes auth via Supabase session helpers.
- Data Hooks → API Routes: fetches · Client hooks call the Next.js domain APIs.
- Vehicle UI → Data Hooks: uses · Vehicle UI is driven by client data hooks.
- Service UI → Data Hooks: uses · Service UI is driven by client data hooks.
- Expense UI → Data Hooks: uses · Expense UI is driven by client data hooks.
- Pockets UI → Data Hooks: uses · Pockets UI is driven by client data hooks.
- Offline Sync → Sync Manager: coordinates · Provider delegates offline queue management.
- Sync Manager → API Routes: replays · Queued mutations are sent to the API when online.
- API Routes → Maintenance Logic: calls · API handlers use domain maintenance calculations.
- API Routes → Supabase Clients: accesses · Route handlers access Supabase through server adapters.
- Auth Middleware → Supabase Clients: shares session · Middleware uses Supabase helpers for request-time auth.
- Supabase Clients → Postgres Schema: writes/reads · Adapters mediate database access to Postgres.
- Postgres Schema → Row-Level Security: enforced by · Row-level security constrains all persisted data access.
- PWA Shell → Service Worker: enables · Manifest and worker provide installable offline behavior.
- Offline Sync → Service Worker: supports · Offline provider cooperates with the browser worker.
- Dashboard → Offline Sync: wraps with · Main app surface is wrapped with offline sync support.
- Dashboard → Supabase Clients: authenticates via · Authenticated views depend on Supabase session state.
- API Routes → Postgres Schema: persists to · API handlers persist domain data in Postgres.
- Maintenance Logic → Postgres Schema: reads rules · Maintenance calculations depend on stored vehicle/rule data.
