Skip to content
Merged
19 changes: 19 additions & 0 deletions .changeset/39569-notification-template-preview-pane.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@object-ui/fields': patch
'@object-ui/i18n': patch
'@objectstack/service-messaging': patch
---

feat(fields, service-messaging): a notification template's record page previews the saved template as it would be sent

Clause-②: no
Metadata-protocol: none

Who sees it: an administrator opening a notification template under Setup → Notification Templates.

- **Under the body, a Preview pane shows the saved subject and body as the messaging service renders them for a send.** The server's preview route renders them with the same function the email and SMS channels use. Nothing is sent and nothing is written.
- **Each placeholder in the saved subject and body gets a sample-value input** (`{{ amount }}` and `{{ payload.amount }}` share one input; `{{ topic }}` is filled from the template's own topic). Typing sends nothing; **Preview with these values** renders again with what was typed.
- **The pane refreshes when the template is saved**, not while you type in the edit form. A refusal (no access to the template, a template that no longer exists) is shown as its own text and code.
- An `html` or `mjml` template shows the rendered HTML through the console's sanitizer; a `markdown` or `text` template shows the plain text the channels send.
- The edit form is unchanged: the body keeps its markdown editor. The pane is the body's read face on the record page, declared on `sys_notification_template.body` as `widget: 'notification-template-preview'`. A list cell of the body draws the body alone.
- The pane's words are translated in every console language (`fields.notificationTemplatePreview.*`).
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ export const NotificationTemplate = ObjectSchema.create({
description: 'Template body, interpreted per `format`. '
+ 'Variables: {{ title }} and {{ body }} (the notification\'s own title and text), {{ topic }}, '
+ 'and {{ payload.<key> }} for any other value its topic sends.',
// [#39569, ruling 7405856818 on #39412 item 2 = B] The record
// page draws the body with the saved row's preview under it, as
// the preview door (`notification-template-preview-routes.ts`)
// renders it against sample values. A read face only: the edit
// form keeps the markdown editor (the hint degrades to `type`).
widget: 'notification-template-preview',
}),

format: Field.select(['markdown', 'html', 'text', 'mjml'], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ const OBJECT_LITERAL_CENSUS: ReadonlyArray<readonly [type: string, text: string,
// ── objectstack#30209: the criteria read face ───────────────────────────
// `{}` is not stored criteria text, so it reads as the text cell reads it.
['filter-condition', '[Object]', false],
// ── objectstack#39569: a notification template body's read face ─────────
// Handed no row, it is the markdown face alone, so `{}` reads as `markdown`.
['notification-template-preview', '[Object]', false],
];

/** A real value per type — the POPULATED half, which refuses the caricature. */
Expand Down Expand Up @@ -372,6 +375,7 @@ const POPULATED_CENSUS: ReadonlyArray<readonly [type: string, value: unknown]> =
['position-ref', 'sales_manager'],
['permission-ref', 'admin_full_access'],
['filter-condition', '{"priority":"urgent"}'],
['notification-template-preview', '**bold**'],
];

describe('objectui#8596 — an object literal is not a cell value, and these renderers invented one', () => {
Expand Down
32 changes: 32 additions & 0 deletions ui/packages/fields/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3611,6 +3611,15 @@ const WIDGET_CELL_RENDERER_TYPES: ReadonlySet<string> = new Set([
'permission-ref',
]);

/**
* Widget hints whose read face draws a `markdown` field: the body it stores,
* and something drawn from it under the body. Promoted over `markdown` alone,
* so the face can always draw the body the way the markdown cell does.
* `notification-template-preview` is a notification template's body with the
* server's preview of the saved row under it (objectstack#39569).
*/
const MARKDOWN_WIDGET_CELL_RENDERER_TYPES: ReadonlySet<string> = new Set(['notification-template-preview']);

/**
* Resolve the canonical cell-renderer key for a field. Accepts either a
* raw type string (back-compat) or a field-metadata object so that
Expand All @@ -3626,6 +3635,9 @@ export function resolveCellRendererType(fieldOrType: string | { type?: string; f
if (typeof widget === 'string' && WIDGET_CELL_RENDERER_TYPES.has(widget) && TEXTUAL_BASE_TYPES.has(baseType)) {
return widget;
}
if (typeof widget === 'string' && MARKDOWN_WIDGET_CELL_RENDERER_TYPES.has(widget) && baseType === 'markdown') {
return widget;
}
const formatRaw = fieldOrType.format;
const format = typeof formatRaw === 'string' ? formatRaw.toLowerCase() : '';
if (format && FORMAT_TO_RENDERER[format] && TEXTUAL_BASE_TYPES.has(baseType)) {
Expand Down Expand Up @@ -4359,6 +4371,26 @@ registerFieldRenderer('permission-ref', PermissionRefCell);
// The criteria key draws its sentence through the widget's own read display,
// loaded lazily on the first cell that shows one (the stored text until then).
registerFieldRenderer('filter-condition', FilterConditionCell);
// A notification template's body, with the server's preview of the saved row
// under it on the record page (objectstack#39569). The pane loads lazily, off
// the eager closure. A grid cell, and a host that hands no row, draw the body
// alone and ask nothing: the pane previews the row it is handed.
const LazyNotificationTemplatePreview = recoverableLazy(() =>
import('./widgets/NotificationTemplatePreviewField.js').then((m) => ({ default: m.NotificationTemplatePreviewField })),
);
function NotificationTemplatePreviewCell(props: CellRendererProps): React.ReactElement {
const body = <RICH_TEXT_CELL_RENDERERS.markdown {...props} />;
if (props.inGridColumn || !props.dependentValues) return body;
return (
<div>
{body}
<React.Suspense fallback={null}>
<LazyNotificationTemplatePreview dependentValues={props.dependentValues} />
</React.Suspense>
</div>
);
}
registerFieldRenderer('notification-template-preview', NotificationTemplatePreviewCell);
// `owner` was registered here to the same UserCellRenderer until objectui#4814
// retired the spelling — see the TOMBSTONE below. `getCellRenderer('owner')`
// now reports the prescription and falls to the text cell.
Expand Down
Loading
Loading