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 siteLanguages: number[];
13 showCommunity?: boolean;
14 removeDuplicates?: boolean;
15 enableDownvotes: boolean;
19 export class PostListings extends Component<PostListingsProps, any> {
20 duplicatesMap = new Map<number, PostView[]>();
22 constructor(props: any, context: any) {
23 super(props, context);
27 return this.props.removeDuplicates
28 ? this.removeDuplicates()
35 {this.posts.length > 0 ? (
36 this.posts.map(post_view => (
40 duplicates={Some(this.duplicatesMap.get(post_view.post.id))}
43 showCommunity={this.props.showCommunity}
44 enableDownvotes={this.props.enableDownvotes}
45 enableNsfw={this.props.enableNsfw}
46 allLanguages={this.props.allLanguages}
47 siteLanguages={this.props.siteLanguages}
49 <hr className="my-3" />
54 <div>{i18n.t("no_posts")}</div>
55 {this.props.showCommunity && (
56 <T i18nKey="subscribe_to_communities">
57 #<Link to="/communities">#</Link>
66 removeDuplicates(): PostView[] {
67 // Must use a spread to clone the props, because splice will fail below otherwise.
68 let posts = [...this.props.posts];
70 // A map from post url to list of posts (dupes)
71 let urlMap = new Map<string, PostView[]>();
73 // Loop over the posts, find ones with same urls
74 for (let pv of posts) {
77 !pv.community.deleted &&
78 !pv.community.removed &&
81 if (!urlMap.get(url)) {
82 urlMap.set(url, [pv]);
84 urlMap.get(url).push(pv);
92 // Remove the ones that have no length
93 for (let e of urlMap.entries()) {
94 if (e[1].length == 1) {
97 e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
101 for (let i = 0; i < posts.length; i++) {
105 let found = urlMap.get(url);
107 // If its the oldest, add
108 if (pv.post.id == found[0].post.id) {
109 this.duplicatesMap.set(pv.post.id, found.slice(1));
111 // Otherwise, delete it
113 posts.splice(i--, 1);