/** * External dependencies */ import clsx from 'clsx'; /** * WordPress dependencies */ import { VisuallyHidden } from '@wordpress/components'; import { useRef, useContext, useMemo } from '@wordpress/element'; import { useRegistry } from '@wordpress/data'; import { useViewportMatch } from '@wordpress/compose'; import { Stack } from '@wordpress/ui'; /** * Internal dependencies */ import ItemActions, { PrimaryActions } from '../../dataviews-item-actions'; import DataViewsContext from '../../dataviews-context'; import { ItemClickWrapper } from '../utils/item-click-wrapper'; import type { NormalizedField, ViewActivityProps } from '../../../types'; function ActivityItem< Item >( props: ViewActivityProps< Item > & { item: Item; mediaField?: NormalizedField< Item >; titleField?: NormalizedField< Item >; descriptionField?: NormalizedField< Item >; otherFields: NormalizedField< Item >[]; posinset?: number; } ) { const { view, actions, item, titleField, mediaField, descriptionField, otherFields, posinset, onClickItem, renderItemLink, isItemClickable, } = props; const { showTitle = true, showMedia = true, showDescription = true, infiniteScrollEnabled, } = view; const itemRef = useRef< HTMLDivElement >( null ); const registry = useRegistry(); const { paginationInfo } = useContext( DataViewsContext ); const { primaryActions, eligibleActions } = useMemo( () => { // If an action is eligible for all items, doesn't need // to provide the `isEligible` function. const _eligibleActions = actions.filter( ( action ) => ! action.isEligible || action.isEligible( item ) ); const _primaryActions = _eligibleActions.filter( ( action ) => action.isPrimary ); return { primaryActions: _primaryActions, eligibleActions: _eligibleActions, }; }, [ actions, item ] ); const isMobileViewport = useViewportMatch( 'medium', '<' ); const density = view.layout?.density ?? 'balanced'; const mediaContent = showMedia && density !== 'compact' && mediaField?.render ? ( ) : null; const renderedMediaField = (
{ mediaContent || (
); const renderedTitleField = showTitle && titleField?.render ? ( ) : null; const verticalGap = useMemo( () => { switch ( density ) { case 'comfortable': return 'md'; default: return 'sm'; } }, [ density ] ); return (
{ renderedMediaField } { renderedTitleField && ( { renderedTitleField } ) } { showDescription && descriptionField && (
) }
{ otherFields.map( ( field ) => (
{ field.label }
) ) }
{ !! primaryActions?.length && ( ) }
{ ( primaryActions.length < eligibleActions.length || // Since we hide primary actions on mobile, we need to show the menu // there if there are any actions at all. ( isMobileViewport && // At the same time, only show the menu if there are actions to show. eligibleActions.length > 0 ) ) && (
) }
); } export default ActivityItem;