Admin panel in English and Polish, reworded per docs/dev/admin-ux.md
- admin-i18n module: AdminText { en, pl }, component texts under the ipal
namespace (project translations win), en + pl admin languages when the
project sets none (project's supportedLanguages wins)
- every plugin label, description, option, tab and group as { en, pl };
explicit labels instead of auto-labels; sentence case; glossary terms
- visible renames (slugs, names, data unchanged): Cookie banner, Form
messages, Privacy group, Search engines, Maintenance, Spam protection,
E-mail sending, stats nav link Overview (hidden without access)
- Polish visitor defaults by page locale: cookie banner, form messages,
validators, lightbox, accessibility widget; {field} uses the label
- fixes: e-mail status banner used Turnstile wording; developer words and
env var names out of editor messages; stats numbers/dates in the admin
language; test e-mail no longer says ipal-kit
- test/adminUx.test.ts: length limits, en + pl present, no developer words
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
eeac33ed2f
commit
6b46bc0c14
@@ -2,23 +2,31 @@ import type { Payload } from 'payload'
|
||||
|
||||
import type { IntegrationStatus } from './status.js'
|
||||
|
||||
import { turnstileKeyProblem } from '../turnstile/config.js'
|
||||
import { turnstileMissingKey } from '../turnstile/config.js'
|
||||
import { type IntegrationLabel, resolveLabel, serverText, STATUS_CODE_TEXTS } from './texts.js'
|
||||
|
||||
type Props = {
|
||||
/** Admin route, for the link. Default '/admin'. */
|
||||
adminRoute?: string
|
||||
/** Status groups to check: field name → label. */
|
||||
checks?: Record<string, string>
|
||||
/** Status groups to check: field name → label (one string, or one per language). */
|
||||
checks?: Record<string, IntegrationLabel>
|
||||
/** Injected by Payload into admin server components. */
|
||||
i18n?: { language?: string; t?: (key: never, vars?: Record<string, unknown>) => string }
|
||||
/** Injected by Payload into admin server components. */
|
||||
payload?: Payload
|
||||
}
|
||||
|
||||
const DEFAULT_CHECKS: Record<string, IntegrationLabel> = {
|
||||
emailStatus: { en: 'E-mail sending', pl: 'Wysyłka e-mail' },
|
||||
turnstileStatus: { en: 'Spam protection', pl: 'Ochrona przed spamem' },
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard alert (admin.components.beforeDashboard): lists integrations that
|
||||
* are currently failing, with a link to Site Integrations. Renders nothing when
|
||||
* are currently failing, with a link to Site integrations. Renders nothing when
|
||||
* everything is fine — and never throws (DB not ready → nothing).
|
||||
*/
|
||||
export async function IntegrationAlerts({ adminRoute = '/admin', checks = { emailStatus: 'Email', turnstileStatus: 'Turnstile' }, payload }: Props) {
|
||||
export async function IntegrationAlerts({ adminRoute = '/admin', checks = DEFAULT_CHECKS, i18n, payload }: Props) {
|
||||
if (!payload) {return null}
|
||||
let doc: Record<string, unknown>
|
||||
try {
|
||||
@@ -26,17 +34,26 @@ export async function IntegrationAlerts({ adminRoute = '/admin', checks = { emai
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
const i18nLike = i18n
|
||||
const language = i18n?.language
|
||||
const failing = Object.entries(checks)
|
||||
.map(([field, label]) => {
|
||||
const status = doc?.[field] as IntegrationStatus | undefined
|
||||
return { label, message: status?.state === 'degraded' ? (status.message ?? status.code ?? 'error') : null }
|
||||
if (status?.state !== 'degraded') {return { field, label: resolveLabel(label, language), message: null }}
|
||||
const key = status.code ? STATUS_CODE_TEXTS[status.code] : undefined
|
||||
const message = key ? serverText(i18nLike, key) : (status.message ?? status.code ?? serverText(i18nLike, 'alertsUnknownError'))
|
||||
return { field, label: resolveLabel(label, language), message }
|
||||
})
|
||||
.filter((x): x is { label: string; message: string } => x.message !== null)
|
||||
.filter((x): x is { field: string; label: string; message: string } => x.message !== null)
|
||||
// Configuration problems no request will ever record (nothing is verified
|
||||
// while Turnstile is off) — checked live from the saved keys.
|
||||
const keyProblem = turnstileKeyProblem(doc ?? {})
|
||||
if (keyProblem && !failing.some((f) => f.label === 'Turnstile')) {
|
||||
failing.push({ label: 'Turnstile', message: keyProblem })
|
||||
const missing = turnstileMissingKey(doc ?? {})
|
||||
if (missing && 'turnstileStatus' in checks && !failing.some((f) => f.field === 'turnstileStatus')) {
|
||||
failing.push({
|
||||
field: 'turnstileStatus',
|
||||
label: resolveLabel(checks.turnstileStatus, language),
|
||||
message: serverText(i18nLike, missing === 'secret' ? 'statusTurnstileNoSecretKey' : 'statusTurnstileNoSiteKey'),
|
||||
})
|
||||
}
|
||||
if (!failing.length) {return null}
|
||||
|
||||
@@ -51,16 +68,16 @@ export async function IntegrationAlerts({ adminRoute = '/admin', checks = { emai
|
||||
padding: '1rem 1.25rem',
|
||||
}}
|
||||
>
|
||||
<strong>Integration problem — the site still works, but something needs attention.</strong>
|
||||
<strong>{serverText(i18nLike, 'alertsHeading')}</strong>
|
||||
<ul style={{ margin: '0.5rem 0 0', paddingLeft: '1.2rem' }}>
|
||||
{failing.map(({ label, message }) => (
|
||||
<li key={label}>
|
||||
{failing.map(({ field, label, message }) => (
|
||||
<li key={field}>
|
||||
<strong>{label}:</strong> {message}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<a href={`${adminRoute.replace(/\/$/, '')}/globals/site-integrations`} style={{ display: 'inline-block', marginTop: '0.5rem' }}>
|
||||
Open Site Integrations →
|
||||
{serverText(i18nLike, 'alertsLink')}
|
||||
</a>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
'use client'
|
||||
import { useFormFields } from '@payloadcms/ui'
|
||||
import { useFormFields, useTranslation } from '@payloadcms/ui'
|
||||
|
||||
import type { IntegrationBannerTexts } from './status.js'
|
||||
|
||||
import { formatDateTime, ipalT } from '../admin-i18n/t.js'
|
||||
import { type IntegrationLabel, resolveLabel, STATUS_CODE_TEXTS } from './texts.js'
|
||||
|
||||
type Props = {
|
||||
/** Integration name shown in the banner, e.g. 'Turnstile'. */
|
||||
label: string
|
||||
/** Integration name shown in the banner, e.g. { en: 'Spam protection', pl: 'Ochrona przed spamem' }. */
|
||||
label: IntegrationLabel
|
||||
/** Name of the hidden status group (buildIntegrationStatusField). */
|
||||
statusField: string
|
||||
}
|
||||
|
||||
const fmt = (iso: unknown) => {
|
||||
if (typeof iso !== 'string' || !iso) {return ''}
|
||||
const d = new Date(iso)
|
||||
return Number.isNaN(d.getTime()) ? '' : d.toLocaleString()
|
||||
/** Texts keys for this integration's failing heading and hint. */
|
||||
texts?: IntegrationBannerTexts
|
||||
}
|
||||
|
||||
/**
|
||||
* Status banner on an integration's tab (Site Integrations). Shows nothing until
|
||||
* Status banner on an integration's tab (Site integrations). Shows nothing until
|
||||
* the integration has been used once; a red box while it's failing; a short
|
||||
* green line once it works again.
|
||||
* green line once it works again. Wording comes from the integration (`texts`).
|
||||
*/
|
||||
export function IntegrationStatusBanner({ label, statusField }: Props) {
|
||||
export function IntegrationStatusBanner({ label, statusField, texts = {} }: Props) {
|
||||
const { i18n, t } = useTranslation()
|
||||
const state = useFormFields(([f]) => f[`${statusField}.state`]?.value) as string | undefined
|
||||
const message = useFormFields(([f]) => f[`${statusField}.message`]?.value) as string | undefined
|
||||
const code = useFormFields(([f]) => f[`${statusField}.code`]?.value) as string | undefined
|
||||
@@ -28,14 +30,28 @@ export function IntegrationStatusBanner({ label, statusField }: Props) {
|
||||
|
||||
if (!state) {return null}
|
||||
|
||||
const name = resolveLabel(label, i18n.language)
|
||||
const date = (iso: unknown) => {
|
||||
if (typeof iso !== 'string' || !iso || Number.isNaN(new Date(iso).getTime())) {return ''}
|
||||
return formatDateTime(iso, i18n.language)
|
||||
}
|
||||
// Project-provided keys are not in AdminTextKey; read them through `t` the same way.
|
||||
const text = (key: string, vars?: Record<string, unknown>) => ipalT(t as never, key as never, vars)
|
||||
|
||||
if (state === 'ok') {
|
||||
const sinceText = date(since)
|
||||
return (
|
||||
<div style={{ fontSize: '0.85rem', marginBottom: '1.5rem', opacity: 0.75 }}>
|
||||
✓ {label} working{since ? ` since ${fmt(since)}` : ''}.
|
||||
✓ {sinceText ? text('statusWorkingSince', { date: sinceText, label: name }) : text('statusWorking', { label: name })}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const codeKey = code ? STATUS_CODE_TEXTS[code] : undefined
|
||||
const detail = codeKey ? text(codeKey) : message
|
||||
const sinceText = date(since)
|
||||
const checkedText = date(checkedAt)
|
||||
|
||||
return (
|
||||
<div
|
||||
role="alert"
|
||||
@@ -47,16 +63,18 @@ export function IntegrationStatusBanner({ label, statusField }: Props) {
|
||||
padding: '0.9rem 1rem',
|
||||
}}
|
||||
>
|
||||
<strong>{label} is not working — forms are accepted without this check.</strong>
|
||||
{message ? <div style={{ marginTop: '0.35rem' }}>{message}</div> : null}
|
||||
<strong>{text(texts.failing ?? 'statusFailing', { label: name })}</strong>
|
||||
{detail ? <div style={{ marginTop: '0.35rem' }}>{detail}</div> : null}
|
||||
<div style={{ fontSize: '0.8rem', marginTop: '0.35rem', opacity: 0.75 }}>
|
||||
{code ? `Code: ${code}. ` : ''}
|
||||
{since ? `Since ${fmt(since)}. ` : ''}
|
||||
{checkedAt ? `Last seen ${fmt(checkedAt)}.` : ''}
|
||||
</div>
|
||||
<div style={{ fontSize: '0.8rem', marginTop: '0.35rem', opacity: 0.75 }}>
|
||||
Fix the keys below and save — the status is checked again on the next form submission.
|
||||
{[
|
||||
code ? text('statusCode', { code }) : '',
|
||||
sinceText ? text('statusSince', { date: sinceText }) : '',
|
||||
checkedText ? text('statusLastChecked', { date: checkedText }) : '',
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ')}
|
||||
</div>
|
||||
<div style={{ fontSize: '0.8rem', marginTop: '0.35rem', opacity: 0.75 }}>{text(texts.hint ?? 'statusHint')}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@ export {
|
||||
buildIntegrationStatusField,
|
||||
recordIntegrationStatus,
|
||||
} from './status.js'
|
||||
export type { IntegrationState, IntegrationStatus } from './status.js'
|
||||
export type { IntegrationBannerTexts, IntegrationState, IntegrationStatus } from './status.js'
|
||||
export type { IntegrationLabel } from './texts.js'
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import type { BasePayload, Field } from 'payload'
|
||||
|
||||
import type { AdminTextKey } from '../admin-i18n/texts/index.js'
|
||||
import type { IntegrationLabel } from './texts.js'
|
||||
|
||||
/**
|
||||
* Health of a third-party integration (Turnstile today, SMTP/Graph later),
|
||||
* stored on the SiteIntegrations global so the panel can show it:
|
||||
@@ -19,7 +22,10 @@ export type IntegrationStatus = {
|
||||
checkedAt?: null | string
|
||||
/** Machine code from the provider or the plugin, e.g. 'invalid-input-secret', 'missing-secret'. */
|
||||
code?: null | string
|
||||
/** Human explanation shown in the panel (English — panel language). */
|
||||
/**
|
||||
* Plain explanation for the panel, in English. Codes listed in
|
||||
* STATUS_CODE_TEXTS are shown in the editor's language instead.
|
||||
*/
|
||||
message?: null | string
|
||||
/** When the current state started. */
|
||||
since?: null | string
|
||||
@@ -45,15 +51,29 @@ export function buildIntegrationStatusField(name: string): Field {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wording of the banner while the integration fails: texts keys under the
|
||||
* `ipal` namespace (a project can add its own via `i18n.translations`).
|
||||
* Default: a generic "<label> is not working." and "Correct the settings below and save."
|
||||
*/
|
||||
export type IntegrationBannerTexts = {
|
||||
failing?: ({} & string) | AdminTextKey
|
||||
hint?: ({} & string) | AdminTextKey
|
||||
}
|
||||
|
||||
/** Read-only banner (UI field) — shows the status group `statusField` on the tab. */
|
||||
export function buildIntegrationStatusBanner(statusField: string, label: string): Field {
|
||||
export function buildIntegrationStatusBanner(
|
||||
statusField: string,
|
||||
label: IntegrationLabel,
|
||||
texts: IntegrationBannerTexts = {},
|
||||
): Field {
|
||||
return {
|
||||
name: `${statusField}Banner`,
|
||||
type: 'ui',
|
||||
admin: {
|
||||
components: {
|
||||
Field: {
|
||||
clientProps: { label, statusField },
|
||||
clientProps: { label, statusField, texts },
|
||||
path: '@intecion/ipal-kit/admin#IntegrationStatusBanner',
|
||||
},
|
||||
},
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import type { AdminText } from '../admin-i18n/define.js'
|
||||
import type { AdminTextKey } from '../admin-i18n/texts/index.js'
|
||||
|
||||
import { adminTexts } from '../admin-i18n/texts/index.js'
|
||||
|
||||
/** A label as Payload takes it: one string, or one string per language. */
|
||||
export type IntegrationLabel = AdminText | Record<string, string> | string
|
||||
|
||||
/** The label in `language`, falling back to English, then to any language. */
|
||||
export function resolveLabel(label: IntegrationLabel | undefined, language: string | undefined): string {
|
||||
if (!label) {return ''}
|
||||
if (typeof label === 'string') {return label}
|
||||
const map = label as Record<string, string>
|
||||
return map[language ?? 'en'] ?? map.en ?? Object.values(map)[0] ?? ''
|
||||
}
|
||||
|
||||
/** Status codes whose message has its own text — shown in the editor's language. */
|
||||
export const STATUS_CODE_TEXTS: Record<string, AdminTextKey> = {
|
||||
'bad-response': 'statusTurnstileBadResponse',
|
||||
'graph-unavailable': 'statusEmailGraphUnavailable',
|
||||
'internal-error': 'statusTurnstileInternalError',
|
||||
'invalid-input-secret': 'statusTurnstileInvalidSecret',
|
||||
'missing-input-secret': 'statusTurnstileMissingSecret',
|
||||
'missing-secret': 'statusTurnstileMissingSecret',
|
||||
'network-error': 'statusTurnstileNetworkError',
|
||||
}
|
||||
|
||||
type I18nLike = { language?: string; t?: (key: never, vars?: Record<string, unknown>) => string } | undefined
|
||||
|
||||
/**
|
||||
* One plugin text on the server: through `i18n.t` when Payload's i18n is there
|
||||
* (so a project's own translation wins), else the plugin's English text.
|
||||
*/
|
||||
export function serverText(i18n: I18nLike, key: AdminTextKey, vars: Record<string, unknown> = {}): string {
|
||||
if (i18n?.t) {
|
||||
const out = i18n.t(`ipal:${key}` as never, vars)
|
||||
if (out && out !== `ipal:${key}` && out !== key) {return out}
|
||||
}
|
||||
const lang = i18n?.language === 'pl' ? 'pl' : 'en'
|
||||
return adminTexts[lang][key].replace(/\{\{(\w+)\}\}/g, (_, name: string) => String((vars[name] as number | string | undefined) ?? ''))
|
||||
}
|
||||
Reference in New Issue
Block a user