How jeangq24/mototrack works
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: page.tsx Dashboard · route.ts API Routes · useVehicles.ts Data Hooks · client.ts Supabase Clients
Components 20
Browser / PWA
User-facing app, installable shell, and offline capture layer.
- Vehicle UI · Manages vehicle profiles and mileage-oriented interactions.
app/components/VehicleManager.tsx - Service UI · Displays upcoming services and service history.
app/components/UpcomingServices.tsx - Expense UI · Captures and reviews operating expenses.
app/components/ExpenseForm.tsx - History View · Aggregates historical activity into a single view.
app/components/HistoryView.tsx - Pockets UI · Manages goal-based allocations and budget pockets.
app/components/PocketsManager.tsx - Stats Bar · Summarizes key dashboard metrics.
app/components/StatsBar.tsx - Offline Sync · Coordinates queued writes and replay when connectivity returns.
app/components/OfflineSyncProvider.tsx - PWA Shell · Installable app manifest and service worker support.
public/manifest.json - Service Worker · Caches assets and supports offline interaction.
public/sw.js
Next.js App
Pages, route handlers, feature components, hooks, and shared app logic.
- Dashboard · Authenticated home screen and main entry point.
app/page.tsx - Login · Sign-in entry point for email/OTP auth.
app/login/page.tsx - Auth Callback · Handles Supabase auth return and session completion.
app/auth/callback/route.ts - Sync Manager · Offline queueing and synchronization orchestration.
app/utils/syncManager.ts - Data Hooks · Fetch and mutate domain data for vehicles, services, expenses, pockets, and maintenance.
app/hooks/useVehicles.ts - API Routes · Next.js route handlers for domain CRUD and maintenance actions.
app/api/vehicles/route.ts - Maintenance Logic · Computes maintenance status and rule-based thresholds.
lib/maintenance.ts - Supabase Clients · Browser, server, and middleware Supabase adapters.
utils/supabase/client.ts - Auth Middleware · Protects authenticated routes and keeps sessions in sync.
middleware.ts
Supabase Backend
Authentication, database, and row-level security.
- Postgres Schema · Supabase/PostgreSQL schema and production initialization.
supabase_init_production.sql - Row-Level Security · Database-level user isolation and authorization boundary.
supabase_init_production.sql
Connections 26
- 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.