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;
14 enableDownvotes: boolean;
18 export class PostListings extends Component<PostListingsProps, any> {
19 constructor(props: any, context: any) {
20 super(props, context);
26 {this.props.posts.length > 0 ? (
27 this.outer().map(post => (
31 showCommunity={this.props.showCommunity}
32 enableDownvotes={this.props.enableDownvotes}
33 enableNsfw={this.props.enableNsfw}
40 <div>{i18n.t('no_posts')}</div>
41 {this.props.showCommunity !== undefined && (
42 <T i18nKey="subscribe_to_communities">
43 #<Link to="/communities">#</Link>
52 outer(): Array<Post> {
53 let out = this.props.posts;
54 if (this.props.removeDuplicates) {
55 out = this.removeDuplicates(out);
58 if (this.props.sort !== undefined) {
59 postSort(out, this.props.sort, this.props.showCommunity == undefined);
65 removeDuplicates(posts: Array<Post>): Array<Post> {
66 // A map from post url to list of posts (dupes)
67 let urlMap = new Map<string, Array<Post>>();
69 // Loop over the posts, find ones with same urls
70 for (let post of posts) {
75 !post.community_deleted &&
76 !post.community_removed
78 if (!urlMap.get(post.url)) {
79 urlMap.set(post.url, [post]);
81 urlMap.get(post.url).push(post);
87 // Remove the ones that have no length
88 for (let e of urlMap.entries()) {
89 if (e[1].length == 1) {
92 e[1].sort((a, b) => a.published.localeCompare(b.published));
96 for (let i = 0; i < posts.length; i++) {
99 let found = urlMap.get(post.url);
101 // If its the oldest, add
102 if (post.id == found[0].id) {
103 post.duplicates = found.slice(1);
105 // Otherwise, delete it
107 posts.splice(i--, 1);