File size: 6,978 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { Badge, Button } from '@automattic/components';
import { formatNumberCompact } from '@automattic/number-formatters';
import { useTranslate } from 'i18n-calypso';
import { useSelector } from 'react-redux';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import {
	useMarketplaceReviewsQuery,
	useMarketplaceReviewsStatsQuery,
} from 'calypso/data/marketplace/use-marketplace-reviews';
import { gaRecordEvent } from 'calypso/lib/analytics/ga';
import { preventWidows } from 'calypso/lib/formatting';
import PluginIcon from 'calypso/my-sites/plugins/plugin-icon/plugin-icon';
import { useLocalizedPlugins, formatPluginRating } from 'calypso/my-sites/plugins/utils';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import usePluginVersionInfo from '../plugin-management-v2/hooks/use-plugin-version-info';

import './style.scss';

const PluginDetailsHeader = ( {
	plugin,
	isPlaceholder,
	isJetpackCloud,
	onReviewsClick = () => {},
	isMarketplaceProduct,
} ) => {
	const moment = useLocalizedMoment();
	const translate = useTranslate();
	const { localizePath } = useLocalizedPlugins();

	const selectedSite = useSelector( getSelectedSite );

	const { currentVersionsRange } = usePluginVersionInfo( plugin, selectedSite?.ID );

	const { data: reviewsStats } = useMarketplaceReviewsStatsQuery( {
		productType: 'plugin',
		slug: plugin.slug,
	} );

	const { data: marketplaceReviews } = useMarketplaceReviewsQuery( {
		productType: 'plugin',
		slug: plugin.slug,
	} );

	// Rating can be a valid number, 0 or null, discard undefined for easier comparison
	let rating = plugin.rating ?? null;
	if ( isMarketplaceProduct ) {
		rating = reviewsStats?.ratings_average ? ( reviewsStats.ratings_average * 100 ) / 5 : 0;
	}

	if ( isPlaceholder ) {
		return <PluginDetailsHeaderPlaceholder />;
	}

	const getMarketPlacePluginReviewsLink = () => {
		const numberOfReviews = marketplaceReviews?.length || 0;

		return (
			<Button
				borderless
				className="plugin-details-header__number-reviews-link is-link"
				onClick={ onReviewsClick }
			>
				{ numberOfReviews > 0 &&
					translate( '%(numberOfReviews)d review', '%(numberOfReviews)d reviews', {
						count: numberOfReviews,
						args: {
							numberOfReviews,
						},
					} ) }
				{ numberOfReviews === 0 && translate( 'Add a review' ) }
			</Button>
		);
	};

	const getPluginReviewsLink = () => {
		if ( plugin.num_ratings > 0 ) {
			return null;
		}

		const onClickPluginRatingsLink = () => {
			gaRecordEvent( 'Plugins', 'Clicked Add a review link', 'Plugin Name', plugin.slug );
		};

		return (
			<a
				href={ `https://wordpress.org/support/plugin/${ plugin.slug }/reviews` }
				onClick={ onClickPluginRatingsLink }
				target="_blank"
				rel="noopener noreferrer"
			>
				{ translate( 'Add a review' ) }
			</a>
		);
	};

	return (
		<div className="plugin-details-header__container">
			<div className="plugin-details-header__main-info">
				<PluginIcon className="plugin-details-header__icon" image={ plugin.icon } />
				<div className="plugin-details-header__title-container">
					<h1 className="plugin-details-header__name">{ plugin.name }</h1>
					<div className="plugin-details-header__subtitle">
						<span className="plugin-details-header__author">
							{ isJetpackCloud
								? plugin.author_name
								: translate( 'By {{author/}}', {
										components: {
											author: (
												<a
													href={ localizePath(
														`/plugins/${ selectedSite?.slug || '' }?s=developer:"${ getPluginAuthor(
															plugin
														) }"`
													) }
												>
													{ plugin.author_name }
												</a>
											),
										},
								  } ) }
						</span>

						<span className="plugin-details-header__subtitle-separator">·</span>
					</div>
				</div>
			</div>
			<div className="plugin-details-header__description">
				{ preventWidows( plugin.short_description || plugin.description ) }
			</div>
			{ ! isJetpackCloud && <Tags plugin={ plugin } /> }
			<div className="plugin-details-header__additional-info">
				{ /* We want to accept rating 0, which means no rating for Marketplace products */ }
				{ rating !== null && (
					<div className="plugin-details-header__info">
						<div className="plugin-details-header__info-title">{ translate( 'Rating' ) }</div>
						<div className="plugin-details-header__info-value">
							{ rating !== 0 && <div>{ `${ formatPluginRating( rating, true ) }/5` }</div> }
							{ isMarketplaceProduct ? getMarketPlacePluginReviewsLink() : getPluginReviewsLink() }
						</div>
					</div>
				) }
				<div className="plugin-details-header__info">
					<div className="plugin-details-header__info-title">{ translate( 'Version' ) }</div>
					<div className="plugin-details-header__info-value">
						{ /* Show the default version if plugin is not installed */ }
						{ currentVersionsRange?.min || plugin.version }
						{ currentVersionsRange?.max && ` - ${ currentVersionsRange.max }` }
					</div>
				</div>
				{ Boolean( plugin.active_installs ) && (
					<div className="plugin-details-header__info">
						<div className="plugin-details-header__info-title">
							{ translate( 'Active installations' ) }
						</div>
						<div className="plugin-details-header__info-value">
							{ formatNumberCompact( plugin.active_installs ) }
						</div>
					</div>
				) }
				<div className="plugin-details-header__info">
					<div className="plugin-details-header__info-title">{ translate( 'Last updated' ) }</div>
					<div className="plugin-details-header__info-value">
						{ moment.utc( plugin.last_updated, 'YYYY-MM-DD hh:mma' ).format( 'MMM D, YYYY' ) }
					</div>
				</div>
			</div>
		</div>
	);
};

const LIMIT_OF_TAGS = 3;
function Tags( { plugin } ) {
	const selectedSite = useSelector( getSelectedSite );
	const { localizePath } = useLocalizedPlugins();

	if ( ! plugin?.tags ) {
		return null;
	}

	const tagKeys = Object.keys( plugin.tags || {} ).slice( 0, LIMIT_OF_TAGS );

	return (
		<div className="plugin-details-header__tag-badge-container">
			{ tagKeys.map( ( tagKey ) => (
				<a
					key={ `badge-${ tagKey.replace( ' ', '' ) }` }
					className="plugin-details-header__tag-badge"
					href={ localizePath( `/plugins/browse/${ tagKey }/${ selectedSite?.slug || '' }` ) }
				>
					<Badge type="info">{ plugin.tags[ tagKey ] }</Badge>
				</a>
			) ) }
		</div>
	);
}

function PluginDetailsHeaderPlaceholder() {
	return (
		<div className="plugin-details-header__wrapper is-placeholder">
			<div className="plugin-details-header__tags">...</div>
			<div className="plugin-details-header__container">
				<h1 className="plugin-details-header__name">...</h1>
				<div className="plugin-details-header__description">...</div>
				<div className="plugin-details-header__additional-info">...</div>
			</div>
		</div>
	);
}

function getPluginAuthor( plugin ) {
	return plugin.author_name;
}

export default PluginDetailsHeader;