How nhamonin/widget-dashboard works
1. Monorepo shape
- This is a pnpm workspace with three main codebases:
- packages/shared: the contract layer
- apps/server: the Fastify API and persistence layer
- apps/web: the React SPA
2. Single source of truth for the data model
- packages/shared holds the Zod schemas and inferred types for widgets and HTTP payloads.
- Both server and web import these shared definitions, so validation, runtime parsing, and TypeScript types stay aligned.
- The same schemas also feed API docs generation on the backend.
3. Backend request path
- The server is a Fastify 5 app in apps/server/src/server.ts.
- Requests enter through route modules in src/routes:
- /health for infrastructure checks
- /api/widgets for the widget resource
- Route handlers delegate to services, which delegate to repositories, which talk to SQLite.
4. Backend layering and responsibilities
- services/widget.service.ts contains business rules such as widget creation, update rules, and text-edit restrictions.
- repositories/widget.repo.ts isolates persistence queries and record mapping.
- lib/chart-data.ts is the server-side generator for random chart points; the data is created once and then stored, so reloads remain stable.
5. Persistence boundary
- The backend uses Node’s built-in node:sqlite with a persistent database file.
- The README indicates WAL mode and a single-writer deployment model.
- Widget ordering is insertion-based; deletions leave gaps rather than renumbering everything, which matters for the dashboard layout.
6. Backend cross-cutting concerns
- Custom Fastify plugins handle JSON body parsing, centralized error formatting, OpenAPI exposure, and per-IP rate limiting.
- Errors are normalized to { error: { message, code } }.
- The API is limited to 100 requests per minute per IP, which is an architectural boundary the frontend must tolerate.
7. Frontend application shape
- The SPA lives in apps/web and is built with React 18 + Vite.
- main.tsx bootstraps the app, and providers/AppProviders.tsx likely wires shared client concerns such as TanStack Query and notifications.
- The app is a client-side dashboard, not a server-rendered page app.
8. Frontend data flow
- src/lib/api-client.ts centralizes browser calls to the backend API.
- src/hooks/widgets.ts uses TanStack Query to fetch widgets and run create/update/delete mutations.
- This gives each widget or action its own loading/error state rather than one global screen state.
9. Dashboard and widget rendering
- components/dashboard owns the board layout: empty state, grid, widget cards, skeletons, and the add-widget control.
- components/widgets renders type-specific content:
- charts via Recharts for line and bar
- inline-editable text for text
- The UI enforces the product rules described in the README, including save confirmation, delete confirmation, and the 1,000-character cap for text.
10. Shared UI and presentation layer
- The app uses shadcn/ui-style components under components/ui for dialogs, buttons, cards, menus, skeletons, and toasts.
- src/lib/widget-meta.ts and src/lib/skeleton-count.ts support view logic such as widget-specific labels and responsive loading placeholders.
- Styling is handled through index.css plus component-level composition, with the dashboard designed for a three-column responsive grid.
11. Routing and deployment boundary
- The browser always calls /api/* on the same origin.
- In production, Vercel serves the SPA and rewrites /api to the Fly.io backend.
- Locally, docker-compose.yml plus Caddy provide the same single-origin shape by serving the SPA and proxying /api to Fastify.
- This avoids CORS entirely and keeps the frontend deployment independent from the API deployment.
12. Operational and test boundaries
- The backend is containerized with its own Dockerfile and entrypoint, and Fly.io mounts a persistent volume for SQLite.
- The frontend also has its own Dockerfile and Vercel config.
- Tests are split by layer: backend node:test, web Vitest/Testing Library, plus CI for lint, typecheck, build, and a backend smoke test.
Main entry points: server.ts Fastify app · main.tsx App bootstrap · widget.service.ts Widget service · pnpm-workspace.yaml pnpm workspace
Components 21
Workspace
pnpm monorepo and shared tooling
- pnpm workspace · Workspace root and shared package management
pnpm-workspace.yaml
Shared Contracts
Schema and type layer used by both apps
- Widget schema · Widget domain schemas and inferred types
packages/shared/src/widget.ts - HTTP schema · Request and response payload schemas
packages/shared/src/http.ts - Shared exports · Public re-exports for the contract layer
packages/shared/src/index.ts
Server
Fastify API and SQLite persistence
- Fastify app · Server bootstrap and application composition
apps/server/src/server.ts - Routes · Health and widget route modules
apps/server/src/routes/widgets.ts - Widget service · Widget rules, validation flow, and mutation policy
apps/server/src/services/widget.service.ts - Widget repo · SQLite queries and record mapping
apps/server/src/repositories/widget.repo.ts - SQLite DB · Persistent SQLite boundary used by the API
apps/server/src/db/index.ts - Chart data · Stable random chart-point generation stored once
apps/server/src/lib/chart-data.ts - Fastify plugins · JSON parsing, error formatting, OpenAPI, and rate limiting
apps/server/src/plugins/openapi.ts
Web
React dashboard SPA
- App bootstrap · Client entrypoint and provider wiring
apps/web/src/main.tsx - App providers · Shared client concerns such as query and toast providers
apps/web/src/providers/AppProviders.tsx - API client · Central browser access to the backend API
apps/web/src/lib/api-client.ts - Widgets hook · TanStack Query fetches and mutations for widgets
apps/web/src/hooks/widgets.ts - Dashboard · Board layout, empty state, widget cards, skeletons, and add control
apps/web/src/components/dashboard/DashboardGrid.tsx - Widget views · Type-specific chart and text widget rendering
apps/web/src/components/widgets/ChartWidget.tsx - Widget meta · Widget labels and presentation helpers
apps/web/src/lib/widget-meta.ts
Deployment
Local and production routing, containers, and persistent storage
- Same-origin API · Browser traffic goes to /api on the same origin
Caddyfile - Local proxy · Caddy and docker compose provide the SPA plus API proxy locally
docker-compose.yml - Prod deploy · Vercel serves the SPA and Fly.io runs the API with persistent storage
fly.toml
Connections 27
- pnpm workspace → Shared exports: workspace contains · Monorepo includes the shared contracts package
- pnpm workspace → Fastify app: workspace contains · Monorepo includes the backend app
- pnpm workspace → App bootstrap: workspace contains · Monorepo includes the frontend app
- Shared exports → Widget schema: re-exports · Shared package exposes widget schema and types
- Shared exports → HTTP schema: re-exports · Shared package exposes HTTP payload schemas
- Fastify app → Fastify plugins: uses · App installs cross-cutting Fastify plugins
- Fastify app → Routes: mounts · App composes route modules
- Routes → Widget service: delegates · Route handlers forward widget operations to services
- Widget service → Widget repo: persists via · Business rules use repository methods for storage
- Widget repo → SQLite DB: queries · Repository talks to SQLite database
- Widget service → Chart data: creates · Service uses stable chart data generation
- Fastify app → HTTP schema: validates with · Server uses shared payload schemas
- Widget service → Widget schema: validates with · Service enforces widget contract rules
- App bootstrap → App providers: wraps with · SPA bootstraps shared client providers
- App bootstrap → Dashboard: renders · Root app composes the dashboard experience
- Widgets hook → API client: calls · Query layer centralizes browser requests
- Dashboard → Widgets hook: reads · Dashboard state comes from widget queries and mutations
- Widget views → Widget meta: uses · Widget views rely on presentation metadata
- Dashboard → Widget views: renders · Dashboard shows chart and text widgets
- API client → HTTP schema: shares contract · Browser client follows the same request/response schemas
- Widget views → Widget schema: shares contract · Widget rendering follows shared widget types
- Same-origin API → Local proxy: supports · Local proxy shape matches same-origin API access
- Same-origin API → Prod deploy: supports · Production routing preserves same-origin API access
- Local proxy → Fastify app: proxies to · Caddy forwards API calls to the backend
- Local proxy → App bootstrap: serves · Local environment serves the SPA
- Prod deploy → SQLite DB: persists on · Fly deployment keeps SQLite data on a mounted volume
- Prod deploy → App bootstrap: hosts · Vercel hosts the frontend application