/* eslint-disable no-nested-ternary */ import classNames from 'classnames'; import { ChevronRight } from 'lucide-react'; import moment from 'moment'; import React, { useState } from 'react'; import { useSelect } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; import { __, sprintf } from '@wordpress/i18n'; import { Duration, Post, HistogramDiffs, compactMetric, getConversionRateLift, trackEvent } from '../../utils/admin'; import ImageGroup from './ImageGroup'; import SparkChart from './SparkChart'; import StandaloneBlockPreview from './StandaloneBlockPreview'; import { Button } from '@/components/ui/button'; import { Spinner } from '@/components/ui/spinner'; const ListItem = function ( props: { listId: string, item: Post, maxViewsPerUrl?: number, histogramDiffs?: HistogramDiffs, period?: Duration, onManage?: Function, isNested?: boolean, actions?: { [ k: string ]: Function }, } ) { const { listId, item: post, maxViewsPerUrl, histogramDiffs = {}, period, onManage, isNested, actions, } = props; const [ isExpanded, setIsExpanded ] = useState( false ); const [ canFetch, setCanFetch ] = useState( false ); let lift: number | null = null; let change: number | null = null; if ( post.lift ) { lift = getConversionRateLift( post.lift.fallback, post.lift.personalized ); } if ( histogramDiffs[ post.id ] && histogramDiffs[ post.id ].previous.views > 0 ) { change = ( ( histogramDiffs[ post.id ].current.views - histogramDiffs[ post.id ].previous.views ) / histogramDiffs[ post.id ].previous.views ) * 100; } const isNestable = post.type.name === 'broadcast'; const nested = useSelect( select => { if ( ! canFetch || ! post.blocks?.length ) { return []; } return select( 'accelerate' ).getPosts( { type: 'wp_block', include: post.blocks, }, false ); }, [ post.blocks, canFetch ] ); const onEdit = ( e: React.MouseEvent ) => { trackEvent( 'list_action', { list: listId, action: 'edit', type: post.type, } ); if ( onManage ) { e.preventDefault(); onManage( post ); } }; return ( <> { isNestable && (
) } { isNested && ( ) }
{ isNestable ? ( post.blocks.length > 0 ) ? ( ) : (
) : post.rawContent ? ( post.variants > 1 ? (
{ post.variants > 2 && ( +{ post.variants - 2 } ) } { /* Back card - variant B */ } { /* Front card - variant A */ }
) : ( ) ) : post.editUrl && (
) }
{ if ( onManage ) { onEdit( e ); } else if ( post.editUrl ) { trackEvent( 'list_navigate', { list: listId, type: post.type, } ); } } }> { decodeEntities( post.title ) }
{ decodeEntities( post.subtype?.label || post.type.label ) }
{ post.parent && (
{ post.parent.title }
) }
{ moment.utc( post.date ).fromNow() }
{ ! isNested ? (
{ new Intl.NumberFormat().format( histogramDiffs[ post.id ]?.current?.views || post.views ) }
= 0 ? 'pos' : 'neg' }` } title={ __( 'Comparison to previous period', 'altis' ) } > { !! change && ! isNaN( change ) && ( change > 0 ? '↑' : '↓' ) }   { !! change && ! isNaN( change ) && compactMetric( parseFloat( Math.abs( change ).toFixed( 1 ) ), '%' ) }
) : <>  }
{ ! actions && post.editUrl && ( <> { ' ' } { __( 'Edit', 'altis' ) } ) } { post.url && ( <> { ' ' } trackEvent( 'list_action', { list: listId, action: 'view', type: post.type, } ) }> { __( 'View', 'altis' ) } ) } { actions && Object.keys( actions ).map( label => ( { e.preventDefault(); actions[ label ]( post ); } } > { label } ) ) }
{ decodeEntities( post.author.name ) }
{ isExpanded && ( ( post.blocks.length > 0 ) ? ( ( nested ? nested.map( nestedBlock => ( ) ): (
{ __( 'Loading nested blocks...', 'altis' ) }
) ) ) : ( { __( 'No blocks assigned to this block just yet.', 'altis' ) } { ' ' } { __( 'Add blocks', 'altis' ) } ) ) } ); }; function GlobalBlockConvertionRate( { post }: { post: Post } ) { if ( ! post.lift || ! post.subtype || post.lift.conversions / post.lift.views * 100 === 0 ) { return null; } const goalLabelMap = { click_any_link: __( 'Clickthrough', 'altis' ), engagement: __( 'Engagement', 'altis' ), submit_form: __( 'Form Submission', 'altis' ), }; return (
{ Math.round( post.lift.conversions / post.lift.views * 100 ) }%
{ ( goalLabelMap[ post.goal ] ) || '' }
); } export default ListItem;