Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(richtext-lexical): incorrect UploadData types #11288

Merged
merged 1 commit into from
Feb 19, 2025
Merged

Conversation

AlessioGr
Copy link
Member

This PR fixes the UploadData type that was weakened in a previous PR, causing a breaking change. It also improves the newly added UploadDataImproved type by bringing back its support for generated types and using the UploadCollectionSlug type helper to restrict collection slugs to upload-enabled collections.

@AlessioGr AlessioGr enabled auto-merge (squash) February 19, 2025 20:03
@AlessioGr AlessioGr merged commit 2612756 into main Feb 19, 2025
74 checks passed
@AlessioGr AlessioGr deleted the fix/uploaddata-type branch February 19, 2025 20:22
@@ -8,34 +8,29 @@ export const UploadJSXConverter: JSXConverters<SerializedUploadNode> = {
upload: ({ node }) => {
// TO-DO (v4): SerializedUploadNode should use UploadData_P4
const uploadDocument = node as UploadDataImproved
if (typeof uploadDocument.value !== 'object') {
if (typeof uploadDocument?.value !== 'object') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess ts was asking for optional chaining here? any reason why it is needed now? afaik value should be defined

Comment on lines -40 to +47
value: (FileData & TypeWithID) | number | string
}
[TCollectionSlug in UploadCollectionSlug]: {
fields: TUploadExtraFieldsData
// Every lexical node that has sub-fields needs to have a unique ID. This is the ID of this upload node, not the ID of the linked upload document
id: string
relationTo: TCollectionSlug
// Value can be just the document ID, or the full, populated document
value: number | string | TypedUploadCollection[TCollectionSlug]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think TypedUploadCollection[TCollectionSlug] is significantly less precise than (FileData & TypeWithID).

Before this PR:
image

After this PR:
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only be the case in our monorepo though - it will be more precise in a real project

Comment on lines +15 to 34
const value = uploadDocument.value
const url = value.url

/**
* If the upload is not an image, return a link to the upload
*/
if (!uploadDocument.value.mimeType.startsWith('image')) {
if (!value.mimeType.startsWith('image')) {
return (
<a href={url} rel="noopener noreferrer">
{uploadDocument.value.filename}
{value.filename}
</a>
)
}

/**
* If the upload is a simple image with no different sizes, return a simple img tag
*/
if (!Object.keys(uploadDocument.value.sizes).length) {
return (
<img
alt={uploadDocument.value.filename}
height={uploadDocument.value.height}
src={url}
width={uploadDocument.value.width}
/>
)
if (!Object.keys(value.sizes).length) {
return <img alt={value.filename} height={value.height} src={url} width={value.width} />
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now almost all things derived from value are "any"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a type assertion in https://github.com/payloadcms/payload/pull/11292/files.

We could have it fall back to FileData & TypeWithID, but I don't know if we should. If it has to fall back, that means the collection slug likely doesn't exist, or it isn't an uploads-enabled collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants