Framework
A sales lead opening the dashboard might want overdue quotes ahead of every other metric. Put that view in a module widget, give it a permission and load only the data the current user is allowed to see.
The admin dashboard at /backend is built from small, self-contained widgets that are discovered from every enabled module. This document explains how widgets are authored, discovered, and controlled inside Open Mercato.
Widgets live under packages/<module>/src/modules/<module>/widgets/dashboard/<slug>/widget.ts. The generator looks for files named widget.ts, widget.js, widget.tsx, or widget.jsx and registers them automatically. A widget module must export a default object implementing DashboardWidgetModule from @open-mercato/shared/modules/dashboard/widgets.
Core ships with the discovery/runtime infrastructure, while the Example module (packages/example) provides a set of reference widgets that demonstrate common patterns:
| Widget ID | Path | Purpose |
|---|---|---|
example.dashboard.welcome | .../welcome/widget.ts | Greets the current user with a configurable headline and message. |
example.dashboard.notes | .../notes/widget.ts | Allows a user to maintain private text notes. |
example.dashboard.todos | .../todos/widget.ts | Surfaces Example module todos and allows creating/completing tasks via the public API. |
Each widget module exports:
const widget: DashboardWidgetModule<MySettings> = {
metadata: { /* id, title, features, defaults, etc. */ },
Widget: MyClientComponent,
hydrateSettings: (raw) => parsedSettings,
dehydrateSettings: (settings) => serialisableSettings,
}
Dashboard widgets run on the client, but the discovery phase happens in node. Keep client React code in widget.client.tsx and export plain configuration helpers from a config.ts file. The server-facing widget.ts should lazily reference the client component so CLI discovery never imports next/* modules.
Use lazyDashboardWidget from @open-mercato/shared/modules/dashboard/widgets:
import { lazyDashboardWidget, type DashboardWidgetModule } from '@open-mercato/shared/modules/dashboard/widgets'
import { DEFAULT_SETTINGS } from './config'
const MyWidgetClient = lazyDashboardWidget(() => import('./widget.client'))
const widget: DashboardWidgetModule = {
metadata: { /* ... */, defaultSettings: DEFAULT_SETTINGS },
Widget: MyWidgetClient,
}
The dynamic import() is load-bearing, not a style preference. When the CLI bundles a module's
sources it replaces local *.client dynamic imports with an inert stub, which is what keeps
widget.ts importable in Node while the browser-only subgraph (charts, editors, anything reaching
next/dynamic) stays out of the CLI bundle. A static import MyWidgetClient from './widget.client'
in widget.ts is not stubbed: its transitive browser imports are hoisted into the CLI bundle and
every CLI entry point – yarn dev included – fails to boot.
Only dynamic imports are rewritten, so a server-side helper that merely follows a *.client.ts
naming convention keeps working when it is imported statically.
The generator (mercato generate registry via packages/cli/src/lib/generators/module-registry.ts) records every widget as { moduleId, key, loader }. At runtime:
packages/core/src/modules/dashboards/lib/widgets.ts lazy-loads the registry, requests each widget via loader(), validates its metadata, and caches the result.loadAllWidgets() returns a deduplicated list (one entry per widget metadata.id), while loadWidgetById() fetches a single widget lazily.dashboards/api/layout/route.ts) and widget visibility APIs use the discovery helpers to present the available widgets to the UI and to enforce feature flags/upgrades.The UI dashboard (packages/ui/src/backend/dashboard/DashboardScreen.tsx) consumes the available widgets, renders the configured layout, and obtains per-widget components by calling the loaders exposed from the module registry.
Widget availability is gated by existing RBAC rules:
features array. The discovery helpers ensure that a user either has all listed features or is a super admin before the widget is offered.DashboardRoleWidgets) and per user (DashboardUserWidgets). The CLI command mercato dashboards seed-defaults writes default visibility for the standard roles after mercato init.packages/example/src/modules/example/acl.ts (example.widgets.*). Grant these via role ACLs to allow access.packages/<my-module>/src/modules/<my-module>/widgets/dashboard/<slug>/widget.ts.DashboardWidgetModule, including metadata.id (must be globally unique), title, and features.hydrateSettings/dehydrateSettings to keep the persisted JSON payload stable.@open-mercato/ui/backend/utils/apiCall (e.g., apiCall or readApiResultOrThrow) so authentication headers are applied consistently.yarn generate so the registry picks up your new widget.acl.ts. That makes it easy to grant/revoke widgets without editing layout data manually.With these conventions any module can safely plug additional functionality into the admin landing page without touching shared code.
Previous: Custom entities (EAV) · Next: Concurrency & Record Locking