1 import { Component } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { PostView, SortType } from 'lemmy-js-client';
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 private duplicatesMap = new Map<number, PostView[]>();
21 constructor(props: any, context: any) {
22 super(props, context);
28 {this.props.posts.length > 0 ? (
29 this.outer().map(post_view => (
33 duplicates={this.duplicatesMap.get(post_view.post.id)}
34 showCommunity={this.props.showCommunity}
35 enableDownvotes={this.props.enableDownvotes}
36 enableNsfw={this.props.enableNsfw}
43 <div>{i18n.t('no_posts')}</div>
44 {this.props.showCommunity !== undefined && (
45 <T i18nKey="subscribe_to_communities">
46 #<Link to="/communities">#</Link>
56 let out = this.props.posts;
57 if (this.props.removeDuplicates) {
58 out = this.removeDuplicates(out);
61 if (this.props.sort !== undefined) {
62 postSort(out, this.props.sort, this.props.showCommunity == undefined);
68 removeDuplicates(posts: PostView[]): PostView[] {
69 // A map from post url to list of posts (dupes)
70 let urlMap = new Map<string, PostView[]>();
72 // Loop over the posts, find ones with same urls
73 for (let pv of posts) {
78 !pv.community.deleted &&
81 if (!urlMap.get(pv.post.url)) {
82 urlMap.set(pv.post.url, [pv]);
84 urlMap.get(pv.post.url).push(pv);
90 // Remove the ones that have no length
91 for (let e of urlMap.entries()) {
92 if (e[1].length == 1) {
95 e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
99 for (let i = 0; i < posts.length; i++) {
102 let found = urlMap.get(pv.post.url);
104 // If its the oldest, add
105 if (pv.post.id == found[0].post.id) {
106 this.duplicatesMap.set(pv.post.id, found.slice(1));
108 // Otherwise, delete it
110 posts.splice(i--, 1);