import { isJetpackSearchSlug } from '@automattic/calypso-products'; import Debug from 'debug'; import QueryJetpackModules from 'calypso/components/data/query-jetpack-modules'; import QuerySiteSettings from 'calypso/components/data/query-site-settings'; import QueryUserPurchases from 'calypso/components/data/query-user-purchases'; import IsJetpackDisconnectedSwitch from 'calypso/components/jetpack/is-jetpack-disconnected-switch'; import { UpsellProductCardPlaceholder } from 'calypso/components/jetpack/upsell-product-card'; import UpsellSwitch from 'calypso/components/jetpack/upsell-switch'; import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud'; import { hasLoadedUserPurchasesFromServer, isFetchingUserPurchases, } from 'calypso/state/purchases/selectors'; import getSiteSetting from 'calypso/state/selectors/get-site-setting'; import isFetchingJetpackModules from 'calypso/state/selectors/is-fetching-jetpack-modules'; import isJetpackModuleActive from 'calypso/state/selectors/is-jetpack-module-active'; import { isRequestingSiteSettings } from 'calypso/state/site-settings/selectors'; import { isJetpackSite } from 'calypso/state/sites/selectors'; import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id'; import JetpackSearchDisconnected from './disconnected'; import JetpackSearchUpsell from './jetpack-search-upsell'; import SearchMain from './main'; import WpcomSearchUpsellPlaceholder from './wpcom-search-upsell-placeholder'; const debug = new Debug( 'calypso:my-sites:search:controller' ); export function showUpsellIfNoSearch( context, next ) { debug( 'controller: showUpsellIfNoSearch', context.params ); const state = context.store.getState(); const siteId = getSelectedSiteId( state ); const isJetpack = isJetpackSite( state, siteId ); const QueryComponent = isJetpack ? QueryJetpackModules : QuerySiteSettings; const getSearchState = isJetpack ? getJetpackSearchState : getWPComSearchState; const isRequestingForSite = ( asyncState, asyncSiteId ) => { // On Jetpack sites, we need to check if the search module is active, on WPCom sites we // check 'jetpack_search_enabled' in the site settings. const isRequestingSearchStatus = isJetpack ? isFetchingJetpackModules : isRequestingSiteSettings; // We also need to wait for user purchases to be loaded, or we risk flashing the upsell to // customers who already own the product. const isRequestingPurchases = ! hasLoadedUserPurchasesFromServer( asyncState ) || isFetchingUserPurchases( asyncState ); return isRequestingPurchases || isRequestingSearchStatus( asyncState, asyncSiteId ); }; const UpsellPlaceholder = isJetpackCloud() ? UpsellProductCardPlaceholder : WpcomSearchUpsellPlaceholder; context.primary = ( <> ); next(); } export function showJetpackIsDisconnected( context, next ) { debug( 'controller: showJetpackIsDisconnected', context.params ); context.primary = ( } falseComponent={ context.primary } /> ); next(); } /* handles /jetpack-search/:site, see `jetpackSearchMainPath` */ export function jetpackSearchMain( context, next ) { debug( 'controller: jetpackSearchMain', context.params ); context.primary = ; next(); } // Selectors used for the Search UpsellSwitch (above) function getJetpackSearchState( state, siteId ) { const isSearchModuleActive = isJetpackModuleActive( state, siteId, 'search' ); return { state: isSearchModuleActive ? 'active' : 'unavailable', ...( ! isSearchModuleActive && { reason: 'search_not_active' } ), }; } function getWPComSearchState( state, siteId ) { const isSearchSettingEnabled = getSiteSetting( state, siteId, 'jetpack_search_enabled' ); return { state: isSearchSettingEnabled ? 'active' : 'unavailable', ...( ! isSearchSettingEnabled && { reason: 'search_not_active' } ), }; }