'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) 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) : 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[] | 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 (
{warning.text}
) }