1 import { Component } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Post, SortType } from '../interfaces';
4 import { postSort } from '../utils';
5 import { PostListing } from './post-listing';
6 import { i18n } from '../i18next';
7 import { T } from 'inferno-i18next';
9 interface PostListingsProps {
11 showCommunity?: boolean;
12 removeDuplicates?: boolean;
16 export class PostListings extends Component<PostListingsProps, any> {
17 constructor(props: any, context: any) {
18 super(props, context);
24 {this.props.posts.length > 0 ? (
25 this.outer().map(post => (
29 showCommunity={this.props.showCommunity}
36 <div>{i18n.t('no_posts')}</div>
37 {this.props.showCommunity !== undefined && (
38 <T i18nKey="subscribe_to_communities">
39 #<Link to="/communities">#</Link>
48 outer(): Array<Post> {
49 let out = this.props.posts;
50 if (this.props.removeDuplicates) {
51 out = this.removeDuplicates(out);
54 if (this.props.sort !== undefined) {
55 postSort(out, this.props.sort, this.props.showCommunity == undefined);
61 removeDuplicates(posts: Array<Post>): Array<Post> {
62 // A map from post url to list of posts (dupes)
63 let urlMap = new Map<string, Array<Post>>();
65 // Loop over the posts, find ones with same urls
66 for (let post of posts) {
71 !post.community_deleted &&
72 !post.community_removed
74 if (!urlMap.get(post.url)) {
75 urlMap.set(post.url, [post]);
77 urlMap.get(post.url).push(post);
83 // Remove the ones that have no length
84 for (let e of urlMap.entries()) {
85 if (e[1].length == 1) {
88 e[1].sort((a, b) => a.published.localeCompare(b.published));
92 for (let i = 0; i < posts.length; i++) {
95 let found = urlMap.get(post.url);
97 // If its the oldest, add
98 if (post.id == found[0].id) {
99 post.duplicates = found.slice(1);
101 // Otherwise, delete it
103 posts.splice(i--, 1);