Files
websitedev/proto/src/globals/fields/ctaLink.ts
T
Do SikiandClaude Opus 5.5 ca4d447d73 feat(web): open every external link in a new tab, derived from the URL (MITHOME-124)
General rule: links to another host open in a new tab with
rel="noopener noreferrer"; internal paths, mailto: and tel: stay in the
same tab. Behaviour was inconsistent before — the home Webmail button
opened in the same tab while the other webmail links did not, and the
CTA and header relied on a manual "external" switch.

src/lib/external-link.ts decides from the URL (externalLinkProps for
JSX, openExternalLinksInNewTab for CMS-provided HTML in About paragraphs,
legal pages and the GDPR label). All link sites use it; the manual
"external" CTA checkbox and NavigationItem flag are removed so the rule
cannot be bypassed. Rule documented in architecture.md and CLAUDE.md;
staging smoke SMOKE-07 checks every external homepage link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:00:48 +02:00

65 lines
2.2 KiB
TypeScript

import type { GroupField } from 'payload'
import { CTA_PAGES, type CtaPage } from '../../lib/cta-link'
const PAGE_LABELS: Record<CtaPage, string> = {
home: 'Főoldal',
about: 'Rólunk',
services: 'Szolgáltatások',
contact: 'Kapcsolat',
privacy: 'Adatvédelem',
terms: 'Felhasználási feltételek',
}
type Sibling = { linkType?: string } | undefined
const isPageLink = (sibling: Sibling) => (sibling?.linkType ?? 'page') === 'page'
/**
* MITHOME-123: CTA button = localized text + either an internal page
* (resolved per locale by src/lib/cta-link.ts) or an external URL.
*
* WHY custom `validate` instead of `required`: `page` and `href` are only
* required for their own link type; the other one is hidden and empty.
* WHY no "open in new tab" switch: external links always open in a new
* tab, decided from the URL (MITHOME-124, src/lib/external-link.ts).
* DECISION: `href` stays localized (MITHOME-111) — an external target may
* differ per language; `linkType`/`page` are shared across locales.
*/
export function ctaLinkGroup(name: string): GroupField {
return {
name,
type: 'group',
fields: [
{ name: 'text', type: 'text', required: true, localized: true },
{
name: 'linkType',
type: 'radio',
required: true,
defaultValue: 'page',
options: [
{ label: 'Belső oldal', value: 'page' },
{ label: 'Külső URL', value: 'url' },
],
admin: { layout: 'horizontal' },
},
{
name: 'page',
type: 'select',
options: CTA_PAGES.map((value) => ({ label: PAGE_LABELS[value], value })),
admin: { condition: (_data, sibling) => isPageLink(sibling) },
validate: (value: unknown, { siblingData }: { siblingData: Sibling }) =>
!isPageLink(siblingData) || Boolean(value) || 'Válassz oldalt.',
},
{
name: 'href',
type: 'text',
localized: true,
admin: { condition: (_data, sibling) => !isPageLink(sibling) },
validate: (value: unknown, { siblingData }: { siblingData: Sibling }) =>
isPageLink(siblingData) || Boolean(value) || 'Add meg az URL-t.',
},
],
}
}