From 0c40ff402ed444acd877ba4b5b685a612ebf5e45 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Tue, 3 Dec 2024 15:15:11 -0500 Subject: [PATCH] Extract subscription logic and refresh subscriptions when the count changes. --- client/reader/following/main.tsx | 27 +------------ client/reader/following/use-subscriptions.ts | 40 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 client/reader/following/use-subscriptions.ts diff --git a/client/reader/following/main.tsx b/client/reader/following/main.tsx index 92454de48957f0..43dbc57c1033f5 100644 --- a/client/reader/following/main.tsx +++ b/client/reader/following/main.tsx @@ -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'; @@ -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' ) ? : null; if ( ! isLoading && ! hasNonSelfSubscriptions ) { diff --git a/client/reader/following/use-subscriptions.ts b/client/reader/following/use-subscriptions.ts new file mode 100644 index 00000000000000..1957c747ac99d6 --- /dev/null +++ b/client/reader/following/use-subscriptions.ts @@ -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, + }; +}