File size: 5,935 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 | import { SimplifiedSegmentedControl, StatsCard } from '@automattic/components';
import { comment } from '@wordpress/icons';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import React, { useState } from 'react';
import QuerySiteStats from 'calypso/components/data/query-site-stats';
import InlineSupportLink from 'calypso/components/inline-support-link';
import { STAT_TYPE_COMMENTS } from 'calypso/my-sites/stats/constants';
import StatsInfoArea from 'calypso/my-sites/stats/features/modules/shared/stats-info-area';
import { useShouldGateStats } from 'calypso/my-sites/stats/hooks/use-should-gate-stats';
import StatsCardUpsell from 'calypso/my-sites/stats/stats-card-upsell';
import StatsListCard from 'calypso/my-sites/stats/stats-list/stats-list-card';
import { useSelector } from 'calypso/state';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import {
isRequestingSiteStatsForQuery,
getSiteStatsNormalizedData,
} from 'calypso/state/stats/lists/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import EmptyModuleCard from '../../../components/empty-module-card/empty-module-card';
import StatsCardSkeleton from '../shared/stats-card-skeleton';
import type { StatsDefaultModuleProps, StatsStateProps } from '../types';
import './styles.scss';
type CommentActionType = {
type: string;
data: boolean;
};
type CommentDataAuthors< T = string > = {
label: string;
value: T;
iconClassName: string;
icon: string;
link: string;
className: string;
actions: CommentActionType[];
};
type CommentDataPosts< T = string > = {
label: string;
value: T;
page: string;
actions: CommentActionType[];
};
type CommentData = {
authors: CommentDataAuthors[];
posts: CommentDataPosts[];
};
type HeaderToggleOptionType = {
value: string;
label: string;
};
const StatsComments: React.FC< StatsDefaultModuleProps > = ( { className } ) => {
const translate = useTranslate();
const siteId = useSelector( getSelectedSiteId ) as number;
const statType = STAT_TYPE_COMMENTS;
const moduleTitle = translate( 'Comments' );
const [ activeFilter, setActiveFilter ] = useState< string >( 'top-authors' );
const isSiteJetpackNotAtomic = useSelector( ( state ) =>
isJetpackSite( state, siteId, { treatAtomicAsJetpackSite: false } )
);
const supportContext = isSiteJetpackNotAtomic ? 'stats-comments-jetpack' : 'stats-comments';
// Use StatsModule to display paywall upsell.
const shouldGateStatsModule = useShouldGateStats( statType );
const isRequestingData = useSelector( ( state: StatsStateProps ) =>
isRequestingSiteStatsForQuery( state, siteId, statType, {} )
);
const commentsStatsData = useSelector( ( state: StatsStateProps ) =>
getSiteStatsNormalizedData( state, siteId, statType, {} )
) as CommentData;
const selectOptions = [
{
value: 'top-authors',
label: translate( 'By authors' ),
},
{
value: 'top-content',
label: translate( 'By posts & pages' ),
},
];
const commentsAuthors = commentsStatsData?.authors;
const commentsPosts = commentsStatsData?.posts;
const data = activeFilter === 'top-authors' ? commentsAuthors : commentsPosts;
const hasPosts = data?.length > 0;
const dataForBars = data?.map( ( item ) => ( {
...item,
value: parseInt( item.value, 10 ),
} ) ) as CommentDataAuthors< number >[] | CommentDataPosts< number >[];
const handleFilterChange = ( selection: HeaderToggleOptionType ) => {
setActiveFilter( selection.value );
};
return (
<>
{ ! shouldGateStatsModule && siteId && statType && (
<QuerySiteStats statType={ statType } siteId={ siteId } />
) }
{ isRequestingData && (
<StatsCardSkeleton
isLoading={ isRequestingData }
className={ className }
title={ moduleTitle }
type={ 3 }
/>
) }
{ ( ( ! isRequestingData && hasPosts ) || shouldGateStatsModule ) && (
// show data or an overlay
// @ts-expect-error TODO: Refactor StatsListCard with TypeScript.
<StatsListCard
moduleType="comments"
data={ dataForBars }
title={ translate( 'Comments' ) }
titleNodes={
<StatsInfoArea>
{ translate(
'Learn about the {{link}}comments{{/link}} your site receives by authors, posts, and pages.',
{
comment: '{{link}} links to support documentation.',
components: {
link: (
<InlineSupportLink supportContext={ supportContext } showIcon={ false } />
),
},
context: 'Stats: Info box label when the Comments module is empty',
}
) }
</StatsInfoArea>
}
mainItemLabel={ translate( 'Author' ) }
metricLabel={ translate( 'Comments' ) }
splitHeader
useShortNumber
toggleControl={
<SimplifiedSegmentedControl options={ selectOptions } onSelect={ handleFilterChange } />
}
className={ clsx( 'stats__modernised-comments', className ) }
showLeftIcon
overlay={
siteId &&
shouldGateStatsModule && (
<StatsCardUpsell
className="stats-module__upsell"
statType={ STAT_TYPE_COMMENTS }
siteId={ siteId }
/>
)
}
/>
) }
{ ! isRequestingData && ! hasPosts && ! shouldGateStatsModule && (
// show empty state
<StatsCard
className={ className }
title={ moduleTitle }
isEmpty
emptyMessage={
<EmptyModuleCard
icon={ comment }
description={ translate(
'Learn about the {{link}}comments{{/link}} your site receives by authors, posts, and pages.',
{
comment: '{{link}} links to support documentation.',
components: {
link: (
<InlineSupportLink supportContext={ supportContext } showIcon={ false } />
),
},
context: 'Stats: Info box label when the Comments module is empty',
}
) }
/>
}
/>
) }
</>
);
};
export default StatsComments;
|