MOCA is a Next.js (App Router) application for on-demand mobility coordination, designed for "福富町" style use-cases. It features a Japanese-localized UI for both passengers and operators, a built-in HTTP API, and geospatial routing powered by Supabase and OSRM.
This README is your technical guide—covering project structure, setup, API, integrations, and design notes to help you get productive fast.
- Overview
- Quick Start
- Environment Variables
- Architecture
- Key Files & Directories
- API Design
- Data Flow & Integrations
- Types & Validation
- UI Structure
- Migrations & Database
- Testing & Linting
- Deployment
- Troubleshooting
- Contributing
- Framework: Next.js (App Router), React 19, TypeScript
- UI:
- Passenger booking (
app/page.tsx) - Operator dashboard (
app/operator)
- Passenger booking (
- API: Next.js route handlers (
app/api/*) using Supabase - Geospatial: OSRM integration with fallback dummy routing
-
Install dependencies (choose one):
npm install # or pnpm install # or yarn
-
Configure environment: Create
.env.local(see Environment Variables), then run:npm run dev # or pnpm dev -
Scripts: Available in
package.json:dev— Start dev serverbuild— Production buildstart— Run built apptypes— Type-check
Create .env.local (do not commit):
NEXT_PUBLIC_SUPABASE_URL=https://your-project-id.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key-hereMissing variables will cause errors in lib/supabase.ts.
- Frontend: React pages/components call Next.js API routes (
app/api/*) - Backend: API routes use Supabase client (
lib/supabase.ts) - Routing:
lib/osrm.tshandles OSRM HTTP API calls, with fallback to dummy generator - Types: Defined in
/types/*, validated with Zod - UI: Components in
/componentsuse strict typing
- App Entrypoints:
app/page.tsx— Passenger bookingapp/operator/page.tsx— Operator dashboardapp/layout.tsx— Global layout, font, footer
- API Handlers:
app/api/points/route.ts— List/create pointsapp/api/points/[id]/route.ts— Single point CRUDapp/api/routes/route.ts— Routes list/createapp/api/reservations/*,app/api/vehicles/*,app/api/demand-predictions/*— More endpoints
- Lib:
lib/supabase.ts— Supabase clientlib/api-helpers.ts— Response helpers, Zod formatting, error translationlib/osrm.ts— OSRM integration & fallback
- Types:
types/— API/component types, Zod schemas
- Components:
components/Booking.tsx,FukutomiMap.tsx,RouteList.tsx,PointList.tsx, etc.
- Docs:
docs/API.md— API details & examplesdocs/migrations/— SQL migrations
All API routes return JSON in a consistent format:
- Success:
{ "success": true, "data": { ... }, "message": "optional" } - Error:
{ "success": false, "error": "message", "details": [{ "field": "name", "message": "..." }] }
See docs/API.md for full endpoint specs.
- Supabase:
Typed client in
lib/supabase.ts. Errors mapped to user-friendly messages viahandleSupabaseErrorinlib/api-helpers.ts. - OSRM:
generateRouteWithOSRM(points, startTime)— Real routinggenerateDummyRouteData(points, startTime)— FallbackgetOrGenerateRouteData(...)— Tries OSRM, falls back if needed
OSRM is called via the public router.project-osrm.org URL. For production, consider hosting your own OSRM instance or using a paid routing API for stability.
- Zod schemas for all API payloads (see
types/api.points.types.ts) - Helpers in
lib/api-helpers.ts:formatZodErrors()— Normalize validation errorsvalidateBody(request, schema)— Safe JSON + schema checkparseQueryParams(request)— Typed query parsinghandleSupabaseError(error)— DB error translation
- Type hints for Supabase DB in
types/supabase.types.ts
- Map:
components/FukutomiMap.tsx(Leaflet, client-only) - Booking:
components/Booking.tsx(map + route list + reservation) - Operator: CRUD for points/routes, live map updates
- Primitives:
Header.tsx,Sidebar.tsx,Card.tsx,Dialog.tsx, etc.
The operator page (app/operator/page.tsx) demonstrates the full CRUD flow for points, including live map updates.
- SQL migrations in
docs/migrations/ - Schema changes: update migrations and Zod types together
- Use Supabase panel or migration runner to apply SQL
- TypeScript strict mode
- Type-check:
npm run types