Files
ipal-kit/docs/notifications.md
T
Radosław SmolińskiandClaude Opus 5.5 37592764b5 Docs: fix doc/code drift found against 1.16.3 (docs only)
Verified the IAWL summary's "Doc/code" notes against the code and swept
CLI flags, option names/defaults and env vars. Fixed in the docs:
admin groups (consent-records, cookie-review → Privacy; Form messages
global → Forms), tab labels (More, Spam protection, E-mail sending,
Search engines; Sending method), construction field labels, {field} =
label, globalOverrides instead of "a plugin after ipalKit", enabled:false
keeps globals, pages role labels en+pl, switchLocalePath prefix,
accessibility cookie (necessary), seeding summary() shape, prepublishOnly
chain, doctor --strict --offline in the starter, package files list,
ipal-skills --external in PROGRAMISTA, DATABASE_URI (not _URL),
uploadsCollection exists, SiteProviders nonce, buildLinksField labels,
MCP list_pages limit wording, IPAL_DOCTOR_TOKEN in the env table; shipped
seo-check message "Search engines". Reference skills copy refreshed.

Open questions (not changed): forms over-long text, PAYLOAD_SECRET
length, Site settings update access, doctor seen in CI, bare
`ipal-kit create --external`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 23:47:35 +01:00

119 lines
6.6 KiB
Markdown

# notifications
Notification texts (action results) configured in the admin panel, per language, with a
fallback. Today it covers form result messages (`submitForm`),
with room for future contexts. The **Notifications** global is always built.
## Principle
The plugin returns a result CODE (`submitForm` returns `reason`), not text. This module
maps code → text from the panel (localized), with an English fallback per field.
The frontend gets a ready string and styles it as it likes (toast, inline, banner).
This way no message is hardcoded — everything goes through the panel.
## Config
No options — the **Notifications** global (slug `notifications`, shown in the panel as **Form messages**, group *Forms*) is always built. The editor manages
the texts in the panel (Form messages), grouped per context. Group `form`:
`success`, `error`, `rateLimited`, `turnstile`, `validation`, `consent`,
`notFound` and the multi-step form buttons: `next` ("Next"), `back` ("Back"),
`stepOf` ("Step {current} of {total}"). Every empty field → the built-in default in the page language
(`notificationFallback(locale)`: `NOTIFICATION_FALLBACK_PL` for `pl`, else `NOTIFICATION_FALLBACK` — EN;
all exported from `@intecion/ipal-kit`). In `validation`, `{field}` is the field's label in the page language.
For forms: `getFormTexts(locale)` from `lib/content` = the `form` group in the given language.
Group `lightbox` (only with `ipalKit({ lightbox: true })` — adds columns, run a migration on Postgres):
`open` ("Enlarge image"), `dialog` ("Image viewer"), `previous`, `next`, `close`, `counter`
("{current} / {total}"). `getNotificationTexts(...).lightbox` always returns the full set (fallback
`LIGHTBOX_FALLBACK`), and `getSiteShell(locale).lightboxTexts` carries it to `<SiteProviders lightbox>`.
Details: [lightbox.md](./lightbox.md).
## Helper — getNotificationTexts
Reads the texts from the global per language, with a fallback per field. The counterpart of `getConsentTexts`:
```ts
import { getNotificationTexts } from '@intecion/ipal-kit'
const notifications = await getNotificationTexts({ payload, locale }) // in lib/content: getFormTexts(locale) = .form
// notifications.form.error, notifications.form.success, ..., notifications.lightbox.close
```
## Mapping the result — resolveFormMessage
The bridge between `submitForm` and the UI: takes the result and the texts, returns one message.
Interpolates `{field}` in the validation text. NEVER shows a raw exception
(`error` → a generic text, not the backend error message).
```ts
import { resolveFormMessage } from '@intecion/ipal-kit'
const result = await submitFormAction(...)
if (!result.success) {
setError(resolveFormMessage(result, notifications.form))
}
```
This replaces a hardcoded `Error: ${result.reason}` — now a friendly text from the panel,
per language.
## File field texts
With `forms.attachments` ([forms.md](./forms.md) "Files") the **Files** group of Form messages holds:
Each one tells the visitor that the problem is the file, why, and what to do next.
| Key | Placeholders | English default |
|---|---|---|
| `fileTooLarge` | `{file}`, `{max}` (MB) | `The file "{file}" is too large. Choose a file of up to {max} MB.` |
| `fileType` | `{file}`, `{types}` | `The file "{file}" is in a format we do not accept. Choose one of: {types}.` |
| `fileRefused` | `{file}`, `{types}` | `The file "{file}" was refused: it is a program or an archive, it holds macros, or its content does not match its type. Choose another file ({types}).` |
| `fileCount` | `{field}`, `{max}` | `Too many files in "{field}". Remove some: the limit is {max}.` |
| `fileNotSent` | `{file}` | `We could not send the file "{file}". Choose a smaller file or another format and try again.` |
| `fileOptional` | — | `You can also send the form without the file.` — added to the five above when the File field is not required |
| `fileRemove` | `{file}` | `Remove {file}` |
Polish defaults ship too; any other language is filled in the panel (an empty field falls back to English there).
`resolveFormMessage` maps `kind: 'file_too_large' | 'file_type' | 'file_refused' | 'file_count'` and
`reason: 'delivery'` to them and appends `fileOptional` when the result says `optional`; a texts object without
these keys falls back to `validation` / `error`. The result carries `field`, so the message shows at the File field.
## {field} interpolation
The `validation` text may contain `{field}` — it is replaced with the **label** of the field with
the error in the page language (the technical `name` only when the field has no label), only the first `{field}` occurrence;
no field at fault → the generic `error` text instead:
```
Panel: "Sprawdź pole {field} i spróbuj ponownie."
Result: "Sprawdź pole E-mail i spróbuj ponownie."
```
## Adding new contexts
The `form` group is the first context. Further ones (`newsletter`, `system`) are added
the same way — a new group in `globals/Notifications/fields.ts` + a field in the types +
a fallback. `getNotificationTexts` resolves whatever exists.
## Access
The global has `read: () => true` — the texts are public (shown to end
users), so the frontend reads them without a session. Write: `editor+` (`adminOrEditor`). Unlike SiteIntegrations
(`read: isAdmin` — secrets live there).
## Turn off, override, extend
- **Turn off:** none — the `notifications` global is currently always added (also without the `forms` option). The `lightbox` group is opt-in (`ipalKit({ lightbox: true })`); `lightboxNotificationFields` puts it in your own global instead.
- **Override texts:** in the panel, per language; empty field → `notificationFallback(locale)` (Polish on `pl` pages, English otherwise).
The fallbacks cannot be swapped with a plugin option — apply your own default texts in the project
on the result of `getNotificationTexts` (e.g. `texts.form.error ||= '…'`).
- **Override access / hooks / admin:** `ipalKit({ globalOverrides: { notifications: … } })` — an object merges
`access`, `admin` and `hooks` key by key over the plugin's (e.g. let any logged-in user edit:
`{ notifications: { access: { update: authenticated } } }`), a function gets the global and returns the one to use.
- **Override mapping:** `resolveFormMessage` is optional — you can map `result.reason`
with your own switch.
- **Extend:** the global's fields are fixed in the plugin (no `ipalKit` option to add groups / fields);
for a project-only extra, a `globalOverrides.notifications` function can append fields, but
`getNotificationTexts` will not read them — a new shared context needs a change in the plugin
(`globals/Notifications/fields.ts`, types, fallback).