Skip to content

View Engine (@eerp/core-front)

The engine is the reusable, metadata-driven, server-rendered view machinery shared by the Next host and every business module. It is the package @eerp/core-front under core-front/packages/core-front/. Nothing in it is module-specific.

The contract it offers is deliberately small: a module contributes descriptors only. From a descriptor the engine derives the server loader, the Zustand store, and the renderer. If a module ever needs a custom component, that is an engine gap to fix in @eerp/core-front — not a per-module escape hatch. Keeping it that way is what makes "add an entity = add a descriptor" true.

flowchart LR
    D[ViewDescriptor] --> L[Server loader<br/>RSC + ApiClient]
    L -->|initialData| R[Client renderer]
    R --> S[Zustand store<br/>seeded, no fetch-on-mount]
    S -->|commit| SA[Server Action]
    SA -->|revalidateTag| L

The two barrels

The engine exposes exactly two public entry points; everything else is internal.

Import Marker Contains
@eerp/core-front client-safe descriptors, Zustand store factories, renderers, <Can>, usePermission, the registry
@eerp/core-front/server server-only the ApiClient, RSC data loaders, server permission guards

The server-only marker turns any accidental import of the server barrel from a Client Component into a build error — this is what keeps the access token and the session cookie out of browser code.


ApiClient (the BFF data client)

The ApiClient runs server-side only (RSC, route handlers, Server Actions). It is the single place the frontend talks to Go.

createServerApiClient() reads the session cookie via next/headers, builds ${API_BASE}/api/v${API_VERSION}, and attaches Authorization: Bearer … to every call. Its methods map straight onto Go routes:

Method Go call
list<T>(entity) GET /{entity}/
get<T>(entity, id) GET /{entity}/{id}
create<T>(entity, body) POST /{entity}/
update<T>(entity, id, body) PUT /{entity}/{id}
remove(entity, id) soft-delete (archive)

Two behaviors matter for correctness:

  • Reads integrate the Next Data Cache. GETs use fetch(..., { next: { tags: [entity] } }), so a result is reused across reloads and users. After a successful mutation the client calls revalidateTag(entity) so the next render reflects the write.
  • Single-flight refresh on expiry. A 401-expired triggers exactly one POST /auth/refresh, then retries the original request once. Concurrent 401s share a single in-flight refresh promise — the refresh token is single-use, so a double refresh would trip theft detection. On refresh failure the client clears the session cookie and signals session-expired (callers redirect to /login). It never loops. See Authentication.

Error model

Every failure becomes an ApiError { code, message, requestId, status, fields? }. parseError(response) reads the Go error envelope { error: { code, message, request_id, fields? } }; if the body is not that shape it synthesizes a code from the status (500 → INTERNAL_ERROR). The requestId is always surfaced so a UI error can be traced back to a backend log line.

Server Actions never throw a Go error to the client: a production Next build replaces a thrown error's message with an opaque digest. Wrap the body in settleAction(() => …), which returns an ActionError value; EntityView and every Ops provider (unwrapActions) turn it back into a thrown ApiError, so consumers keep plain try/catch. ErrorAlert renders it — required fields named, fields missing from the form flagged, and a Copy details button for the administrator.


Descriptors

A view is described by data, not code:

type ViewType = 'form' | 'tree' | 'dashboard' | 'catalog'

interface FieldDescriptor {
  name: string
  label: string
  type: 'text' | 'number' | 'date' | 'relation' | 'boolean' | 'selection' | 'totals' | 'address'
  required?: boolean
  widget?: string            // rendering hint, e.g. 'picture' | 'long' — see the widget matrix below
  states?: Condition         // declarative visible/readOnly/required
  groups?: string[]          // UX mirror of a server-side field-group gate — see ADR-014
}

interface ViewDescriptor<T> {
  entity: string             // maps to the Go route: 'crm' → /crm/
  viewType: ViewType
  fields: FieldDescriptor[]
  layout?: LayoutNode         // optional explicit layout tree — see below
  extends?: ViewExtension     // optional inheritance from another module's view
  permissions?: string[]
  search?: SearchDescriptor   // list search/filter bar config — see ADR-015
  actions?: MenuNode[]        // form options-menu tree — see ADR-012
  headerButtons?: HeaderButtonDescriptor[] // always-visible workflow buttons — see ADR-012
  statusBar?: { field: string; editable?: boolean } // status breadcrumb — see Form Chrome
  formContext?: string        // a settings endpoint whose keys conditions read as `context.<key>`
  viewModeDefaults?: ViewModeDefaults // module baselines for the tree display modes (below)
  dashboardLinks?: { title: string; description?: string; icon?: string; path: string }[] // dashboards only
  showChatter?: boolean       // per-record activity feed panel — see the Chatter Panel page
  navLabel?: string           // label in the per-app top-bar header menu
  navQuery?: string           // a `?…` query appended to the menu link (a removable default filter)
  hideFromTopBar?: boolean    // opt a route out of the auto-populated top-bar menu
  listFilter?: Record<string, unknown> // a fixed filter baked into a route (e.g. a status-scoped sub-list)
  catalog?: CatalogDescriptor  // icon/title/subtitle rows — 'catalog' viewType only
}

Adding an entity is a new descriptor. Adding a view type is one store factory + one renderer + one server loader path — and then every entity can use it.

Every one of the descriptor blocks above is plain, RSC-safe data, validated once at ModuleRegistry.register() time — never a function, never inspected lazily at render — the same posture extends, compute, and on_change already take (see View inheritance and Computed & Reactive Fields below). See Views & Components for a practical, per-view-type and per-widget usage guide; this page stays the architectural reference.


Layout: the default form anatomy

layout is optional. When a form descriptor declares none, layout normalization synthesizes a richer default than the flat one other view types get: a header row (the first widget: 'picture' field, then the first text field rendered large), a two-column group holding every other field, and a notebook whose default "Settings" page collects every widget: 'long' field. tree, dashboard, and any future view type keep the original flat, untitled group. A module that declares an explicit layout gets exactly what it declared, at any view type — declaring one opts out of the default.

The synthesized nodes carry stable, well-known ids (__form_header, __form_columns) so they're addressable by the same extension mechanism as a hand-authored layout. See ADR-008: Default Form Anatomy for the full design, including the notebook node and the container-query column grid.


View inheritance: extends

A module can reshape another module's already-registered view without editing its source — the frontend counterpart to the ORM's orm.WithTableName() schema extension. extends names a target path and a list of serializable Operations (addField, removeField, setField, move, addNode, setDescriptor), applied once at ModuleRegistry.register() time — never at render. Renderers always receive one plain, already-merged ViewDescriptor and stay unaware inheritance exists.

const extension: ViewExtension = {
  target: '/crm/:id',
  operations: [
    { op: 'addField', name: 'date', field: { name: 'date', label: 'Date', type: 'date' } },
    { op: 'move', name: 'email', target: 'name', position: 'before' },
  ],
}

Operations are data an interpreter walks (the same reasoning that keeps compute/on_change registered by name rather than passed as closures) — they cross the RSC server/client boundary as plain JSON and are fully unit-testable without a running app. A base module's route path is part of its public contract: renaming it breaks every extension targeting it, loudly, at registration. Extending modules must declare the base module in their own depends so topological registration order guarantees the target already exists. See ADR-006: Frontend View Inheritance; core/modules/crminheritdemo is the worked example end to end.


Display modes and runtime view settings

TreeRenderer renders a List | Kanban | Calendar | Graph | Gantt | WBS | PERT | OBS | Risk mode switcher above every tree view; the choice persists per entity in the browser. A mode is omitted from the switcher (not shown disabled) until its configuration resolves, and a persisted mode that has since been turned off falls back to List.

The configuration comes from two sources:

  • Module baseline: ViewDescriptor.viewModeDefaults. Some keys are only ever module-declared.
  • Workspace override: the admin's choice in app_settings views.<entity>.fields (GET|PUT /api/v1/settings/views/:entity/fields), which always wins when non-null. A null means "not overridden", not "off". Settings → Views shows each value pre-filled from its effective value, warns before diverging from a module default, and offers a revert to module settings button.
Key Mode Overridable
kanbanStatusField Kanban (selection or many2one field) yes
kanbanColumns, kanbanBadgeField Kanban columns from a many2one's records no
calendarDateField Calendar yes
calendarColorField Calendar red accent from a boolean no
enableGraphs Graph yes
ganttStartField, ganttEndField Gantt yes
ganttDependencies, colorTagsField Gantt arrows, color groups (also Kanban/Calendar ribbons) no
wbsParentField, wbsGroupField, wbsOrderField WBS no
pertDependencies, pertHoursField PERT no
obs OBS (list pinned to one project) no
risk Risk (list pinned to one project) no

Graph mode's tile layout is a sibling key, views.<entity>.graph. The Settings → Views UI sources its choices from the registered descriptor (selection fields for Kanban, date fields for Calendar and Gantt): the descriptor owns which fields exist, app_settings which one a workspace picked. See ADR-007 and Settings.

Every mode shares the same data rules: counts and pages reflect every matching row, never a loaded page (RelationOps.listPage → {records, total}), every total renders through formatTotal, and the search bar's structured filters reach every mode. Field moves (Kanban, Calendar, Gantt) go through useOptimisticFieldMove; clicks on an empty spot quick-create a record (quick-create.tsx).

See Views & Components for a per-mode walkthrough with shipped examples.


Field Widgets

FieldDescriptor.widget picks a rendering variant for a type; each type has a default (the first entry) so widget is only needed to opt into a non-default one:

Type Widgets (first = default)
text simple, long, phone, table, color (a list column shows a swatch beside the hex), url, html (Tiptap rich text — the storing service must sanitize), mindmap (read-only mind-map over sibling fields — see Risk View)
number float, int, percent, stars, duration (integer minutes edited as hours : minutes), phone, monetary
boolean switch, picture, signature, file, stars (one star toggling the flag, e.g. a task's priority)
date simple (YYYY-MM-DD), datetime (a timestamptz instant edited in the browser's zone, stored as UTC)
selection select, linked (patches sibling fields per widgetOptions.presets — e.g. PDF page-format presets), radio (one radio per option in a row, for short lists such as a contact's Company/Contact kind)
relation search, tags, list (one default per relation kind), plus carousel, summary and colortags opt-ins
geo point, shape — Leaflet maps over PostGIS columns (Geographic Widgets)
distance meters — the distance between two records' points, in km or miles
totals recap — a live, computed recap of sibling relation lines (e.g. a sale quote's tax-rate breakdown); always store: false
address form — a composite widget owning seven sibling columns (<name>_number\|complement\|street\|zip_code\|city\|state\|country), with optional OpenStreetMap autocomplete (see Settings → OpenStreetMap connector)

See Views & Components for a one-page-per-widget usage guide with real examples from shipped modules.

Dispatch is a flat lookup table keyed by "<type>/<widget>", not a switch — adding a widget is one new table entry and one new component, never a branch to extend:

const WIDGET_COMPONENTS: Record<string, ComponentType<WidgetProps>> = {
  'text/simple': TextSimpleWidget, 'boolean/picture': BooleanPictureWidget, /* … */
}
function fieldWidget(field: FieldDescriptor): ComponentType<WidgetProps> {
  const widget = resolveWidget(field)   // validates + applies the default
  return WIDGET_COMPONENTS[`${field.type}/${widget}`]
}

An invalid combination (widget: 'stars' on a text field) throws at registration, naming the field and the bad pairing — the same fail-fast posture as an unresolved view extension. Numeric widgets format exclusively through useNumberFormat(), so a workspace's decimal/thousands-separator setting applies everywhere with zero per-widget code.

boolean/picture and boolean/signature are the two widgets backed by the object storage service: the field's boolean value tracks whether an image exists, but the bytes live in S3, addressed by {table, recordId, field}. signature renders an HTML5 <canvas> the user draws on with pointer events; "done" calls canvas.toBlob() and uploads the PNG. Both widgets are service-authoritative — on mount they resolve the anchor against the picture service and reconcile the draft boolean with what actually exists there, rather than trusting the record's own field value.

SelectionDescriptor { options: string[] } (FieldDescriptor.selection, required for type: 'selection') declares a closed value list for the one select widget — the type exists to express "a fixed set of values," not a presentation choice. Order matters twice: it's the dropdown's display order, and (absent an explicit default) the first option seeds the field.

{ name: 'status', label: 'Status', type: 'selection',
  selection: { options: ['incoming', 'running', 'won', 'lost', 'closed'] } }

Computed & Reactive Fields

compute and on_change on a FieldDescriptor are names, resolved against a module-populated registry — never functions on the descriptor itself, for the same RSC-serialization reason extends operations are data rather than closures (see View inheritance).

export function registerFieldFunction(fn: {
  entity: string; name: string; depends: string[]
  handler: (draft: Readonly<DraftRecord>) => unknown
}): void

export function registerOnChange(handler: {
  entity: string; name: string; onChange: string[]
  handler: (draft: Readonly<DraftRecord>) => Partial<DraftRecord> | void
}): void

A module registers these at import time, then references them by name on a field:

registerFieldFunction({
  entity: 'crm', name: 'crm.displayName', depends: ['name', 'company'],
  handler: (draft) => { const n = String(draft.name ?? ''); const c = String(draft.company ?? ''); return c ? `${n} (${c})` : n },
})
registerOnChange({
  entity: 'crm', name: 'crm.scoreFromStatus', onChange: ['status'],
  handler: (draft) => ({ score: STATUS_SCORE[String(draft.status ?? '').toLowerCase()] ?? 0 }),
})

// on the descriptor:
{ name: 'display_name', type: 'text', compute: 'crm.displayName', store: false }

compute fields recompute whenever a field in depends changes and re-run in dependency order; pair one with store: false to keep it out of the record's commit payload when it's purely derived for display. on_change fields run their handler whenever any field in their own onChange list changes, patching the draft with whatever the handler returns. Duplicate registration names throw immediately — the registry is a flat map, not a namespace a module can quietly shadow.


Relation Widgets

A type: 'relation' field carries a relation: RelationDescriptor describing which entity it points at and how:

type RelationKind = 'many2one' | 'one2many' | 'many2many'

interface RelationDescriptor {
  entity: string           // the related entity (its Go route/table name)
  kind: RelationKind
  labelField?: string      // default 'name'
  inverseField?: string    // one2many: the FK column on the related entity
  via?: string             // many2many: the junction entity name
  viaFields?: { own: string; related: string }  // default '<own>_id' / '<related>_id'
}

Each kind gets exactly one default widget — RELATION_KIND_WIDGETS = { many2one: 'search', one2many: 'list', many2many: 'tags' } — validated at registration (an o2m without inverseField, or an m2m without via, fails fast naming the field). See ORM → Relations for how these map to plain FK columns and junction tables on the Go side.

Two widget overrides opt a relation field into a different presentation:

  • widget: 'carousel' (o2m) — a photo-gallery-style carousel over embedded rows each backed by a boolean/picture field, e.g. propertymanagement's Equipment "Photos."
  • widget: 'summary' (many2one) — an inline card summarizing the linked record instead of a search box.

Many-to-many resolution is batched, not per-row: resolveManyToManyLinks fetches a field's junction rows, then resolves every linked record in one call via in[id]=<id1>,<id2>,... (see ORM → Query Parameters) regardless of row count — the REST answer to the N+1 fan-out problem GraphQL is usually reached for. See ADR-018.

{ name: 'contact_id', label: 'Contact', type: 'relation',
  relation: { entity: 'contact', kind: 'many2one', labelField: 'name' } },
{ name: 'tags', label: 'Tags', type: 'relation',
  relation: { entity: 'tag', kind: 'many2many', via: 'crm_tag', labelField: 'name' } },

Relation fields render on forms only — the list DataGrid skips them. many2one (RelationSearchWidget) is a debounced MUI Autocomplete; many2many (RelationTagsWidget) is a chip picker whose links are junction rows written immediately (not part of the record's own draft); one2many (RelationListWidget) is a read-only embedded grid of the inverse-side records.

The many2one search widget appends a synthetic option after its (max 6) search results: picking it opens RelationCreateWizard, a real form dialog for the related entity. It reuses that entity's own registered form descriptor when one exists (falling back to a bare single-field form for lookup-only entities like tag), so seed defaults, on_change, compute, and permission checks all behave exactly as they would on the entity's real form — Go re-authorizes the create the same as any other write. A preset seeds and hides fields the caller already knows (e.g. the inverse FK for an o2m context). This is the mechanism behind "add a new contact without leaving the CRM form."


Zustand stores

Each per-view store is seeded with server-fetched initialData and does not fetch on mount — the server already did the work. Store factories live in the engine:

  • createEntityStore<T>(descriptor, initialData) → { records, selected, error, setSelected }
  • createFormStore → draft, dirty, edit(record), setField(k, v), commit(). commit() invokes the entity's Server Action (create vs update decided by id), updates optimistically, and clears dirty; server revalidation then supplies the authoritative data.
  • createTreeStore<T extends {id; parent_id?}> → expanded, roots, children(id), toggle(id).
  • createDashboardStore → widgets[], refresh().

Two cross-cutting stores use Zustand persist (localStorage):

  • useSessionStore — a non-secret mirror of identity + effective permissions, used only for UI gating. The server remains the source of truth; the mirror never authorizes anything that matters.
  • useUiStore — theme, sidebar, last route.

Mutations always reconcile: the client updates optimistically, the Server Action calls Go, revalidateTag fires, and the server re-render supplies the real data.


Renderers and the server dispatcher

The split between server and client is explicit:

  • Server — loadView(descriptor, serverApi) fetches cached initialData. EntityViewServer({ descriptor }) is a Server Component that loads the data and hands it to the client view.
  • Client — EntityView<T> ('use client') builds the Zustand store from the descriptor + initialData, shows an MUI Alert on error (with requestId in the caption), and otherwise dispatches on viewType:
    • FormRenderer — fields → MUI inputs bound to draft via setField; Save calls commit() and is disabled unless dirty.
    • TreeRenderer — MUI X RichTreeView from roots/children/expanded, with a flat @mui/x-data-grid fallback (columns from fields).
    • DashboardRenderer — a responsive MUI grid of widget cards.

All three render server-seeded from a descriptor with zero entity-specific code.

The UI is themed through the engine's theme/palette/tokens modules and the host's AppThemeProvider, so modules inherit a consistent look without shipping their own theme.


Permission gate

Authorization is enforced on the server; the client only gates UI.

The DSL is module:resource:action with segment-wise wildcards: * matches any segment, so crm:*:read grants crm:contacts:read and *:*:read grants any :read.

hasPermission(effective: string[], required: string): boolean
  • Server — requirePermission(required) guards RSC and route handlers (redirect or 403 when missing). This is the real check.
  • Client — the <Can permission> component and usePermission(required) hook read the effective set from useSessionStore to show or hide UI. Because the mirror is non-secret and the server re-checks every request, a tampered client mirror cannot grant access — it can only mis-paint the UI for the user doing the tampering.

Testing

Every file ships with tests (a non-negotiable rule from the build plan):

  • Unit tests mock the ApiClient/Server Actions — no network. They assert store seeding, create-vs-update routing, the wildcard permission matrix, the single-flight refresh under concurrency, and that mutations call revalidateTag.
  • Integration tests run against MSW or a live backend, are named *.integration.test.ts, and are skipped unless TEST_API_BASE is set.

See Testing for the backend equivalent.