import React from 'react' import classNames from 'classnames' import { GridItem, SerializableRecord } from '@interval/sdk/dist/ioSchema' import DropdownMenu from '../DropdownMenu' import useImageLoaded from '~/utils/useImageLoaded' import MoreIcon from '~/icons/compiled/More' import useRenderContext from '../RenderContext' import { getCurrentPath } from '~/utils/url' import { useIsomorphicLink } from '~/utils/useIsomorphicLocation' interface GridItemProps { label?: string | null // @deprecated in favor of `label` title?: string | null description?: string | null image?: GridItem['image'] | null menu?: GridItem['menu'] | null route?: string params?: SerializableRecord url?: string } const LinkBlock = ({ to, state, url, children, className = '', }: { children: React.ReactNode state?: any to?: string | null url?: string className?: string }) => { const baseClassName = 'flex group' const Link = useIsomorphicLink() if (to) { return ( {children} ) } return ( {children} ) } const TextBlock = ({ children, className = '', }: { children: React.ReactNode className?: string }) => { return
{children}
} const Image = (props: GridItemProps) => { const { imageLoaded, imgRef } = useImageLoaded() if (!props.image) return null const { fit = 'cover', aspectRatio = 16 / 9 } = props.image ?? {} if (!props.image.url) { return (
) } return ( {props.image.alt ) } const Metadata = ( props: GridItemProps & { to?: string | null url?: string } ) => { const label = props.label ?? props.title const Link = useIsomorphicLink() return (
{label && (

{props.to ? ( {label} ) : props.url ? ( {label} ) : ( <>{label} )}

)} {props.description && (

{props.description}

)}
) } function Menu(props: GridItemProps) { const { getActionUrl } = useRenderContext() if (!props.menu) return null const options = props.menu.map(item => { if (!('route' in item)) { return item } return { ...item, path: getActionUrl({ base: window.location.origin, slug: item.route, params: item.params, }), } }) return ( ) } const MediaGridItem = React.forwardRef( (props: GridItemProps, ref: React.Ref) => { const isLinked = props.url || props.route const hasTextContent = props.label || props.title || props.description const { getActionUrl } = useRenderContext() const actionUrl = props.route ? getActionUrl({ base: window.location.origin, slug: props.route, params: props.params, }) : null const Container = isLinked ? LinkBlock : TextBlock if (!props.image) { return (
{props.menu && (
)}
) } return (
{hasTextContent && (
{props.menu && (
)}
)} {!hasTextContent && props.menu && (
)}
) } ) MediaGridItem.displayName = 'MediaGridItem' export default MediaGridItem