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 { Language, PostView } from "lemmy-js-client";
6 import { i18n } from "../../i18next";
7 import { PostListing } from "./post-listing";
9 interface PostListingsProps {
11 allLanguages: Language[];
12 showCommunity?: boolean;
13 removeDuplicates?: boolean;
14 enableDownvotes: boolean;
18 export class PostListings extends Component<PostListingsProps, any> {
19 duplicatesMap = new Map<number, PostView[]>();
21 constructor(props: any, context: any) {
22 super(props, context);
26 return this.props.removeDuplicates
27 ? this.removeDuplicates()
34 {this.posts.length > 0 ? (
35 this.posts.map(post_view => (
39 duplicates={Some(this.duplicatesMap.get(post_view.post.id))}
42 showCommunity={this.props.showCommunity}
43 enableDownvotes={this.props.enableDownvotes}
44 enableNsfw={this.props.enableNsfw}
45 allLanguages={this.props.allLanguages}
47 <hr className="my-3" />
52 <div>{i18n.t("no_posts")}</div>
53 {this.props.showCommunity && (
54 <T i18nKey="subscribe_to_communities">
55 #<Link to="/communities">#</Link>
64 removeDuplicates(): PostView[] {
65 // Must use a spread to clone the props, because splice will fail below otherwise.
66 let posts = [...this.props.posts];
68 // A map from post url to list of posts (dupes)
69 let urlMap = new Map<string, PostView[]>();
71 // Loop over the posts, find ones with same urls
72 for (let pv of posts) {
75 !pv.community.deleted &&
76 !pv.community.removed &&
79 if (!urlMap.get(url)) {
80 urlMap.set(url, [pv]);
82 urlMap.get(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++) {
103 let found = urlMap.get(url);
105 // If its the oldest, add
106 if (pv.post.id == found[0].post.id) {
107 this.duplicatesMap.set(pv.post.id, found.slice(1));
109 // Otherwise, delete it
111 posts.splice(i--, 1);