File size: 2,499 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
import { useTranslate } from 'i18n-calypso';
import { useSelector } from 'react-redux';
import QueryReaderListItems from 'calypso/components/data/query-reader-list-items';
import EmptyContent from 'calypso/components/empty-content';
import { getListItems } from 'calypso/state/reader/lists/selectors';
import getPreviousRoute from 'calypso/state/selectors/get-previous-route';

interface ListEmptyContentProps {
	list?: {
		title: string;
		owner: string;
		slug: string;
		is_owner: boolean;
		ID: number;
	};
}

export default function ListEmptyContent( { list }: ListEmptyContentProps ): JSX.Element {
	const translate = useTranslate();
	const previousRoute: string = useSelector( getPreviousRoute );
	const listItems = useSelector( ( state ) =>
		list ? getListItems( state, list.ID ) : undefined
	);
	const isEmptyList = ! listItems?.length;
	const shouldPromptManagement = isEmptyList && list?.is_owner;
	const isRecommendedBlogsList = list?.slug === 'recommended-blogs';

	function previousRouteIsUserProfileLists(): boolean {
		return /^\/reader\/users\/[a-z0-9]+\/lists\??$/.test( previousRoute );
	}

	function getActionBtnLink(): string {
		if ( shouldPromptManagement ) {
			return `/reader/list/${ list?.owner }/${ list?.slug }/edit/items`;
		}

		return previousRouteIsUserProfileLists() ? previousRoute : '/reader';
	}

	function getActionBtnText(): string {
		if ( shouldPromptManagement ) {
			return isRecommendedBlogsList ? translate( 'Add recommendations' ) : translate( 'Add items' );
		}

		return previousRouteIsUserProfileLists()
			? translate( 'Back to User Profile' )
			: translate( 'Back to Following' );
	}

	function getTitle(): string {
		if ( shouldPromptManagement ) {
			return isRecommendedBlogsList
				? translate( 'No recommendations' )
				: translate( 'This list is empty' );
		}

		return translate( 'No recent posts' );
	}

	function getLine(): string {
		if ( shouldPromptManagement ) {
			return isRecommendedBlogsList
				? translate( 'You have not recommended any blogs yet.' )
				: translate( 'You have not added any items to this list.' );
		}

		return translate( 'The sites in this list have not posted anything recently.' );
	}

	const action = (
		<a className="empty-content__action button is-primary" href={ getActionBtnLink() }>
			{ getActionBtnText() }
		</a>
	);

	return (
		<>
			<QueryReaderListItems owner={ list?.owner } slug={ list?.slug } />
			<EmptyContent title={ getTitle() } line={ getLine() } action={ action } />
		</>
	);
}