1 import { None, Some } from "@sniptt/monads";
2 import { Component } from "inferno";
3 import { T } from "inferno-i18next-dess";
4 import { Link } from "inferno-router";
5 import { PostView } from "lemmy-js-client";
6 import { i18n } from "../../i18next";
7 import { PostListing } from "./post-listing";
9 interface PostListingsProps {
11 showCommunity?: boolean;
12 removeDuplicates?: boolean;
13 enableDownvotes: boolean;
17 export class PostListings extends Component<PostListingsProps, any> {
18 duplicatesMap = new Map<number, PostView[]>();
20 constructor(props: any, context: any) {
21 super(props, context);
25 return this.props.removeDuplicates
26 ? this.removeDuplicates()
33 {this.posts.length > 0 ? (
34 this.posts.map(post_view => (
38 duplicates={Some(this.duplicatesMap.get(post_view.post.id))}
41 showCommunity={this.props.showCommunity}
42 enableDownvotes={this.props.enableDownvotes}
43 enableNsfw={this.props.enableNsfw}
50 <div>{i18n.t("no_posts")}</div>
51 {this.props.showCommunity && (
52 <T i18nKey="subscribe_to_communities">
53 #<Link to="/communities">#</Link>
62 removeDuplicates(): PostView[] {
63 // Must use a spread to clone the props, because splice will fail below otherwise.
64 let posts = [...this.props.posts];
66 // A map from post url to list of posts (dupes)
67 let urlMap = new Map<string, PostView[]>();
69 // Loop over the posts, find ones with same urls
70 for (let pv of posts) {
73 !pv.community.deleted &&
74 !pv.community.removed &&
77 if (!urlMap.get(url)) {
78 urlMap.set(url, [pv]);
80 urlMap.get(url).push(pv);
88 // Remove the ones that have no length
89 for (let e of urlMap.entries()) {
90 if (e[1].length == 1) {
93 e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
97 for (let i = 0; i < posts.length; i++) {
101 let found = urlMap.get(url);
103 // If its the oldest, add
104 if (pv.post.id == found[0].post.id) {
105 this.duplicatesMap.set(pv.post.id, found.slice(1));
107 // Otherwise, delete it
109 posts.splice(i--, 1);