Files
ipal-kit/src/modules/seo/MetaDescriptionCheck.tsx
T
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

120 lines
4.0 KiB
TypeScript

'use client'
import { useConfig, useDocumentInfo, useFormFields, useLocale, useTranslation } from '@payloadcms/ui'
import { useEffect, useState } from 'react'
import { iaplT } from '../admin-i18n/t.js'
export type MetaDescriptionCheckProps = {
/** An empty description is filled from the page content on save (autoFill). */
autoFill?: boolean
/** SiteSettings global slug. */
settingsSlug?: string
/** Field on SiteSettings holding the site-wide fallback description. */
siteDescriptionField?: string
/** Field shown as the name of the page that has the same description. */
titleField?: string
}
type Warning = { text: string; tone: 'info' | 'warn' }
const norm = (v: unknown) => (typeof v === 'string' ? v.trim().replace(/\s+/g, ' ') : '')
/**
* Hint under the meta description in the SEO tab. Google ignores a description
* it sees on many pages and quotes page text instead (usually the footer), so
* this flags a description that matches the site-wide fallback or another
* document in the collection. Advisory only — saving is never blocked. Length
* is already shown by plugin-seo's own counter.
*/
export function MetaDescriptionCheck({
autoFill = true,
settingsSlug = 'site-settings',
siteDescriptionField = 'siteDescription',
titleField = 'title',
}: MetaDescriptionCheckProps) {
const description = norm(useFormFields(([f]) => f['meta.description']?.value))
const { id, collectionSlug } = useDocumentInfo()
const { code: locale } = useLocale()
const {
config: {
routes: { api },
serverURL,
},
} = useConfig()
const { t } = useTranslation()
const [warning, setWarning] = useState<null | Warning>(null)
useEffect(() => {
if (!description) {
setWarning(
autoFill
? { text: iaplT(t, 'seoDescEmptyAutoFill'), tone: 'info' }
: { text: iaplT(t, 'seoDescEmpty'), tone: 'warn' },
)
return
}
if (!collectionSlug) {return}
const controller = new AbortController()
const base = `${serverURL ?? ''}${api}`
const get = async (url: string) => {
const res = await fetch(url, { credentials: 'include', signal: controller.signal })
return res.ok ? ((await res.json()) as Record<string, unknown>) : null
}
const timer = setTimeout(() => {
void (async () => {
try {
const settings = await get(`${base}/globals/${settingsSlug}?depth=0&locale=${locale}`)
if (settings && norm(settings[siteDescriptionField]) === description) {
setWarning({ text: iaplT(t, 'seoDescSameAsSite'), tone: 'warn' })
return
}
const query = new URLSearchParams({
depth: '0',
limit: '1',
locale,
'where[meta.description][equals]': description,
})
if (id !== undefined && id !== null) {query.set('where[id][not_equals]', String(id))}
const found = await get(`${base}/${collectionSlug}?${query}`)
const twin = (found?.docs as Record<string, unknown>[] | undefined)?.[0]
if (twin) {
const name = norm(twin[titleField]) || String(twin.id)
setWarning({ text: iaplT(t, 'seoDescSameAs', { name }), tone: 'warn' })
return
}
setWarning(null)
} catch {
// aborted or offline — a hint is not worth an error
}
})()
}, 600)
return () => {
clearTimeout(timer)
controller.abort()
}
}, [api, autoFill, collectionSlug, description, id, locale, serverURL, settingsSlug, siteDescriptionField, t, titleField])
if (!warning) {return null}
return (
<div
role="status"
style={{
background:
warning.tone === 'warn' ? 'var(--theme-warning-50, rgba(255, 178, 36, 0.1))' : 'var(--theme-elevation-50)',
border: `1px solid ${warning.tone === 'warn' ? 'var(--theme-warning-400, #ffb224)' : 'var(--theme-elevation-150)'}`,
borderRadius: 4,
fontSize: '0.85rem',
marginBottom: '1.5rem',
padding: '0.6rem 0.9rem',
}}
>
{warning.text}
</div>
)
}