File size: 3,249 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
import { SelectDropdown } from '@automattic/components';
import { SubscriptionManager, Reader } from '@automattic/data-stores';
import SearchInput from '@automattic/search';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import { CommentList } from 'calypso/landing/subscriptions/components/comment-list';
import { SearchIcon } from 'calypso/landing/subscriptions/components/icons';
import { Notice, NoticeType } from 'calypso/landing/subscriptions/components/notice';
import { SortControls, Option } from 'calypso/landing/subscriptions/components/sort-controls';
import { getOptionLabel } from 'calypso/landing/subscriptions/helpers';
import { useSiteSubscriptionsFilterOptions } from 'calypso/landing/subscriptions/hooks';
import TabView from '../tab-view';
import './styles.scss';

const { PostSubscriptionsSortBy: SortBy, SiteSubscriptionsFilterBy: FilterBy } = Reader;

const useSortOptions = () => {
	const translate = useTranslate();

	return [
		{ value: SortBy.RecentlySubscribed, label: translate( 'Recently subscribed' ) },
		{ value: SortBy.PostName, label: translate( 'Post name' ) },
	];
};

const Comments = () => {
	const translate = useTranslate();
	const [ sortTerm, setSortTerm ] = useState( SortBy.RecentlySubscribed );
	const [ searchTerm, setSearchTerm ] = useState< string >();
	const sortOptions = useSortOptions();
	const availableFilterOptions = useSiteSubscriptionsFilterOptions();
	const [ filterOption, setFilterOption ] = useState( FilterBy.All );

	const {
		data: { posts, totalCount },
		isLoading,
		error,
	} = SubscriptionManager.usePostSubscriptionsQuery( { searchTerm, sortTerm, filterOption } );

	// todo: translate when we have agreed on the error message
	const errorMessage = error ? 'An error occurred while fetching your subscriptions.' : '';

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

	return (
		<TabView errorMessage={ errorMessage } isLoading={ isLoading }>
			<div className="comments-list-actions-bar">
				<SearchInput
					delaySearch
					placeholder={ translate( 'Search by post, site title, or address…' ) }
					searchIcon={ <SearchIcon size={ 18 } /> }
					onSearch={ setSearchTerm }
				/>

				<SelectDropdown
					className="list-actions-bar__filter-control list-actions-bar__spacer"
					options={ availableFilterOptions }
					onSelect={ ( selectedOption: Option< Reader.SiteSubscriptionsFilterBy > ) =>
						setFilterOption( selectedOption.value as Reader.SiteSubscriptionsFilterBy )
					}
					selectedText={ translate( 'View: %s', {
						args: getOptionLabel( availableFilterOptions, filterOption ) || '',
					} ) }
				/>

				<SortControls options={ sortOptions } value={ sortTerm } onChange={ setSortTerm } />
			</div>

			<CommentList posts={ posts } />

			{ totalCount && posts?.length === 0 && (
				<Notice type={ NoticeType.Warning }>
					{ translate( 'Sorry, no posts match {{italic}}%s.{{/italic}}', {
						components: { italic: <i /> },
						args: searchTerm || getOptionLabel( availableFilterOptions, filterOption ),
					} ) }
				</Notice>
			) }
		</TabView>
	);
};

export default Comments;