1 import { Component } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Post } from '../interfaces';
4 import { PostListing } from './post-listing';
5 import { i18n } from '../i18next';
6 import { T } from 'inferno-i18next';
8 interface PostListingsProps {
10 showCommunity?: boolean;
11 removeDuplicates?: boolean;
14 export class PostListings extends Component<PostListingsProps, any> {
15 constructor(props: any, context: any) {
16 super(props, context);
22 {this.props.posts.length > 0 ? (
23 (this.props.removeDuplicates
24 ? this.removeDuplicates(this.props.posts)
30 showCommunity={this.props.showCommunity}
32 <hr class="d-md-none my-2" />
33 <div class="d-none d-md-block my-2"></div>
38 <div>{i18n.t('no_posts')}</div>
39 {this.props.showCommunity !== undefined && (
40 <T i18nKey="subscribe_to_communities">
41 #<Link to="/communities">#</Link>
50 removeDuplicates(posts: Array<Post>): Array<Post> {
51 // A map from post url to list of posts (dupes)
52 let urlMap = new Map<string, Array<Post>>();
54 // Loop over the posts, find ones with same urls
55 for (let post of posts) {
60 !post.community_deleted &&
61 !post.community_removed
63 if (!urlMap.get(post.url)) {
64 urlMap.set(post.url, [post]);
66 urlMap.get(post.url).push(post);
72 // Remove the ones that have no length
73 for (let e of urlMap.entries()) {
74 if (e[1].length == 1) {
77 e[1].sort((a, b) => a.published.localeCompare(b.published));
81 for (let i = 0; i < posts.length; i++) {
84 let found = urlMap.get(post.url);
86 // If its the oldest, add
87 if (post.id == found[0].id) {
88 post.duplicates = found.slice(1);
90 // Otherwise, delete it