Open MercatoDocsSearch docsAPI reference

Framework

Dashboard widgets

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.

Widget locations

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 IDPathPurpose
example.dashboard.welcome.../welcome/widget.tsGreets the current user with a configurable headline and message.
example.dashboard.notes.../notes/widget.tsAllows a user to maintain private text notes.
example.dashboard.todos.../todos/widget.tsSurfaces 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,
}

Client/server split

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.

Discovery and runtime

The generator (mercato generate registry via packages/cli/src/lib/generators/module-registry.ts) records every widget as { moduleId, key, loader }. At runtime:

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.

Access control

Widget availability is gated by existing RBAC rules:

Writing your own widget

  1. Create packages/<my-module>/src/modules/<my-module>/widgets/dashboard/<slug>/widget.ts.
  2. Export a DashboardWidgetModule, including metadata.id (must be globally unique), title, and features.
  3. Provide hydrateSettings/dehydrateSettings to keep the persisted JSON payload stable.
  4. If the widget performs network traffic, use the helpers from @open-mercato/ui/backend/utils/apiCall (e.g., apiCall or readApiResultOrThrow) so authentication headers are applied consistently.
  5. Run yarn generate so the registry picks up your new widget.

Best practices

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