Skip to content

Commit

Permalink
fix: improve SanityDefaultPreview memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 18, 2024
1 parent 21c9fab commit efc4a70
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type ComponentType,
type ElementType,
isValidElement,
memo,
type ReactElement,
type ReactNode,
useCallback,
Expand Down Expand Up @@ -36,7 +37,9 @@ export interface SanityDefaultPreviewProps extends Omit<PreviewProps, 'renderDef
* Used in cases where no custom preview component is provided
* @internal
* */
export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactElement {
export const SanityDefaultPreview = memo(function SanityDefaultPreview(
props: SanityDefaultPreviewProps,
): ReactElement {
const {icon: Icon, layout, media: mediaProp, imageUrl, title, tooltip, ...restProps} = props

const client = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
Expand All @@ -49,6 +52,8 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle
dimensions: {width?: number; height?: number; fit: ImageUrlFitMode; dpr?: number}
}) => {
const {dimensions} = options
const width = dimensions.width || 100
const height = dimensions.height || 100

// Handle sanity image
return (
Expand All @@ -60,8 +65,8 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle
.image(
mediaProp as SanityImageSource /*will only enter this code path if it's compatible*/,
)
.width(dimensions.width || 100)
.height(dimensions.height || 100)
.width(width)
.height(height)
.fit(dimensions.fit)
.dpr(dimensions.dpr || 1)
.url() || ''
Expand Down Expand Up @@ -140,4 +145,5 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle
}

return children
}
})
SanityDefaultPreview.displayName = 'Memo(SanityDefaultPreview)'

0 comments on commit efc4a70

Please sign in to comment.