Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: improve Download CSV consistency #99763

Merged
merged 5 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions client/my-sites/stats/stats-download-csv/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Button, Gridicon } from '@automattic/components';
import { Button } from '@wordpress/components';
import { download } from '@wordpress/icons';
import { saveAs } from 'browser-filesaver';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -92,12 +93,12 @@ class StatsDownloadCsv extends Component {
onClick={ this.downloadCsv }
disabled={ disabled }
borderless={ borderless }
icon={ download }
>
{ ! skipQuery && siteId && statType && query && (
<QuerySiteStats statType={ statType } siteId={ siteId } query={ query } />
) }
<Gridicon icon="cloud-download" />{ ' ' }
{ translate( 'Download data as CSV', {
{ translate( 'Download CSV', {
context: 'Action shown in stats to download data as csv.',
} ) }
</Button>
Expand Down
4 changes: 4 additions & 0 deletions client/my-sites/stats/stats-list/stats-list-card.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const StatsListCard = ( {
showMore,
title,
titleNodes,
downloadCsv,
emptyMessage,
loader,
useShortLabel,
Expand All @@ -30,6 +31,7 @@ const StatsListCard = ( {
heroElement, // a node placed before the list
metricLabel, // a label to use for the values on the right side of the bars - `Views` by default
splitHeader, // instead of using a simple header containing the name of the card use additional columns and header items
multiHeader,
mainItemLabel,
additionalColumns, // additional columns to be displayed next to the default `views` column
toggleControl, // component to be placed in a split header
Expand Down Expand Up @@ -140,11 +142,13 @@ const StatsListCard = ( {
emptyMessage={ emptyMessage }
isEmpty={ ! loader && ( ! data || ! data?.length ) }
titleNodes={ titleNodes }
downloadCsv={ downloadCsv }
className={ clsx( `list-${ moduleType }`, className ) }
headerClassName={ listItemClassName }
metricLabel={ metricLabel }
heroElement={ heroElement }
splitHeader={ splitHeader }
multiHeader={ multiHeader }
mainItemLabel={ mainItemLabel }
additionalHeaderColumns={ additionalColumns?.header }
toggleControl={ toggleControl }
Expand Down
43 changes: 23 additions & 20 deletions client/my-sites/stats/stats-module/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ class StatsModule extends Component {
isAllTimeList() {
const { summary, statType } = this.props;
const summarizedTypes = [
'statsCountryViews',
'statsTopPosts',
'statsSearchTerms',
'statsClicks',
'statsReferrers',
'statsTopAuthors',
'statsFileDownloads',
// statsEmailsOpen and statsEmailsClick are not used. statsEmailsSummary are used at the moment,
// besides this, email page uses separate summary component: <StatsEmailSummary />
'statsEmailsOpen',
Expand Down Expand Up @@ -261,9 +262,25 @@ class StatsModule extends Component {
const summaryLink = ! this.props.hideSummaryLink && this.getSummaryLink();
const displaySummaryLink = data && summaryLink;
const isAllTime = this.isAllTimeList();
const footerClass = clsx( 'stats-module__footer-actions', {
'stats-module__footer-actions--summary': summary,
} );

const renderDownloadCsv = () => {
if ( gateDownloads ) {
return <DownloadCsvUpsell siteId={ siteId } borderless />;
}

return (
<DownloadCsv
statType={ statType }
query={ query }
path={ path }
borderless
period={ period }
skipQuery={ skipQuery }
/>
);
};

const downloadCsv = renderDownloadCsv();

const emptyMessage = isRealTime ? 'gathering info…' : moduleStrings.empty;
// TODO: Translate empty message
Expand All @@ -281,6 +298,7 @@ class StatsModule extends Component {
useShortLabel={ useShortLabel }
title={ this.props.moduleStrings?.title }
titleNodes={ titleNodes }
downloadCsv={ downloadCsv }
emptyMessage={ emptyMessage }
metricLabel={ metricLabel }
showMore={
Expand All @@ -305,6 +323,7 @@ class StatsModule extends Component {
}
additionalColumns={ additionalColumns }
splitHeader={ !! additionalColumns }
multiHeader={ isAllTime }
mainItemLabel={ mainItemLabel }
showLeftIcon={ path === 'authors' }
listItemClassName={ listItemClassName }
Expand All @@ -322,22 +341,6 @@ class StatsModule extends Component {
}
formatValue={ formatValue }
/>
{ isAllTime && (
<div className={ footerClass }>
{ gateDownloads ? (
<DownloadCsvUpsell siteId={ siteId } borderless />
) : (
<DownloadCsv
statType={ statType }
query={ query }
path={ path }
borderless
period={ period }
skipQuery={ skipQuery }
/>
) }
</div>
) }
</>
);
}
Expand Down
4 changes: 2 additions & 2 deletions client/my-sites/stats/summary/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class StatsSummary extends Component {
/* eslint-disable wpcalypso/jsx-classname-namespace */
summaryView = (
<Fragment key="authors-summary">
{ this.renderSummaryHeader( path, statType, true, query ) }
{ this.renderSummaryHeader( path, statType, false, query ) }
<StatsModuleAuthors
moduleStrings={ StatsStrings.authors }
period={ this.props.period }
Expand Down Expand Up @@ -261,7 +261,7 @@ class StatsSummary extends Component {

summaryView = (
<Fragment key="filedownloads-summary">
{ this.renderSummaryHeader( path, statType, true, query ) }
{ this.renderSummaryHeader( path, statType, false, query ) }
<StatsModuleDownloads
moduleStrings={ StatsStrings.filedownloads }
period={ this.props.period }
Expand Down
20 changes: 20 additions & 0 deletions packages/components/src/horizontal-bar-list/stats-card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@
width: $value-column-width;
text-align: right;
}

.stats-download-csv {
color: var(--color-link);
gap: 4px;

&[disabled],
&.disabled {
color: var(--color-neutral-20);
}

&:hover:not([disabled]) {
color: var(--color-link-dark);
}
}
}

.stats-card-sub-header {
display: flex;
justify-content: space-between;
padding: 0 $padding-outside $header-spacer $padding-outside;
}

.stats-card-header--main {
Expand Down
30 changes: 29 additions & 1 deletion packages/components/src/horizontal-bar-list/stats-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const StatsCard = ( props: StatsCardProps ) => {
titleURL,
titleAriaLevel = 4,
titleNodes,
downloadCsv,
footerAction,
isEmpty,
emptyMessage,
multiHeader,
metricLabel,
mainItemLabel,
additionalHeaderColumns,
Expand Down Expand Up @@ -57,6 +59,22 @@ const StatsCard = ( props: StatsCardProps ) => {
</div>
);

// Show card title and download csv button on one line, description and metric label on another:
const multiHeaderNode = (
<>
<div className={ clsx( `${ BASE_CLASS_NAME }-header`, headerClassName ) }>
{ titleNode }
{ ! isEmpty && downloadCsv }
</div>
{ ! isEmpty && (
<div className={ clsx( `${ BASE_CLASS_NAME }-sub-header`, headerClassName ) }>
<div> All { title.toLowerCase() } </div>
<div>{ metricLabel ?? translate( 'Views' ) }</div>
</div>
) }
</>
);

// Show Card title on one line and all other column header(s) below:
// (main item, optional additional columns and value)
const splitHeaderNode = (
Expand Down Expand Up @@ -87,6 +105,16 @@ const StatsCard = ( props: StatsCardProps ) => {
</div>
);

const getHeaderNode = () => {
if ( multiHeader ) {
return multiHeaderNode;
}
if ( splitHeader ) {
return splitHeaderNode;
}
return simpleHeaderNode;
};

return (
<div
className={ clsx( className, BASE_CLASS_NAME, {
Expand All @@ -101,7 +129,7 @@ const StatsCard = ( props: StatsCardProps ) => {
</div>
) }
<div className={ `${ BASE_CLASS_NAME }--header-and-body` }>
{ splitHeader ? splitHeaderNode : simpleHeaderNode }
{ getHeaderNode() }
<div
className={ clsx( `${ BASE_CLASS_NAME }--body`, {
[ `${ BASE_CLASS_NAME }--body-empty` ]: isEmpty,
Expand Down
8 changes: 8 additions & 0 deletions packages/components/src/horizontal-bar-list/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export type StatsCardProps = {
* @property {React.ReactNode} titleNodes - additional nodes to be displayed next to the title - use TitleExtras component for unified resutl.
*/
titleNodes?: React.ReactNode;
/**
* @property {React.ReactNode} downloadCsv - a node to be displayed next to the title - use DownloadCsv component for unified result.
*/
downloadCsv?: React.ReactNode;
footerAction?: {
label?: string;
url?: string;
Expand All @@ -98,6 +102,10 @@ export type StatsCardProps = {
* @property {React.ReactNode} heroElement - a node placed before the list
*/
heroElement?: React.ReactNode;
/**
* @property {boolean} multiHeader - adds a sub-header row, including metric label, after the header containing the name of the card and download csv button
*/
multiHeader?: boolean;
/**
* @property {boolean} splitHeader - instead of using a simple header containing the name of the card use additional columns and header items
*/
Expand Down