| 1 |
/** |
| 2 |
* Internal dependencies |
| 3 |
*/ |
| 4 |
import { Metric } from './constants'; |
| 5 |
import { formatToImpreciseNumber } from './number'; |
| 6 |
|
| 7 |
/** |
| 8 |
* WordPress dependencies |
| 9 |
*/ |
| 10 |
import { __ } from '@wordpress/i18n'; |
| 11 |
|
| 12 |
/** |
| 13 |
* Defines the data structure of Posts. |
| 14 |
* |
| 15 |
* @since 3.7.0 |
| 16 |
* @since 3.11.0 Renamed to PostData from TopPostData and moved into post.tsx. |
| 17 |
*/ |
| 18 |
export interface PostData { |
| 19 |
author: string; |
| 20 |
avgEngaged: string; |
| 21 |
dashUrl: string; |
| 22 |
date: string; |
| 23 |
id: number; |
| 24 |
postId: number; |
| 25 |
thumbnailUrl: string; |
| 26 |
title: string; |
| 27 |
url: string; |
| 28 |
rawUrl: string; |
| 29 |
views: number; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Defines a generic props structure for post list items. |
| 34 |
* |
| 35 |
* @since 3.11.0 |
| 36 |
*/ |
| 37 |
export interface PostListItemProps { |
| 38 |
metric: Metric; |
| 39 |
post: PostData; |
| 40 |
postContent?: string; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Defines the props structure for PostListItemMetric. |
| 45 |
* |
| 46 |
* @since 3.10.0 |
| 47 |
*/ |
| 48 |
interface PostListItemMetricProps extends PostListItemProps { |
| 49 |
avgEngagedIcon?: React.JSX.Element; |
| 50 |
viewsIcon?: React.JSX.Element; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns a span element with the desired metric data for the Post. |
| 55 |
* |
| 56 |
* Currently, only the `views` and `avg_engaged` metrics are supported. |
| 57 |
* |
| 58 |
* @since 3.10.0 |
| 59 |
* @since 3.11.0 Moved into to post.tsx. |
| 60 |
* |
| 61 |
* @param {PostListItemMetricProps} props The component's props. |
| 62 |
* |
| 63 |
* @return {import('react').JSX.Element} The resulting JSX Element. |
| 64 |
*/ |
| 65 |
export function PostListItemMetric( |
| 66 |
{ metric, post, avgEngagedIcon, viewsIcon }: Readonly<PostListItemMetricProps> |
| 67 |
): React.JSX.Element { |
| 68 |
if ( 'views' === metric ) { |
| 69 |
return ( |
| 70 |
<span className="parsely-post-metric-data"> |
| 71 |
<span className="screen-reader-text"> |
| 72 |
{ __( 'Number of Views', 'wp-parsely' ) } |
| 73 |
</span> |
| 74 |
{ viewsIcon }{ formatToImpreciseNumber( post.views.toString() ) } |
| 75 |
</span> |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
if ( 'avg_engaged' === metric ) { |
| 80 |
return ( |
| 81 |
<span className="parsely-post-metric-data"> |
| 82 |
<span className="screen-reader-text"> |
| 83 |
{ __( 'Average Time', 'wp-parsely' ) } |
| 84 |
</span> |
| 85 |
{ avgEngagedIcon }{ post.avgEngaged } |
| 86 |
</span> |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
return ( |
| 91 |
<span className="parsely-post-metric-data"> |
| 92 |
- |
| 93 |
</span> |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Gets edit url of the post. |
| 99 |
* |
| 100 |
* @since 3.7.0 |
| 101 |
* |
| 102 |
* @param {number} postId ID of the post. |
| 103 |
* |
| 104 |
* @return {string} Edit url of the post. |
| 105 |
*/ |
| 106 |
export function getPostEditUrl( postId: number ): string { |
| 107 |
return `/wp-admin/post.php?post=${ postId }&action=edit`; |
| 108 |
} |
| 109 |
|