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 13, 2024
1 parent 9d075f4 commit 4742128
Showing 1 changed file with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {type SanityImageSource} from '@sanity/image-url/lib/types/types'
import {type ImageUrlFitMode} from '@sanity/types'
import {
type ComponentType,
createElement,
type ElementType,
isValidElement,
memo,
type ReactElement,
type ReactNode,
useCallback,
Expand Down Expand Up @@ -37,8 +37,10 @@ export interface SanityDefaultPreviewProps extends Omit<PreviewProps, 'renderDef
* Used in cases where no custom preview component is provided
* @internal
* */
export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactElement {
const {icon, layout, media: mediaProp, imageUrl, title, tooltip, ...restProps} = props
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)
const imageBuilder = useMemo(() => imageUrlBuilder(client), [client])
Expand All @@ -50,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 @@ -61,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 All @@ -74,11 +78,11 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle
)

const renderIcon = useCallback(() => {
return createElement(icon || FallbackIcon)
}, [icon])
return Icon ? <Icon /> : <FallbackIcon />
}, [Icon])

const media = useMemo(() => {
if (icon === false) {
if (Icon === false) {
// Explicitly disabled
return false
}
Expand Down Expand Up @@ -108,7 +112,7 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle

// Render fallback icon
return renderIcon
}, [icon, imageUrl, mediaProp, renderIcon, renderMedia, title])
}, [Icon, imageUrl, mediaProp, renderIcon, renderMedia, title])

const previewProps: Omit<PreviewProps, 'renderDefault'> = useMemo(
() => ({
Expand All @@ -120,12 +124,11 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle
[media, restProps, title],
)

const layoutComponent = _previewComponents[layout || 'default']
const LayoutComponent = _previewComponents[layout || 'default'] as ComponentType<
Omit<PreviewProps, 'renderDefault'>
>

const children = createElement(
layoutComponent as ComponentType<Omit<PreviewProps, 'renderDefault'>>,
previewProps,
)
const children = <LayoutComponent {...previewProps} />

if (tooltip) {
return (
Expand All @@ -142,4 +145,5 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle
}

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

0 comments on commit 4742128

Please sign in to comment.