Files
payload/packages/plugin-seo/src/fields/MetaDescription/MetaDescriptionComponent.tsx
Jacob Fletcher 21599b87f5 fix(ui): stale paths on custom components within rows (#11973)
When server rendering custom components within form state, those
components receive a path that is correct at render time, but
potentially stale after manipulating array and blocks rows. This causes
the field to briefly render incorrect values while the form state
request is in flight.

The reason for this is that paths are passed as a prop statically into
those components. Then when we manipulate rows, form state is modified,
potentially changing field paths. The component's `path` prop, however,
hasn't changed. This means it temporarily points to the wrong field in
form state, rendering the data of another row until the server responds
with a freshly rendered component.

This is not an issue with default Payload fields as they are rendered on
the client and can be passed dynamic props.

This is only an issue within custom server components, including rich
text fields which are treated as custom components. Since they are
rendered on the server and passed to the client, props are inaccessible
after render.

The fix for this is to provide paths dynamically through context. This
way as we make changes to form state, there is a mechanism in which
server components can receive the updated path without waiting on its
props to update.
2025-04-15 15:23:51 -04:00

209 lines
5.4 KiB
TypeScript

'use client'
import type { FieldType } from '@payloadcms/ui'
import type { TextareaFieldClientProps } from 'payload'
import {
FieldLabel,
TextareaInput,
useConfig,
useDocumentInfo,
useField,
useForm,
useLocale,
useTranslation,
} from '@payloadcms/ui'
import { reduceToSerializableFields } from '@payloadcms/ui/shared'
import React, { useCallback } from 'react'
import type { PluginSEOTranslationKeys, PluginSEOTranslations } from '../../translations/index.js'
import type { GenerateDescription } from '../../types.js'
import { defaults } from '../../defaults.js'
import { LengthIndicator } from '../../ui/LengthIndicator.js'
const { maxLength: maxLengthDefault, minLength: minLengthDefault } = defaults.description
type MetaDescriptionProps = {
readonly hasGenerateDescriptionFn: boolean
} & TextareaFieldClientProps
export const MetaDescriptionComponent: React.FC<MetaDescriptionProps> = (props) => {
const {
field: {
label,
localized,
maxLength: maxLengthFromProps,
minLength: minLengthFromProps,
required,
},
hasGenerateDescriptionFn,
readOnly,
} = props
const {
config: {
routes: { api },
serverURL,
},
} = useConfig()
const { t } = useTranslation<PluginSEOTranslations, PluginSEOTranslationKeys>()
const locale = useLocale()
const { getData } = useForm()
const docInfo = useDocumentInfo()
const maxLength = maxLengthFromProps || maxLengthDefault
const minLength = minLengthFromProps || minLengthDefault
const {
customComponents: { AfterInput, BeforeInput, Label } = {},
errorMessage,
path,
setValue,
showError,
value,
}: FieldType<string> = useField()
const regenerateDescription = useCallback(async () => {
if (!hasGenerateDescriptionFn) {
return
}
const endpoint = `${serverURL}${api}/plugin-seo/generate-description`
const genDescriptionResponse = await fetch(endpoint, {
body: JSON.stringify({
id: docInfo.id,
collectionSlug: docInfo.collectionSlug,
doc: getData(),
docPermissions: docInfo.docPermissions,
globalSlug: docInfo.globalSlug,
hasPublishPermission: docInfo.hasPublishPermission,
hasSavePermission: docInfo.hasSavePermission,
initialData: docInfo.initialData,
initialState: reduceToSerializableFields(docInfo.initialState ?? {}),
locale: typeof locale === 'object' ? locale?.code : locale,
title: docInfo.title,
} satisfies Omit<
Parameters<GenerateDescription>[0],
'collectionConfig' | 'globalConfig' | 'hasPublishedDoc' | 'req' | 'versionCount'
>),
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
})
const { result: generatedDescription } = await genDescriptionResponse.json()
setValue(generatedDescription || '')
}, [
hasGenerateDescriptionFn,
serverURL,
api,
docInfo.id,
docInfo.collectionSlug,
docInfo.docPermissions,
docInfo.globalSlug,
docInfo.hasPublishPermission,
docInfo.hasSavePermission,
docInfo.initialData,
docInfo.initialState,
docInfo.title,
getData,
locale,
setValue,
])
return (
<div
style={{
marginBottom: '20px',
}}
>
<div
style={{
marginBottom: '5px',
position: 'relative',
}}
>
<div className="plugin-seo__field">
{Label ?? (
<FieldLabel label={label} localized={localized} path={path} required={required} />
)}
{hasGenerateDescriptionFn && (
<React.Fragment>
&nbsp; &mdash; &nbsp;
<button
disabled={readOnly}
onClick={() => {
void regenerateDescription()
}}
style={{
background: 'none',
backgroundColor: 'transparent',
border: 'none',
color: 'currentcolor',
cursor: 'pointer',
padding: 0,
textDecoration: 'underline',
}}
type="button"
>
{t('plugin-seo:autoGenerate')}
</button>
</React.Fragment>
)}
</div>
<div
style={{
color: '#9A9A9A',
}}
>
{t('plugin-seo:lengthTipDescription', { maxLength, minLength })}
<a
href="https://developers.google.com/search/docs/advanced/appearance/snippet#meta-descriptions"
rel="noopener noreferrer"
target="_blank"
>
{t('plugin-seo:bestPractices')}
</a>
</div>
</div>
<div
style={{
marginBottom: '10px',
position: 'relative',
}}
>
<TextareaInput
AfterInput={AfterInput}
BeforeInput={BeforeInput}
Error={errorMessage}
onChange={setValue}
path={path}
readOnly={readOnly}
required={required}
showError={showError}
style={{
marginBottom: 0,
}}
value={value}
/>
</div>
<div
style={{
alignItems: 'center',
display: 'flex',
width: '100%',
}}
>
<LengthIndicator maxLength={maxLength} minLength={minLength} text={value} />
</div>
</div>
)
}