Skip to content

Commit

Permalink
fix subscribers empty issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kangzj committed Feb 11, 2025
1 parent 20e24f4 commit 3c64268
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
53 changes: 25 additions & 28 deletions client/my-sites/stats/hooks/use-subscribers-totals-query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { parseAvatar } from 'calypso/state/stats/lists/utils';
import getDefaultQueryParams from './default-query-params';

const MAX_SUBSCRIBERS_TO_RETURN = 10;
const isJetpackApi = config.isEnabled( 'is_running_in_jetpack' );
const isJetpackApi = config.isEnabled( 'is_running_in_jetpack_site' );
const sortByDateDesc = ( a: { date_subscribed: string }, b: { date_subscribed: string } ) => {
return new Date( b.date_subscribed ).getTime() - new Date( a.date_subscribed ).getTime();
};

const querySubscribersTotals = ( siteId: number | null, filterAdmin?: boolean ): Promise< any > => {
return wpcom.req
Expand Down Expand Up @@ -117,8 +120,6 @@ export function useSubscribersTotalsWithoutAdminQueries( siteId: number | null )
}

function useSubscribersTotalsQueries( siteId: number | null, filterAdmin?: boolean ) {
const isJetpackApi = config.isEnabled( 'is_running_in_jetpack' );

const results = useQueries( {
queries: [
{
Expand Down Expand Up @@ -152,54 +153,50 @@ function useSubscribersTotalsQueries( siteId: number | null, filterAdmin?: boole
],
} );

if ( ! isJetpackApi ) {
// Use `subscribers_by_user_type` endpoint in Calypso Stats.
if ( isJetpackApi ) {
// `subscribers_by_user_type` endpoint is not available for Odyssey Stats yet.
return {
data: {
total_email: results[ 3 ]?.data?.total,
total_wpcom: results[ 2 ]?.data?.total,
total: results[ 1 ].data?.email_subscribers,
total_email: results[ 0 ]?.data?.total_email,
total_wpcom: results[ 0 ]?.data?.total_wpcom,
total: results[ 0 ]?.data?.total,
paid_subscribers: results[ 1 ]?.data?.paid_subscribers,
free_subscribers:
results[ 1 ]?.data?.email_subscribers !== undefined &&
results[ 1 ]?.data?.paid_subscribers !== undefined
? results[ 1 ].data.email_subscribers - results[ 1 ].data.paid_subscribers
: null,
social_followers: results[ 1 ]?.data?.social_followers,
is_owner_subscribing: results[ 2 ]?.data?.is_owner_subscribing,
// Merge email and wpcom subscribers and sort by date_subscribed, and only shows the most recent 10 subscribers.
subscribers:
[
...( results[ 3 ]?.data?.subscribers ?? [] ),
...( results[ 2 ]?.data?.subscribers ?? [] ),
]
.sort( ( a, b ) => {
return (
new Date( b.date_subscribed ).getTime() - new Date( a.date_subscribed ).getTime()
);
} )
.slice( 0, MAX_SUBSCRIBERS_TO_RETURN ) ?? [],
is_owner_subscribing: results[ 0 ]?.data?.is_owner_subscribing,
subscribers: ( results[ 0 ]?.data?.subscribers ?? [] ).sort( sortByDateDesc ),
},
isLoading: results.some( ( result ) => result.isLoading ),
isLoading: results.some( ( result ) => result.isPending ),
isError: results.some( ( result ) => result.isError ),
};
}

// `subscribers_by_user_type` endpoint is not available for Odyssey Stats yet.
// Use `subscribers_by_user_type` endpoint in Calypso Stats.
return {
data: {
total_email: results[ 0 ]?.data?.total_email,
total_wpcom: results[ 0 ]?.data?.total_wpcom,
total: results[ 0 ]?.data?.total,
total_email: results[ 3 ]?.data?.total,
total_wpcom: results[ 2 ]?.data?.total,
total: results[ 1 ].data?.email_subscribers,
paid_subscribers: results[ 1 ]?.data?.paid_subscribers,
free_subscribers:
results[ 1 ]?.data?.email_subscribers !== undefined &&
results[ 1 ]?.data?.paid_subscribers !== undefined
? results[ 1 ].data.email_subscribers - results[ 1 ].data.paid_subscribers
: null,
social_followers: results[ 1 ]?.data?.social_followers,
is_owner_subscribing: results[ 0 ]?.data?.is_owner_subscribing,
subscribers: results[ 0 ]?.data?.subscribers,
is_owner_subscribing: results[ 2 ]?.data?.is_owner_subscribing,
// Merge email and wpcom subscribers and sort by date_subscribed, and only shows the most recent 10 subscribers.
subscribers:
[
...( results[ 3 ]?.data?.subscribers ?? [] ),
...( results[ 2 ]?.data?.subscribers ?? [] ),
]
.sort( sortByDateDesc )
.slice( 0, MAX_SUBSCRIBERS_TO_RETURN ) ?? [],
},
isLoading: results.some( ( result ) => result.isLoading ),
isError: results.some( ( result ) => result.isError ),
Expand Down
2 changes: 1 addition & 1 deletion client/my-sites/stats/stats-followers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const StatModuleFollowers = ( { className } ) => {
const difference = now.getTime() - value.getTime();

const seconds = Math.floor( difference / 1000 );
const minutes = Math.floor( seconds / 60 );
const minutes = Math.ceil( seconds / 60 );
const hours = Math.floor( minutes / 60 );
const days = Math.floor( hours / 24 );

Expand Down

0 comments on commit 3c64268

Please sign in to comment.