/
githubmirror
/
wp-calypso
Обзор
Документация
Войти
/
githubmirror
/
wp-calypso
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
client/root.js
141 строка
5 KB
Ashar Fuadi
Don't lose the Calypso Live link domain when clicked by pointing to /home (#112922)
28 июл 2026, 12:55
Не верифицирован
28 июл 2026, 12:55
cdcb953
Код
Авторство
О чём код?
import config from '@automattic/calypso-config'; import globalPageInstance from '@automattic/calypso-router'; import { dashboardLink } from 'calypso/dashboard/utils/link'; import { bumpStat } from 'calypso/lib/analytics/mc'; import { isUserLoggedIn } from 'calypso/state/current-user/selectors'; import { hasDashboardOptIn } from 'calypso/state/dashboard/selectors'; import { fetchPreferences } from 'calypso/state/preferences/actions'; import { hasReceivedRemotePreferences } from 'calypso/state/preferences/selectors'; import getIsSubscriptionOnly from 'calypso/state/selectors/get-is-subscription-only'; import getPrimarySiteId from 'calypso/state/selectors/get-primary-site-id'; import { requestSite } from 'calypso/state/sites/actions'; import { canCurrentUserUseCustomerHome, getSite, getSiteSlug, getSiteAdminUrl, isAdminInterfaceWPAdmin, } from 'calypso/state/sites/selectors'; import { hasReadersAsLandingPage } from 'calypso/state/sites/selectors/has-reader-as-landing-page'; import { hasSitesAsLandingPage } from 'calypso/state/sites/selectors/has-sites-as-landing-page'; /** * @param clientRouter Unused. We can't use the isomorphic router because we want to do redirects. * @param page Used to create isolated unit tests. Default behaviour uses the global 'page' router. */ export default function ( clientRouter, page = globalPageInstance ) { page( '/', ( context ) => { const isLoggedIn = isUserLoggedIn( context.store.getState() ); if ( isLoggedIn ) { handleLoggedIn( page, context ); } else { handleLoggedOut( page ); } } ); } function handleLoggedOut( page ) { if ( config.isEnabled( 'jetpack-cloud' ) ) { if ( config.isEnabled( 'oauth' ) ) { page.redirect( '/connect' ); return; } } // We can't use page.redirect(), the login app is not loaded. window.location.assign( '/log-in' ); } async function handleLoggedIn( page, context ) { let redirectPath = await getLoggedInLandingPage( context.store ); if ( context.querystring ) { redirectPath += `?${ context.querystring }`; } if ( redirectPath.startsWith( '/' ) ) { page.redirect( redirectPath ); } else { // Case for wp-admin redirection when primary site has classic admin interface. window.location.assign( redirectPath ); } } // Helper thunk that ensures that the requested site info is fetched into Redux state before we // continue working with it. // The `siteSelection` handler in `my-sites/controller` contains similar code. const waitForSite = ( siteId ) => async ( dispatch, getState ) => { if ( getSite( getState(), siteId ) ) { return; } try { await dispatch( requestSite( siteId ) ); } catch { // if the fetching of site info fails, return gracefully and proceed to redirect to Reader } }; // Helper thunk that ensures that the user preferences has been fetched into Redux state before we // continue working with it. const waitForPrefs = () => async ( dispatch, getState ) => { if ( hasReceivedRemotePreferences( getState() ) ) { return; } try { await dispatch( fetchPreferences() ); } catch { // if the fetching of preferences fails, return gracefully and proceed to the next landing page candidate } }; const getSitesLink = ( isDashboardOptIn ) => { if ( isDashboardOptIn ) { bumpStat( 'dashboard-redirect', 'landing-page' ); return dashboardLink( '/sites' ); } return '/sites'; }; async function getLoggedInLandingPage( { dispatch, getState } ) { await dispatch( waitForPrefs() ); const useSitesAsLandingPage = hasSitesAsLandingPage( getState() ); const dashboardOptIn = hasDashboardOptIn( getState() ); if ( useSitesAsLandingPage ) { return getSitesLink( dashboardOptIn ); } const useReaderAsLandingPage = hasReadersAsLandingPage( getState() ); if ( useReaderAsLandingPage ) { return '/reader'; } // determine the primary site ID (it's a property of "current user" object) and then // ensure that the primary site info is loaded into Redux before proceeding. const primarySiteId = getPrimarySiteId( getState() ); await dispatch( waitForSite( primarySiteId ) ); const primarySiteSlug = getSiteSlug( getState(), primarySiteId ); if ( ! primarySiteSlug ) { if ( getIsSubscriptionOnly( getState() ) ) { return '/reader'; } // there is no primary site or the site info couldn't be fetched. Redirect to Sites Dashboard. return getSitesLink( dashboardOptIn ); } const isCustomerHomeEnabled = canCurrentUserUseCustomerHome( getState(), primarySiteId ); if ( isCustomerHomeEnabled ) { if ( isAdminInterfaceWPAdmin( getState(), primarySiteId ) ) { return getSiteAdminUrl( getState(), primarySiteId ); } return `/home/${ primarySiteSlug }`; } return `/stats/day/${ primarySiteSlug }`; }