1 import { Component } from "inferno";
2 import { Link } from "inferno-router";
3 import { PostView } from "lemmy-js-client";
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;
12 enableDownvotes: boolean;
16 export class PostListings extends Component<PostListingsProps, any> {
17 private duplicatesMap = new Map<number, PostView[]>();
19 constructor(props: any, context: any) {
20 super(props, context);
26 {this.props.posts.length > 0 ? (
27 this.outer().map(post_view => (
31 duplicates={this.duplicatesMap.get(post_view.post.id)}
32 showCommunity={this.props.showCommunity}
33 enableDownvotes={this.props.enableDownvotes}
34 enableNsfw={this.props.enableNsfw}
41 <div>{i18n.t("no_posts")}</div>
42 {this.props.showCommunity !== undefined && (
43 <T i18nKey="subscribe_to_communities">
44 #<Link to="/communities">#</Link>
54 let out = this.props.posts;
55 if (this.props.removeDuplicates) {
56 out = this.removeDuplicates(out);
62 removeDuplicates(posts: PostView[]): PostView[] {
63 // A map from post url to list of posts (dupes)
64 let urlMap = new Map<string, PostView[]>();
66 // Loop over the posts, find ones with same urls
67 for (let pv of posts) {
72 !pv.community.deleted &&
75 if (!urlMap.get(pv.post.url)) {
76 urlMap.set(pv.post.url, [pv]);
78 urlMap.get(pv.post.url).push(pv);
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.post.published.localeCompare(b.post.published));
93 for (let i = 0; i < posts.length; i++) {
96 let found = urlMap.get(pv.post.url);
98 // If its the oldest, add
99 if (pv.post.id == found[0].post.id) {
100 this.duplicatesMap.set(pv.post.id, found.slice(1));
102 // Otherwise, delete it
104 posts.splice(i--, 1);