Files
ipal-kit/src/modules/seo/injectSeoTabs.ts
T
Radosław SmolińskiandClaude Opus 5.5 6b46bc0c14 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]>
2026-10-27 15:46:25 +01:00

78 lines
2.9 KiB
TypeScript

import type { Config, Field, TabsField } from 'payload'
import type { SeoOption } from './types.js'
const CONTENT_TAB = { en: 'Content', pl: 'Treść' }
const SEO_TAB = { en: 'SEO', pl: 'SEO' }
/** The `meta` group sits alone in the SEO tab, so its own "SEO" heading would repeat the tab name. */
const withoutHeading = (meta: Field): Field => ({ ...meta, label: false }) as Field
/**
* Wraps each SEO-enabled collection's fields into a tabbed UI: a "Content" tab
* holding the collection's own fields, and an "SEO" tab holding the `meta`
* group that @payloadcms/plugin-seo added.
*
* This replaces plugin-seo's own `tabbedUI`, which merges tabs by inspecting
* the first field and breaks when other plugins/fields (roles, slug) already
* sit in the collection — producing an empty SEO tab. Running this AFTER
* plugin-seo (so `meta` already exists) and after other field injections lets
* us build the tabs deterministically.
*
* If a collection's first field is already a tabs field, the SEO tab is
* appended to it instead of creating a new wrapper.
*/
export function injectSeoTabs(config: Config, seo: SeoOption): Config {
return {
...config,
collections: (config.collections ?? []).map((collection) => {
if (!seo.collections.includes(collection.slug)) {
return collection
}
const fields = collection.fields ?? []
// Separate the meta group (added by plugin-seo) from the rest.
const metaField = fields.find(
(f): f is { name: string } & Field => 'name' in f && f.name === 'meta',
)
const otherFields = fields.filter(
(f) => !('name' in f && f.name === 'meta'),
)
// Nothing to do if plugin-seo hasn't added meta (shouldn't happen).
if (!metaField) {return collection}
// If the collection already leads with a tabs field, append an SEO tab.
const firstField = otherFields[0]
if (firstField && firstField.type === 'tabs') {
const existingTabs = firstField
const withSeoTab: TabsField = {
...existingTabs,
tabs: [...existingTabs.tabs, { fields: [withoutHeading(metaField)], label: SEO_TAB }],
}
return {
...collection,
fields: [withSeoTab, ...otherFields.slice(1)],
}
}
// Otherwise wrap everything: Content tab + SEO tab. Sidebar fields (address,
// noindex…) stay outside the tabs, so they still show in the sidebar.
const inSidebar = (f: Field) => (f as { admin?: { position?: string } }).admin?.position === 'sidebar'
const tabs: TabsField = {
type: 'tabs',
tabs: [
{ fields: otherFields.filter((f) => !inSidebar(f)), label: CONTENT_TAB },
{ fields: [withoutHeading(metaField)], label: SEO_TAB },
],
}
return {
...collection,
fields: [tabs, ...otherFields.filter(inSidebar)],
}
}),
}
}