File size: 4,399 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
import { SubscriptionManager } from '@automattic/data-stores';
import { Spinner, __experimentalHStack as HStack, Icon, Tooltip } from '@wordpress/components';
import { info } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getCurrentUserName } from 'calypso/state/current-user/selectors';
import { requestRecommendedBlogsListItems } from 'calypso/state/reader/lists/actions';
import { Notice, NoticeType } from '../notice';
import SiteSubscriptionRow from './site-subscription-row';
import './styles/site-subscriptions-list.scss';

type SiteSubscriptionsListProps = {
	emptyComponent?: React.ComponentType;
	notFoundComponent?: React.ComponentType;
	layout?: 'full' | 'compact';
};

const SiteSubscriptionsList: React.FC< SiteSubscriptionsListProps > = ( {
	emptyComponent: EmptyComponent,
	notFoundComponent: NotFoundComponent,
	layout = 'full',
} ) => {
	const translate = useTranslate();
	const dispatch = useDispatch();
	const currentUserName = useSelector( getCurrentUserName );
	const { isLoggedIn } = SubscriptionManager.useIsLoggedIn();
	const { filterOption, searchTerm } = SubscriptionManager.useSiteSubscriptionsQueryProps();
	const { data, isLoading, error } = SubscriptionManager.useSiteSubscriptionsQuery();
	const { subscriptions, totalCount } = data;

	const isCompactLayout = layout === 'compact';

	// Fetch recommended blogs data once for all subscription rows
	useEffect( () => {
		if ( currentUserName ) {
			dispatch( requestRecommendedBlogsListItems( currentUserName ) );
		}
	}, [ currentUserName, dispatch ] );

	if ( error ) {
		return (
			<Notice type={ NoticeType.Error }>
				{ translate(
					'We had a small hiccup loading your subscriptions. Please try refreshing the page.'
				) }
			</Notice>
		);
	}

	if ( isLoading ) {
		return (
			<div className="loading-container">
				<Spinner />
			</div>
		);
	}

	if ( ! isLoading && ! totalCount ) {
		if ( EmptyComponent ) {
			return <EmptyComponent />;
		}
		return (
			<Notice type={ NoticeType.Warning }>
				{ translate( 'You are not subscribed to any sites.' ) }
			</Notice>
		);
	}

	if ( totalCount > 0 && subscriptions.length === 0 ) {
		if ( NotFoundComponent ) {
			return <NotFoundComponent />;
		}
		return (
			<Notice type={ NoticeType.Warning }>
				{ translate( 'Sorry, no sites match {{italic}}%s.{{/italic}}', {
					components: { italic: <i /> },
					args: searchTerm || filterOption,
				} ) }
			</Notice>
		);
	}

	return (
		<ul
			className={ `site-subscriptions-list${
				isCompactLayout ? ' site-subscriptions-list--compact' : ''
			}` }
			role="table"
		>
			<HStack className="row header" role="row" as="li" alignment="center">
				<span className="title-cell" role="columnheader">
					{ translate( 'Subscribed site' ) }
				</span>
				<span className="date-cell" role="columnheader">
					{ translate( 'Since' ) }
				</span>
				{ isLoggedIn && ! isCompactLayout && (
					<span className="new-posts-cell" role="columnheader">
						{ translate( 'New posts' ) }
					</span>
				) }
				{ isLoggedIn && ! isCompactLayout && (
					<span className="new-comments-cell" role="columnheader">
						{ translate( 'New comments' ) }
					</span>
				) }
				<span className="email-frequency-cell" role="columnheader">
					{ translate( 'Email frequency' ) }
				</span>
				{ isLoggedIn && ! isCompactLayout && (
					<span className="recommend-cell" role="columnheader">
						{ translate( 'Recommend' ) }
						<Tooltip
							className="site-subscriptions-list__recommend-tooltip"
							text={ translate(
								'Recommending a blog adds it to your profile and helps it to be discovered in the Reader.'
							) }
						>
							<span>
								<Icon
									className="site-subscriptions-list__recommend-tooltip-icon"
									icon={ info }
									size={ 16 }
								/>
							</span>
						</Tooltip>
					</span>
				) }
				<span className="unsubscribe-action-cell" role="columnheader">
					{ translate( 'Action' ) }
				</span>
				<span className="actions-cell" role="columnheader" />
			</HStack>
			{ subscriptions.map( ( siteSubscription ) => (
				<SiteSubscriptionRow
					key={ `sites.siteRow.${ siteSubscription.ID }` }
					layout={ layout }
					{ ...siteSubscription }
				/>
			) ) }
		</ul>
	);
};

export default SiteSubscriptionsList;