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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions demo/site/src/blocks/DamImageBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { PixelImageBlock, PreviewSkeleton, PropsWithData, SvgImageBlock, withPreview } from "@comet/cms-site";
import { DamImageBlockData, PixelImageBlockData, SvgImageBlockData } from "@src/blocks.generated";
import { DamImageBlockData } from "@src/blocks.generated";
import { ImageProps } from "next/image";

type Props = PropsWithData<DamImageBlockData> & Omit<ImageProps, "src" | "width" | "height" | "alt"> & { aspectRatio: string | number | "inherit" };
Expand All @@ -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.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, reopening this conversation, but how can urlTemplate be undefined? It seems like it is everywhere required, the PixelImageBlock requires it, because it renders a <NextImage /> where it is used as src prop
cf screenshot:

image

If the urlTemplate is somehow undefined, the image will be broken anyways, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

PixelImageBlock has an if to verify that NextImage is only rendered if a DAM file is present: https://github.com/vivid-planet/comet/blob/main/packages/site/cms-site/src/blocks/PixelImageBlock.tsx#L18

If the DAM file is present, urlTemplate will be present as well. We could probably change the structure of the block's data to reflect this.

It should be typed as nullable here: https://github.com/vivid-planet/comet/blob/main/packages/api/cms-api/src/dam/blocks/pixel-image.block.ts#L175

return <PixelImageBlock data={block.props} aspectRatio={aspectRatio} {...imageProps} />;
} else if (block.type === "svgImage") {
return <SvgImageBlock data={block.props as SvgImageBlockData} />;
return <SvgImageBlock data={block.props} />;
} else {
if (process.env.NODE_ENV === "development") {
return (
Expand Down
Loading