import { SegmentedControl } from '@automattic/components'; import { formatNumber } from '@automattic/number-formatters'; import { Icon, external } from '@wordpress/icons'; import clsx from 'clsx'; import { useTranslate } from 'i18n-calypso'; import moment from 'moment'; import { useState, FunctionComponent } from 'react'; import useReferrersQuery from '../hooks/use-referrers-query'; import useTopPostsQuery from '../hooks/use-top-posts-query'; import { HighLightItem } from '../typings'; import './highlights.scss'; interface ItemWrapperProps { siteId: number; statsBaseUrl: string; isItemLink: boolean; item: HighLightItem; isItemLinkExternal: boolean; } interface TopColumnProps { items: Array< HighLightItem >; viewAllUrl: string; viewAllText: string; title: string; isLoading: boolean; statsBaseUrl: string; siteId: number; isItemLinkExternal?: boolean; isItemLink?: boolean; className?: null | string; } interface HighlightsProps { siteId: number; gmtOffset: number; statsBaseUrl: string; } const HIGHLIGHT_ITEMS_LIMIT = 5; const HIGHLIGHT_TAB_TOP_POSTS_PAGES = 'topPostsAndPages'; const HIGHLIGHT_TAB_TOP_REFERRERS = 'topReferrers'; const postAndPageLink = ( baseUrl: string, siteId: number, postId: number ) => { return `${ baseUrl }/stats/post/${ postId }/${ siteId }`; }; const externalLink = ( item: HighLightItem ) => { // Url is for referrers and href is for top posts and pages. return item.url || item.href; }; const ItemWrapper: FunctionComponent< ItemWrapperProps > = ( { statsBaseUrl, siteId, isItemLink, item, isItemLinkExternal, } ) => { const translate = useTranslate(); const renderedItem = (

{ item.title }

{ translate( '%(views)s Views', { args: { views: formatNumber( item.views ), }, } ) }
); return isItemLink ? ( { renderedItem } { isItemLinkExternal && } ) : ( renderedItem ); }; const TopColumn: FunctionComponent< TopColumnProps > = ( { items, viewAllUrl, viewAllText, title, isLoading, statsBaseUrl, siteId, isItemLink = false, isItemLinkExternal = false, className = null, } ) => { const translate = useTranslate(); return (
{ items.length === 0 && (

{ isLoading ? `${ translate( 'Loading' ) }...` : translate( 'No data to show' ) }

) } { items.length > 0 && ( ) }
{ viewAllText }
); }; export default function Highlights( { siteId, gmtOffset, statsBaseUrl }: HighlightsProps ) { const translate = useTranslate(); const headingTitle = translate( '7 Day Highlights' ); const topPostsAndPagesTitle = translate( 'Top Posts & Pages' ); const topReferrersTitle = translate( 'Top Referrers' ); const moduleTabs = [ { value: HIGHLIGHT_TAB_TOP_POSTS_PAGES, label: topPostsAndPagesTitle, }, { value: HIGHLIGHT_TAB_TOP_REFERRERS, label: topReferrersTitle, }, ]; // Default to the first tab `topPostsAndPages`. const [ selectedTab, setSelectedTab ] = useState( HIGHLIGHT_TAB_TOP_POSTS_PAGES ); const queryDate = moment() .utcOffset( Number.isFinite( gmtOffset ) ? gmtOffset : 0 ) .format( 'YYYY-MM-DD' ); const viewAllPostsStatsUrl = `${ statsBaseUrl }/stats/day/posts/${ siteId }?startDate=${ queryDate }&summarize=1&num=7`; const viewAllReferrerStatsUrl = `${ statsBaseUrl }/stats/day/referrers/${ siteId }?startDate=${ queryDate }&summarize=1&num=7`; const { data: topPostsAndPages = [], isFetching: isFetchingPostsAndPages } = useTopPostsQuery( siteId, 'day', 7, queryDate ); const { data: topReferrers = [], isFetching: isFetchingReferrers } = useReferrersQuery( siteId, 'day', 7, queryDate ); return (
{ moduleTabs.map( ( tab ) => { return ( setSelectedTab( tab.value ) } > { tab.label } ); } ) }
); }