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,11 +2,14 @@ import type { CollectionBeforeDeleteHook } from 'payload'
|
||||
|
||||
import { APIError } from 'payload'
|
||||
|
||||
import { settingsFieldLabel } from './validateUniqueRole.js'
|
||||
|
||||
/**
|
||||
* Blocks deletion of a page assigned a System Page role (homepage,
|
||||
* privacyPolicy, cookiePolicy, termsOfService). An editor deleting the privacy
|
||||
* policy or homepage by accident would break routing and compliance links; this
|
||||
* stops it with a clear error. They must unassign the role first (deliberate).
|
||||
* stops it with a clear error in the editor's admin language, naming the role by
|
||||
* its field label. They must unassign the role first (deliberate).
|
||||
*
|
||||
* Reads the role assignments from SiteSettings (which page holds which role).
|
||||
*
|
||||
@@ -34,9 +37,11 @@ export function buildPreventDeleteSystemPage(
|
||||
const assignedId =
|
||||
assigned && typeof assigned === 'object' ? (assigned as { id?: unknown }).id : assigned
|
||||
if ((typeof assignedId === 'string' || typeof assignedId === 'number') && String(assignedId) === String(id)) {
|
||||
const label = settingsFieldLabel(req, field, { settingsSlug })
|
||||
throw new APIError(
|
||||
`Cannot delete a page assigned to the system role "${field}". ` +
|
||||
`Detach the role in Site Settings first.`,
|
||||
req.i18n?.language === 'pl'
|
||||
? `Tej strony nie można usunąć, bo jest ustawiona jako „${label}” w ustawieniach witryny. Najpierw wybierz tam inną stronę.`
|
||||
: `This page is set as “${label}” in Site settings, so it cannot be deleted. Choose another page there first.`,
|
||||
400,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,31 +1,83 @@
|
||||
import type { FieldHook } from 'payload'
|
||||
import type { FieldHook, PayloadRequest } from 'payload'
|
||||
|
||||
import { APIError } from 'payload'
|
||||
|
||||
type FieldLike = { fields?: FieldLike[]; label?: unknown; name?: string; tabs?: FieldLike[]; type?: string }
|
||||
|
||||
/** Finds a field by name in data that stays flat: rows, collapsibles and unnamed tabs only. */
|
||||
function findFlatField(fields: readonly FieldLike[] | undefined, name: string): FieldLike | undefined {
|
||||
for (const field of fields ?? []) {
|
||||
if (field.name === name && field.type !== 'tabs') {return field}
|
||||
if (field.type === 'tabs') {
|
||||
for (const tab of field.tabs ?? []) {
|
||||
if (tab.name) {continue}
|
||||
const hit = findFlatField(tab.fields, name)
|
||||
if (hit) {return hit}
|
||||
}
|
||||
} else if ((field.type === 'row' || field.type === 'collapsible') && field.fields) {
|
||||
const hit = findFlatField(field.fields, name)
|
||||
if (hit) {return hit}
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** A field label as the editor sees it, in the admin language; the field name when there is none. */
|
||||
export function labelInLanguage(label: unknown, language: string | undefined, fallback: string): string {
|
||||
if (typeof label === 'string' && label) {return label}
|
||||
if (label && typeof label === 'object') {
|
||||
const map = label as Record<string, unknown>
|
||||
const text = (language ? map[language] : undefined) ?? map.en ?? Object.values(map).find((v) => typeof v === 'string')
|
||||
if (typeof text === 'string' && text) {return text}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
/**
|
||||
* The visible label of a Site Settings field (e.g. "Privacy policy" for
|
||||
* `privacyPolicy`) in the request's admin language. Reads it from the given
|
||||
* fields, or from the global in the Payload config; falls back to the name.
|
||||
*/
|
||||
export function settingsFieldLabel(
|
||||
req: Partial<PayloadRequest> | undefined,
|
||||
name: string,
|
||||
{ fields, settingsSlug = 'site-settings' }: { fields?: readonly unknown[]; settingsSlug?: string } = {},
|
||||
): string {
|
||||
const source =
|
||||
fields ?? (req?.payload?.config?.globals?.find((g) => g.slug === settingsSlug)?.fields as readonly unknown[] | undefined)
|
||||
const field = findFlatField(source as FieldLike[] | undefined, name)
|
||||
return labelInLanguage(field?.label, req?.i18n?.language, name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Field hook for a System Page role relationship in SiteSettings: ensures a page
|
||||
* isn't assigned to two roles at once (e.g. the same page as both homepage and
|
||||
* privacyPolicy), which would make routing ambiguous.
|
||||
*
|
||||
* Attach to each role field's beforeValidate. `siblingFields` are the OTHER role
|
||||
* field names to check against.
|
||||
* field names to check against. The error names both fields by their labels in
|
||||
* the editor's admin language.
|
||||
*
|
||||
* hooks: { beforeValidate: [buildValidateUniqueRole({
|
||||
* siblingFields: ['privacyPolicy', 'cookiePolicy', 'termsOfService'],
|
||||
* })] }
|
||||
*/
|
||||
export function buildValidateUniqueRole(args: { siblingFields: string[] }): FieldHook {
|
||||
return ({ field, siblingData, value }) => {
|
||||
return ({ field, global, req, siblingData, value }) => {
|
||||
if (value == null) {return value}
|
||||
const thisId = typeof value === 'object' ? (value as { id?: unknown }).id : value
|
||||
for (const sibling of args.siblingFields) {
|
||||
const other = (siblingData as Record<string, unknown>)?.[sibling]
|
||||
const otherId = other && typeof other === 'object' ? (other as { id?: unknown }).id : other
|
||||
if ((typeof otherId === 'string' || typeof otherId === 'number') && String(otherId) === String(thisId)) {
|
||||
const name = typeof field === 'object' && 'name' in field ? field.name : 'ta rola'
|
||||
const language = req?.i18n?.language
|
||||
const name = (typeof field === 'object' && 'name' in field ? field.name : '') ?? ''
|
||||
const thisLabel = labelInLanguage(typeof field === 'object' ? field.label : undefined, language, name)
|
||||
const otherLabel = settingsFieldLabel(req, sibling, global?.fields ? { fields: global.fields } : {})
|
||||
throw new APIError(
|
||||
`Ta sama strona jest przypisana do "${name}" i "${sibling}". ` +
|
||||
`Każda rola systemowa musi wskazywać inną stronę.`,
|
||||
language === 'pl'
|
||||
? `Ta sama strona jest ustawiona jako „${thisLabel}” i „${otherLabel}”. Wybierz inną stronę dla każdej roli.`
|
||||
: `The same page is set as “${thisLabel}” and “${otherLabel}”. Choose a different page for each.`,
|
||||
400,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user