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 ;
}
const getMarketPlacePluginReviewsLink = () => {
const numberOfReviews = marketplaceReviews?.length || 0;
return (
{ numberOfReviews > 0 &&
translate( '%(numberOfReviews)d review', '%(numberOfReviews)d reviews', {
count: numberOfReviews,
args: {
numberOfReviews,
},
} ) }
{ numberOfReviews === 0 && translate( 'Add a review' ) }
);
};
const getPluginReviewsLink = () => {
if ( plugin.num_ratings > 0 ) {
return null;
}
const onClickPluginRatingsLink = () => {
gaRecordEvent( 'Plugins', 'Clicked Add a review link', 'Plugin Name', plugin.slug );
};
return (
{ translate( 'Add a review' ) }
);
};
return (
{ plugin.name }
{ isJetpackCloud
? plugin.author_name
: translate( 'By {{author/}}', {
components: {
author: (
{ plugin.author_name }
),
},
} ) }
ยท
{ preventWidows( plugin.short_description || plugin.description ) }
{ ! isJetpackCloud &&
}
{ /* We want to accept rating 0, which means no rating for Marketplace products */ }
{ rating !== null && (
{ translate( 'Rating' ) }
{ rating !== 0 &&
{ `${ formatPluginRating( rating, true ) }/5` }
}
{ isMarketplaceProduct ? getMarketPlacePluginReviewsLink() : getPluginReviewsLink() }
) }
{ translate( 'Version' ) }
{ /* Show the default version if plugin is not installed */ }
{ currentVersionsRange?.min || plugin.version }
{ currentVersionsRange?.max && ` - ${ currentVersionsRange.max }` }
{ Boolean( plugin.active_installs ) && (
{ translate( 'Active installations' ) }
{ formatNumberCompact( plugin.active_installs ) }
) }
{ translate( 'Last updated' ) }
{ moment.utc( plugin.last_updated, 'YYYY-MM-DD hh:mma' ).format( 'MMM D, YYYY' ) }
);
};
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 (
);
}
function PluginDetailsHeaderPlaceholder() {
return (
);
}
function getPluginAuthor( plugin ) {
return plugin.author_name;
}
export default PluginDetailsHeader;