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

refactor: remove as casts from DamImageBlock #2911

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Reaw
Copy link

@Reaw Reaw commented Dec 10, 2024

Description

Refactor the DamImageBlock site component to remove the hard casts (as) as they are error prone and unsafe.
Simply use typescript to discriminate the union type with a sub-field of PixelImageBlockData

Example

  • I have verified if my change requires an example

Changeset

  • I have verified if my change requires a changeset

Related tasks and documents

none

@auto-assign auto-assign bot requested a review from johnnyomair December 10, 2024 14:21
Copy link
Collaborator

@johnnyomair johnnyomair left a comment

Choose a reason for hiding this comment

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

A better solution would be to change the generated block types to generate something like { type: "pixelImage"; props: PixelImageBlockData } | { type: "svgImage"; props: SvgImageBlockData }

@@ -11,10 +11,10 @@ const DamImageBlock = withPreview(
return <PreviewSkeleton type="media" hasContent={false} aspectRatio={aspectRatio} />;
}

if (block.type === "pixelImage") {
return <PixelImageBlock data={block.props as PixelImageBlockData} aspectRatio={aspectRatio} {...imageProps} />;
if (block.type === "pixelImage" && "cropArea" in block.props) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This if presumably doesn't work if there's no crop area set in the PixelImageBlock, because then cropArea will be undefined: https://github.com/vivid-planet/comet/blob/main/packages/api/cms-api/src/dam/blocks/pixel-image-block-transformer.service.ts#L75

Copy link
Author

Choose a reason for hiding this comment

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

You're right, I thought I checked, but it's indeed an optional field what about simply using urlTemplate which is required instead?
I think there is no need for refactoring the object's structure, if the issue can be resolved by ts :)

Copy link
Author

Choose a reason for hiding this comment

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

if (block.type === "pixelImage" && "urlTemplate" in block.props)

Copy link
Collaborator

Choose a reason for hiding this comment

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

urlTemplate is probably typed wrong. I believe it can be undefined. At least the block transformer service indicates that it is.

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

Successfully merging this pull request may close these issues.

2 participants