File size: 1,571 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
import page from '@automattic/calypso-router';
import debugFactory from 'debug';
import { createElement } from 'react';
import Posts from 'calypso/my-sites/posts/main';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';
import areAllSitesSingleUser from 'calypso/state/selectors/are-all-sites-single-user';
import { isJetpackSite, isSingleUserSite } from 'calypso/state/sites/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';

const debug = debugFactory( 'calypso:my-sites:posts' );

export default {
	posts: function ( context, next ) {
		const state = context.store.getState();
		const siteId = getSelectedSiteId( state );
		const author = context.params.author === 'my' ? getCurrentUserId( state ) : null;
		let search = context.query.s || '';
		const category = context.query.category;
		const tag = context.query.tag;

		function shouldRedirectMyPosts() {
			if ( ! author ) {
				return false;
			}
			if ( areAllSitesSingleUser( state ) ) {
				return true;
			}
			if ( isSingleUserSite( state, siteId ) || isJetpackSite( state, siteId ) ) {
				return true;
			}
		}

		debug( 'author: `%s`', author );

		// Disable search in all-sites mode because it doesn't work.
		if ( ! siteId ) {
			search = '';
		}

		debug( 'search: `%s`', search );

		if ( shouldRedirectMyPosts() ) {
			page.redirect( context.path.replace( /\/my\b/, '' ) );
			return;
		}

		context.primary = createElement( Posts, {
			context,
			author,
			statusSlug: context.params.status,
			search,
			category,
			tag,
		} );
		next();
	},
};