File size: 4,452 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Card } from '@automattic/components';
import { times } from 'lodash';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import AsyncLoad from 'calypso/components/async-load';
import Spotlight from 'calypso/components/spotlight';
import { getMessagePathForJITM } from 'calypso/lib/route';
import PluginBrowserItem from 'calypso/my-sites/plugins/plugins-browser-item';
import { PluginsBrowserElementVariant } from 'calypso/my-sites/plugins/plugins-browser-item/types';
import PluginsResultsHeader from 'calypso/my-sites/plugins/plugins-results-header';
import getCurrentRoute from 'calypso/state/selectors/get-current-route';
import { PluginsBrowserListVariant } from './types';
import './style.scss';

const DEFAULT_PLACEHOLDER_NUMBER = 6;

const PluginsBrowserList = ( {
	plugins,
	variant = PluginsBrowserListVariant.Fixed,
	title,
	subtitle,
	resultCount,
	extended,
	showPlaceholders,
	site,
	currentSites,
	listName,
	listType,
	browseAllLink,
	size,
	search,
	noHeader = false,
} ) => {
	const extendedVariant = extended
		? PluginsBrowserElementVariant.Extended
		: PluginsBrowserElementVariant.Compact;

	const renderPluginsViewList = () => {
		const pluginsViewsList = plugins.map( ( plugin, n ) => {
			// Needs a beter fix but something is leaking empty objects into this list.
			if ( ! plugin?.slug ) {
				return null;
			}
			return (
				<PluginBrowserItem
					site={ site }
					key={ plugin.slug + n }
					gridPosition={ n + 1 }
					plugin={ plugin }
					currentSites={ currentSites }
					listName={ listName }
					listType={ listType }
					variant={ extendedVariant }
					search={ search }
				/>
			);
		} );

		if ( size ) {
			return pluginsViewsList.slice( 0, size );
		}

		return pluginsViewsList;
	};

	const renderPlaceholdersViews = () => {
		return times( size || DEFAULT_PLACEHOLDER_NUMBER, ( i ) => (
			<PluginBrowserItem
				isPlaceholder
				key={ 'placeholder-plugin-' + i }
				variant={ extendedVariant }
			/>
		) );
	};

	const renderViews = () => {
		if ( ! plugins.length ) {
			return renderPlaceholdersViews();
		}

		switch ( variant ) {
			case PluginsBrowserListVariant.InfiniteScroll:
				if ( showPlaceholders ) {
					return renderPluginsViewList().concat( renderPlaceholdersViews() );
				}
				return renderPluginsViewList();
			case PluginsBrowserListVariant.Paginated:
				if ( showPlaceholders ) {
					return renderPlaceholdersViews();
				}
				return renderPluginsViewList();
			case PluginsBrowserListVariant.Fixed:
			default:
				return renderPluginsViewList();
		}
	};

	const SpotlightPlaceholder = (
		<Spotlight
			isPlaceholder
			taglineText="Calypso placeholder"
			illustrationSrc="https://wordpress.com/wp-content/lib/marketplace-images/sensei-pro.svg"
			onClick={ () => {} }
			titleText="This is the default placeholder rendered in Calypso"
			ctaText="Click me"
		/>
	);

	// Get the message path for the current route. This is needed to be able to display JITMs
	const currentRoute = useSelector( getCurrentRoute );
	const sectionJitmPath = getMessagePathForJITM( currentRoute );

	return (
		<div className="plugins-browser-list">
			{ ! noHeader && ( title || subtitle || resultCount || browseAllLink ) && (
				<PluginsResultsHeader
					title={ title }
					subtitle={ subtitle }
					resultCount={ resultCount }
					browseAllLink={ browseAllLink }
					listName={ listName }
					isRootPage={ listType !== 'browse' }
				/>
			) }
			{ listName === 'paid' && (
				<AsyncLoad
					require="calypso/blocks/jitm"
					template="spotlight"
					placeholder={ null }
					messagePath="calypso:plugins:spotlight"
				/>
			) }
			{ listType === 'search' && (
				<AsyncLoad
					require="calypso/blocks/jitm"
					template="spotlight"
					jitmPlaceholder={ SpotlightPlaceholder }
					messagePath="calypso:plugins:search"
					searchQuery={ search }
				/>
			) }
			{ listType === 'browse' && (
				<AsyncLoad
					require="calypso/blocks/jitm"
					template="spotlight"
					jitmPlaceholder={ SpotlightPlaceholder }
					messagePath={ `calypso:${ sectionJitmPath }:spotlight` }
				/>
			) }
			<Card tagName="ul" className="plugins-browser-list__elements">
				{ renderViews() }
			</Card>
		</div>
	);
};

PluginsBrowserList.propTypes = {
	plugins: PropTypes.array.isRequired,
	variant: PropTypes.oneOf( Object.values( PluginsBrowserListVariant ) ).isRequired,
	extended: PropTypes.bool,
};

export default PluginsBrowserList;