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}
31 <hr class="d-md-none my-2" />
32 <div class="d-none d-md-block my-2"></div>
37 <div>{i18n.t('no_posts')}</div>
38 {this.props.showCommunity !== undefined && (
39 <T i18nKey="subscribe_to_communities">
40 #<Link to="/communities">#</Link>
49 outer(): Array<Post> {
50 let out = this.props.posts;
51 if (this.props.removeDuplicates) {
52 out = this.removeDuplicates(out);
55 if (this.props.sort !== undefined) {
56 postSort(out, this.props.sort);
62 removeDuplicates(posts: Array<Post>): Array<Post> {
63 // A map from post url to list of posts (dupes)
64 let urlMap = new Map<string, Array<Post>>();
66 // Loop over the posts, find ones with same urls
67 for (let post of posts) {
72 !post.community_deleted &&
73 !post.community_removed
75 if (!urlMap.get(post.url)) {
76 urlMap.set(post.url, [post]);
78 urlMap.get(post.url).push(post);
84 // Remove the ones that have no length
85 for (let e of urlMap.entries()) {
86 if (e[1].length == 1) {
89 e[1].sort((a, b) => a.published.localeCompare(b.published));
93 for (let i = 0; i < posts.length; i++) {
96 let found = urlMap.get(post.url);
98 // If its the oldest, add
99 if (post.id == found[0].id) {
100 post.duplicates = found.slice(1);
102 // Otherwise, delete it
104 posts.splice(i--, 1);