File size: 2,421 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 ConnectedReaderSubscriptionListItem from 'calypso/blocks/reader-subscription-list-item/connected';
import '../style.scss';

interface PopularSitesSidebarProps {
	followSource: string;
	items: PopularSiteItemProp[];
	title?: string;
}

/**
 * Represents a popular site item which is passed as a prop to the ReaderPopularSitesSidebar component.
 */
interface PopularSiteItemProp {
	blogId: number;
	feed_ID: number;
	feed_URL: string;
	site_name: string;
	site_description: string;
	site_icon: string;
	url: string;
}

/**
 * Represents a popular site which is displayed in the ReaderPopularSitesSidebar component.
 */
interface ReaderPopularSite {
	blog_ID: number;
	description: string;
	feed_ID: number;
	name: string;
	site_icon: string;
	URL: string;
}

function unescape( str: string ): string {
	return str.replace( /&#(\d+);/g, ( match, entity ) => String.fromCharCode( entity ) );
}

/**
 * Converts a popular site item, provided as a prop, into a popular site object for display in the ReaderPopularSitesSidebar component.
 */
const getPopularSiteFromItem = ( item: PopularSiteItemProp ): ReaderPopularSite | null => {
	if ( item.site_name === undefined || item.site_description === undefined ) {
		return null;
	}

	return {
		feed_ID: item.feed_ID,
		blog_ID: item.blogId,
		URL: item.feed_URL ?? item.url,
		name: unescape( item?.site_name ),
		site_icon: item.site_icon ?? null,
		description: unescape( item?.site_description ),
	};
};

const ReaderPopularSitesSidebar = ( props: PopularSitesSidebarProps ) => {
	const { followSource, items, title } = props;
	const sites: ReaderPopularSite[] = items
		.map( ( item ): ReaderPopularSite | null => getPopularSiteFromItem( item ) )
		.filter( ( site ): site is ReaderPopularSite => site !== null );

	const popularSitesLinks: JSX.Element[] = sites.map( ( site ) => (
		<ConnectedReaderSubscriptionListItem
			key={ site.feed_ID }
			feedId={ site.feed_ID }
			siteId={ site.blog_ID }
			site={ site }
			url={ site.URL }
			showLastUpdatedDate={ false }
			showNotificationSettings={ false }
			showFollowedOnDate={ false }
			followSource={ followSource }
		/>
	) );

	if ( ! popularSitesLinks.length ) {
		return null;
	}

	return (
		<>
			{ title && <h2 className="reader-tag-sidebar-title">{ title }</h2> }
			<div className="reader-tag-sidebar-recommended-sites">{ popularSitesLinks }</div>
		</>
	);
};

export default ReaderPopularSitesSidebar;