Skip to content

Commit

Permalink
Extract subscription logic and refresh subscriptions when the count c…
Browse files Browse the repository at this point in the history
…hanges.
  • Loading branch information
allilevine committed Dec 3, 2024
1 parent ec6e8a2 commit 0c40ff4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
27 changes: 2 additions & 25 deletions client/reader/following/main.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import config from '@automattic/calypso-config';
import { SubscriptionManager } from '@automattic/data-stores';
import clsx from 'clsx';
import { translate } from 'i18n-calypso';
import { useMemo } from 'react';
import AsyncLoad from 'calypso/components/async-load';
import BloganuaryHeader from 'calypso/components/bloganuary-header';
import NavigationHeader from 'calypso/components/navigation-header';
Expand All @@ -12,35 +10,14 @@ import SuggestionProvider from 'calypso/reader/search-stream/suggestion-provider
import ReaderStream, { WIDE_DISPLAY_CUTOFF } from 'calypso/reader/stream';
import ReaderListFollowedSites from 'calypso/reader/stream/reader-list-followed-sites';
import Recent from '../recent';
import { useSubscriptions } from './use-subscriptions';
import { useFollowingView } from './view-preference';
import ViewToggle from './view-toggle';
import './style.scss';

function FollowingStream( { ...props } ) {
const { currentView } = useFollowingView();
const { data: subscriptionsCount, isLoading: isLoadingCount } =
SubscriptionManager.useSubscriptionsCountQuery();
const { data: siteSubscriptions, isLoading: isLoadingSiteSubscriptions } =
SubscriptionManager.useSiteSubscriptionsQuery();

const isLoading = isLoadingCount || isLoadingSiteSubscriptions;

const hasNonSelfSubscriptions = useMemo( () => {
if ( ! subscriptionsCount?.blogs || subscriptionsCount?.blogs === 0 ) {
return false;
}

// If we have site subscriptions data, filter out self-owned blogs.
if ( siteSubscriptions?.subscriptions ) {
const nonSelfSubscriptions = siteSubscriptions.subscriptions.filter(
( sub ) => ! sub.is_owner
);
return nonSelfSubscriptions.length > 0;
}

return subscriptionsCount.blogs > 0;
}, [ subscriptionsCount, siteSubscriptions ] );

const { isLoading, hasNonSelfSubscriptions } = useSubscriptions();
const viewToggle = config.isEnabled( 'reader/recent-feed-overhaul' ) ? <ViewToggle /> : null;

if ( ! isLoading && ! hasNonSelfSubscriptions ) {
Expand Down
40 changes: 40 additions & 0 deletions client/reader/following/use-subscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { SubscriptionManager } from '@automattic/data-stores';
import { useMemo, useEffect } from 'react';

export function useSubscriptions() {
const { data: subscriptionsCount, isLoading: isLoadingCount } =
SubscriptionManager.useSubscriptionsCountQuery();
const {
data: siteSubscriptions,
isLoading: isLoadingSiteSubscriptions,
refetch: refetchSiteSubscriptions,
} = SubscriptionManager.useSiteSubscriptionsQuery();

const isLoading = isLoadingCount || isLoadingSiteSubscriptions;
const blogCount = subscriptionsCount?.blogs ?? 0;

const nonSelfSubscriptions = useMemo(
() => siteSubscriptions?.subscriptions?.filter( ( sub ) => ! sub.is_owner ) ?? [],
[ siteSubscriptions.subscriptions ]
);

const hasNonSelfSubscriptions = useMemo( () => {
if ( blogCount === 0 ) {
return false;
}
return siteSubscriptions?.subscriptions ? nonSelfSubscriptions.length > 0 : blogCount > 0;
}, [ blogCount, siteSubscriptions.subscriptions, nonSelfSubscriptions ] );

useEffect( () => {
if ( blogCount > 0 ) {
refetchSiteSubscriptions();
}
}, [ refetchSiteSubscriptions, blogCount ] );

return {
isLoading,
hasNonSelfSubscriptions,
nonSelfSubscriptions,
blogCount,
};
}

0 comments on commit 0c40ff4

Please sign in to comment.