File size: 3,794 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import {
getAnyLanguageRouteParam,
removeLocaleFromPathLocaleInFront,
} from '@automattic/i18n-utils';
import AsyncLoad from 'calypso/components/async-load';
import {
makeLayout,
redirectInvalidLanguage,
redirectWithoutLocaleParamInFrontIfLoggedIn,
redirectLoggedOutToSignup,
render as clientRender,
} from 'calypso/controller';
import { setLocaleMiddleware } from 'calypso/controller/shared';
import { sectionify } from 'calypso/lib/route';
import { sidebar } from 'calypso/reader/controller';
import {
trackPageLoad,
trackUpdatesLoaded,
trackScrollPage,
} from 'calypso/reader/controller-helper';
import { recordTrack } from 'calypso/reader/stats';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import getCurrentQueryArguments from 'calypso/state/selectors/get-current-query-arguments';
import getCurrentRoute from 'calypso/state/selectors/get-current-route';
import renderHeaderSection from '../lib/header-section';
import { DiscoverDocumentHead } from './discover-document-head';
import { getSelectedTabTitle, DEFAULT_TAB } from './helper';
const ANALYTICS_PAGE_TITLE = 'Reader';
const discover = ( context, next ) => {
const basePath = sectionify( context.path );
const fullAnalyticsPageTitle = ANALYTICS_PAGE_TITLE + ' > Discover';
const streamKey = 'discover:recommended';
const mcKey = 'discover';
const state = context.store.getState();
const currentRoute = getCurrentRoute( state );
const currentQueryArgs = new URLSearchParams( getCurrentQueryArguments( state ) ).toString();
trackPageLoad( basePath, fullAnalyticsPageTitle, mcKey );
recordTrack(
'calypso_reader_discover_viewed',
{},
{ pathnameOverride: `${ currentRoute }?${ currentQueryArgs }` }
);
if ( ! isUserLoggedIn( state ) ) {
context.renderHeaderSection = renderHeaderSection;
}
// Handle both old query parameter-based routing and new path-based routing.
let selectedTab = DEFAULT_TAB;
// Extract the tab from the path for v2, ignoring query params.
const cleanPath = context.path.split( '?' )[ 0 ];
// Remove any locale prefix if it exists to get a clean path.
const pathWithoutLocale = removeLocaleFromPathLocaleInFront( cleanPath );
const pathParts = pathWithoutLocale.split( '/' );
// Now pathParts[2] will consistently be the tab.
selectedTab = pathParts[ 2 ] || DEFAULT_TAB;
const tabTitle = getSelectedTabTitle( selectedTab );
context.primary = (
<>
<DiscoverDocumentHead tabTitle={ tabTitle } />
<AsyncLoad
require="calypso/reader/discover/discover-stream"
key="discover-page"
streamKey={ streamKey }
title="Discover"
trackScrollPage={ trackScrollPage.bind(
null,
basePath,
fullAnalyticsPageTitle,
ANALYTICS_PAGE_TITLE,
mcKey
) }
onUpdatesShown={ trackUpdatesLoaded.bind( null, mcKey ) }
suppressSiteNameLink
isDiscoverStream
useCompactCards
className="is-discover-stream"
selectedTab={ selectedTab }
query={ context.query }
/>
</>
);
next();
};
export default function ( router ) {
const anyLangParam = getAnyLanguageRouteParam();
const commonMiddleware = [
redirectInvalidLanguage,
redirectWithoutLocaleParamInFrontIfLoggedIn,
setLocaleMiddleware(),
sidebar,
discover,
makeLayout,
clientRender,
];
// Must be logged in to access.
router(
[
'/discover/add-new',
'/discover/reddit',
`/${ anyLangParam }/discover/add-new`,
`/${ anyLangParam }/discover/reddit`,
],
redirectLoggedOutToSignup,
...commonMiddleware
);
router(
[
'/discover',
'/discover/firstposts',
'/discover/tags',
'/discover/latest',
`/${ anyLangParam }/discover`,
`/${ anyLangParam }/discover/firstposts`,
`/${ anyLangParam }/discover/tags`,
`/${ anyLangParam }/discover/latest`,
],
...commonMiddleware
);
}
|