2020-05-04 22:58:39 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-08-20 04:02:01 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-05-04 22:58:39 +00:00
|
|
|
import { Fragment } from '@wordpress/element';
|
2021-08-30 19:59:05 +00:00
|
|
|
import { TabPanel, Card, CardHeader, CardFooter } from '@wordpress/components';
|
2020-06-10 16:46:46 +00:00
|
|
|
import { get, xor } from 'lodash';
|
2020-05-04 22:58:39 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
|
|
|
MenuItem,
|
|
|
|
MenuTitle,
|
2020-05-14 03:23:03 +00:00
|
|
|
Link,
|
2020-05-04 22:58:39 +00:00
|
|
|
} from '@woocommerce/components';
|
2020-08-20 04:02:01 +00:00
|
|
|
import { useUserPreferences, PLUGINS_STORE_NAME } from '@woocommerce/data';
|
2020-05-14 03:23:03 +00:00
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2021-01-07 23:57:09 +00:00
|
|
|
import { Text } from '@woocommerce/experimental';
|
2021-04-15 01:32:46 +00:00
|
|
|
import { Experiment } from '@woocommerce/explat';
|
2020-05-04 22:58:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-14 03:23:03 +00:00
|
|
|
import './style.scss';
|
2020-05-04 22:58:39 +00:00
|
|
|
import { DEFAULT_STATS, DEFAULT_HIDDEN_STATS } from './defaults';
|
2020-05-14 03:23:03 +00:00
|
|
|
import StatsList from './stats-list';
|
2020-08-20 04:02:01 +00:00
|
|
|
import { InstallJetpackCTA } from './install-jetpack-cta';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2020-05-04 22:58:39 +00:00
|
|
|
|
2022-01-06 12:53:30 +00:00
|
|
|
const { performanceIndicators = [] } = getAdminSetting( 'dataEndpoints', {
|
2020-05-04 22:58:39 +00:00
|
|
|
performanceIndicators: [],
|
|
|
|
} );
|
2021-07-14 03:09:02 +00:00
|
|
|
|
2020-05-04 22:58:39 +00:00
|
|
|
const stats = performanceIndicators.filter( ( indicator ) => {
|
|
|
|
return DEFAULT_STATS.includes( indicator.stat );
|
|
|
|
} );
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
export const StatsOverview = () => {
|
|
|
|
const { updateUserPreferences, ...userPrefs } = useUserPreferences();
|
2020-06-18 23:44:00 +00:00
|
|
|
const hiddenStats = get(
|
|
|
|
userPrefs,
|
|
|
|
[ 'homepage_stats', 'hiddenStats' ],
|
|
|
|
DEFAULT_HIDDEN_STATS
|
|
|
|
);
|
2020-05-04 22:58:39 +00:00
|
|
|
|
2020-08-20 04:02:01 +00:00
|
|
|
const jetPackIsConnected = useSelect( ( select ) => {
|
|
|
|
return select( PLUGINS_STORE_NAME ).isJetpackConnected();
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const homePageStats = userPrefs.homepage_stats || {};
|
|
|
|
const userDismissedJetpackInstall = homePageStats.installJetpackDismissed;
|
|
|
|
|
2020-05-04 22:58:39 +00:00
|
|
|
const toggleStat = ( stat ) => {
|
|
|
|
const nextHiddenStats = xor( hiddenStats, [ stat ] );
|
2020-06-10 16:46:46 +00:00
|
|
|
updateUserPreferences( {
|
|
|
|
homepage_stats: { hiddenStats: nextHiddenStats },
|
2020-05-04 22:58:39 +00:00
|
|
|
} );
|
|
|
|
recordEvent( 'statsoverview_indicators_toggle', {
|
|
|
|
indicator_name: stat,
|
|
|
|
status: nextHiddenStats.includes( stat ) ? 'off' : 'on',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-05-14 03:23:03 +00:00
|
|
|
const activeStats = stats.filter(
|
|
|
|
( item ) => ! hiddenStats.includes( item.stat )
|
|
|
|
);
|
|
|
|
|
2021-04-15 01:32:46 +00:00
|
|
|
const HeaderText = (
|
2021-06-28 01:14:59 +00:00
|
|
|
<Text variant="title.small" size="20" lineHeight="28px">
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Stats overview', 'woocommerce' ) }
|
2021-04-15 01:32:46 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
|
2020-05-04 22:58:39 +00:00
|
|
|
return (
|
|
|
|
<Card
|
2020-06-18 23:44:00 +00:00
|
|
|
size="large"
|
|
|
|
className="woocommerce-stats-overview woocommerce-homescreen-card"
|
|
|
|
>
|
|
|
|
<CardHeader size="medium">
|
2021-04-15 01:32:46 +00:00
|
|
|
<Experiment
|
|
|
|
name="woocommerce_test_experiment"
|
|
|
|
defaultExperience={ HeaderText }
|
|
|
|
treatmentExperience={ HeaderText }
|
|
|
|
loadingExperience={ HeaderText }
|
|
|
|
/>
|
2020-05-04 22:58:39 +00:00
|
|
|
<EllipsisMenu
|
|
|
|
label={ __(
|
|
|
|
'Choose which values to display',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-05-04 22:58:39 +00:00
|
|
|
) }
|
|
|
|
renderContent={ () => (
|
|
|
|
<Fragment>
|
|
|
|
<MenuTitle>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Display stats:', 'woocommerce' ) }
|
2020-05-04 22:58:39 +00:00
|
|
|
</MenuTitle>
|
|
|
|
{ stats.map( ( item ) => {
|
|
|
|
const checked = ! hiddenStats.includes(
|
|
|
|
item.stat
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
checked={ checked }
|
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ item.stat }
|
|
|
|
onInvoke={ () =>
|
|
|
|
toggleStat( item.stat )
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ item.label }
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
/>
|
2020-06-18 23:44:00 +00:00
|
|
|
</CardHeader>
|
2021-08-30 19:59:05 +00:00
|
|
|
<TabPanel
|
|
|
|
className="woocommerce-stats-overview__tabs"
|
|
|
|
onSelect={ ( period ) => {
|
|
|
|
recordEvent( 'statsoverview_date_picker_update', {
|
|
|
|
period,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
tabs={ [
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
title: __( 'Today', 'woocommerce' ),
|
2021-08-30 19:59:05 +00:00
|
|
|
name: 'today',
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
title: __( 'Week to date', 'woocommerce' ),
|
2021-08-30 19:59:05 +00:00
|
|
|
name: 'week',
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
title: __( 'Month to date', 'woocommerce' ),
|
2021-08-30 19:59:05 +00:00
|
|
|
name: 'month',
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
>
|
|
|
|
{ ( tab ) => (
|
|
|
|
<Fragment>
|
|
|
|
{ ! jetPackIsConnected &&
|
|
|
|
! userDismissedJetpackInstall && (
|
|
|
|
<InstallJetpackCTA />
|
|
|
|
) }
|
|
|
|
<StatsList
|
|
|
|
query={ {
|
|
|
|
period: tab.name,
|
|
|
|
compare: 'previous_period',
|
|
|
|
} }
|
|
|
|
stats={ activeStats }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</TabPanel>
|
2020-06-18 23:44:00 +00:00
|
|
|
<CardFooter>
|
2020-05-20 22:15:18 +00:00
|
|
|
<Link
|
|
|
|
className="woocommerce-stats-overview__more-btn"
|
|
|
|
href={ getNewPath( {}, '/analytics/overview' ) }
|
|
|
|
type="wc-admin"
|
|
|
|
onClick={ () => {
|
|
|
|
recordEvent( 'statsoverview_indicators_click', {
|
|
|
|
key: 'view_detailed_stats',
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'View detailed stats', 'woocommerce' ) }
|
2020-05-20 22:15:18 +00:00
|
|
|
</Link>
|
2020-06-18 23:44:00 +00:00
|
|
|
</CardFooter>
|
2020-05-04 22:58:39 +00:00
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
export default StatsOverview;
|