How rdeeanz/pelindo-mafp works
1. Overall shape
- This is a Next.js 15 App Router application built to run on Cloudflare Pages/Workers with the edge runtime.
- The repo is split cleanly into route handlers/UI in src/app, domain logic in src/lib, presentational components in src/components, and D1 migrations in migrations/.
2. Primary user-facing routes
- The app is organized around a small set of business pages: dashboard, branch list/detail/edit, regional recap, settings, and login.
- Route groups separate authenticated app pages under src/app/(app) from the auth page under src/app/(auth), which is a useful boundary for diagramming access-controlled areas.
3. Authentication and session boundary
- Authentication is JWT-based and stored in an httpOnly cookie.
- The auth subsystem in src/lib/auth includes password hashing, JWT creation/verification, role logic, authorization checks, and session helpers.
- middleware.ts is the likely gatekeeper for protecting app routes and steering unauthenticated users to /login.
4. Data persistence
- The backend database is Cloudflare D1 (SQLite), accessed through src/lib/db.ts.
- Schema changes are managed with SQL migrations:
- 0001_init.sql for base tables
- 0002_seed.sql for initial seed data
- 0003_superadmin_settings.sql for later admin/configuration expansion
- This makes D1 the central system of record for branches, users, facilities, monitoring records, and configuration.
5. Domain model and queries
- The src/lib/queries layer is the main read/write boundary over the database, separated by domain:
- branches.ts
- facilities.ts
- monitoring.ts
- settings.ts
- users.ts
- This suggests a service-style architecture where routes/components call query modules instead of embedding SQL directly.
6. Monitoring workflow
- The core business flow is collecting and maintaining facility availability monitoring for branch ports.
- The edit page for a branch, plus components like MonitoringForm, ObjectRowInput, and FacilityMetaEditor, indicate a structured input flow for facility availability values and metadata.
- src/lib/report/buildReport.ts and recomputeReport.ts show that monitoring changes feed a derived reporting layer, so the system stores raw monitoring data and also computes report views from it.
7. Reporting and analytics views
- The dashboard (/dashboard) presents regional summary cards and branch availability bars.
- The branch detail page (/branches/[branchId]) shows a Lap. Cabang report table, and the recap page (/recap) shows a regional recap table.
- These views rely on shared table/report logic in src/components/table and src/lib/utils for period handling, availability calculations, and formatting.
8. Administration and settings
- The settings area is a separate admin subsystem with subpages for branches, categories, facilities, users, and website settings.
- Each settings domain has both UI panels and API routes under src/app/api/settings/..., which implies CRUD-style management of master data and configuration.
- The presence of SuperadminPanel and role-aware auth logic suggests differentiated access between regional admins and branch PIC users.
9. API surface
- The app uses route handlers in src/app/api as its server boundary:
- /api/auth/login, /logout, /me
- /api/branches
- /api/facilities and /api/facilities/reorder
- /api/settings/...
- /api/monitoring
- In architecture terms, browser UI talks to these handlers for authentication, CRUD actions, and monitoring updates; the handlers in turn talk to D1 and domain logic.
10. Excel export pipeline
- Export is a first-class feature, not an afterthought.
- src/lib/export/generateLapCabangXlsx.ts and generateRekapXlsx.ts produce .xlsx files using xlsx-js-style, and there is a dedicated template file in the repo root.
- Export buttons in src/components/export indicate that users can generate reports directly from the browser, matching an existing business Excel format.
11. Shared UI and layout composition
- The app shell is composed from AppShell, Sidebar, Navbar, ThemeToggle, and reusable UI primitives like Button, Card, Input, Select, Badge, and Toast.
- This means most pages are built by combining a common authenticated layout with page-specific panels/tables rather than each page managing its own chrome.
12. Important cross-cutting utilities
- src/lib/utils contains availability math, date/period parsing, formatter helpers, and settings filters.
- src/lib/constants.ts, src/types, and src/lib/navigation.ts act as shared contracts that reduce duplication across dashboard, tables, forms, and API handlers.
13. Operational/runtime constraints
- The app is designed around Cloudflare-specific deployment: Pages build output, D1 binding, and environment secrets via Wrangler.
- This affects the architecture because server code must be compatible with the edge runtime and uses Cloudflare bindings rather than a traditional Node backend.
14. High-level request/data flow
- A typical flow is: user logs in → JWT cookie is issued → middleware allows access → page loads and calls query/API layers → D1 returns branch/facility/monitoring data → UI renders tables/cards → optionally export generators turn the same data into Excel.
- For an architecture diagram, the key boundaries are browser UI, Next.js route handlers / edge runtime, auth/session layer, query/report/export business logic, and Cloudflare D1 storage.
Main entry points: buildReport.ts Report Engine · db.ts D1 Access · page.tsx Dashboard · route.ts Auth API
Components 24
App Router
Next.js route groups, pages, and API handlers
- Root Layout · Global app wrapper and document shell
src/app/layout.tsx - App Shell · Authenticated application layout boundary
src/app/(app)/layout.tsx - Login · Public sign-in page
src/app/(auth)/login/page.tsx - Dashboard · Regional summary and availability overview
src/app/(app)/dashboard/page.tsx - Branches · Branch list and navigation hub
src/app/(app)/branches/page.tsx - Branch Detail · Per-branch report view
src/app/(app)/branches/[branchId]/page.tsx - Edit Branch · Monitoring and facility editing flow
src/app/(app)/branches/[branchId]/edit/page.tsx - Recap · Regional recap table view
src/app/(app)/recap/page.tsx - Settings · Settings hub for master data and configuration
src/app/(app)/settings/page.tsx - Auth API · Login, logout, and session endpoints
src/app/api/auth/login/route.ts - Branches API · Branch data endpoint
src/app/api/branches/route.ts - Monitoring API · Monitoring update endpoint
src/app/api/monitoring/route.ts - Settings API · CRUD endpoints for admin master data
src/app/api/settings/branches/route.ts
UI Components
Shared layout, form, table, dashboard, and export UI
- App Shell UI · Sidebar, navbar, theme toggle, and page chrome
src/components/layout/AppShell.tsx - Dashboard Cards · Summary and availability presentation
src/components/dashboard/SummaryCard.tsx - Report Panel · Branch report table and period controls
src/components/table/BranchReportPanel.tsx - Monitoring Form · Branch monitoring editor and row inputs
src/components/form/MonitoringForm.tsx - Excel Export · Download buttons for report exports
src/components/export/DownloadExcelButton.tsx
Domain Logic
Auth, queries, reporting, export, and shared utilities
- Auth Session · JWT, password, role, and authorization helpers
src/lib/auth/session.ts - Query Layer · Branch, facility, monitoring, settings, and user queries
src/lib/queries/branches.ts - Report Engine · Report building and recomputation logic
src/lib/report/buildReport.ts - XLSX Export · Excel generation for Lap. Cabang and Rekap
src/lib/export/generateLapCabangXlsx.ts - D1 Access · Cloudflare D1 connection boundary
src/lib/db.ts
Data Layer
Cloudflare D1 schema and migrations
- Init Schema · Base tables and schema foundation
migrations/0001_init.sql
Connections 23
- Root Layout → App Shell: wraps · Root layout composes the authenticated app shell and public routes
- Root Layout → Login: serves · Public auth page sits under the root app layout
- App Shell → App Shell UI: uses · Authenticated route group renders the shared shell
- Login → Auth API: posts to · Login form authenticates through route handlers
- Auth API → Auth Session: uses · Auth endpoints issue and verify sessions
- Auth API → D1 Access: reads · Login and session handlers consult stored users
- Dashboard → Dashboard Cards: renders · Dashboard page is built from summary and availability widgets
- Dashboard → Branches API: loads · Dashboard consumes branch data through server endpoints
- Branches → Branches API: loads · Branch list page reads branch records
- Branch Detail → Report Panel: renders · Detail page centers on branch report presentation
- Branch Detail → Report Engine: computes · Report view relies on derived report building
- Edit Branch → Monitoring Form: renders · Edit page uses the monitoring input workflow
- Edit Branch → Monitoring API: writes to · Monitoring form submits updates through the API
- Recap → Report Engine: reads · Recap page consumes derived reporting data
- Settings → Settings API: manages · Settings pages use CRUD endpoints for master data
- Dashboard → Excel Export: exports · Dashboard and reporting views offer Excel download actions
- Recap → Excel Export: exports · Recap page can generate spreadsheet output
- Excel Export → XLSX Export: calls · UI export actions invoke XLSX generators
- Query Layer → D1 Access: queries · Domain queries are the main database access layer
- Report Engine → Query Layer: uses · Report computation reads domain data from queries
- XLSX Export → Report Engine: formats · Export generation reuses report data and calculations
- Auth Session → D1 Access: checks · Session and authorization logic depend on persisted users and roles
- D1 Access → Init Schema: defined by · D1 schema originates from migrations