1 import { Component } from "inferno";
2 import { T } from "inferno-i18next-dess";
3 import { Link } from "inferno-router";
4 import { Language, PostView } from "lemmy-js-client";
5 import { i18n } from "../../i18next";
6 import { PostListing } from "./post-listing";
8 interface PostListingsProps {
10 allLanguages: Language[];
11 siteLanguages: number[];
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={this.duplicatesMap.get(post_view.post.id)}
40 showCommunity={this.props.showCommunity}
41 enableDownvotes={this.props.enableDownvotes}
42 enableNsfw={this.props.enableNsfw}
43 allLanguages={this.props.allLanguages}
44 siteLanguages={this.props.siteLanguages}
46 <hr className="my-3" />
51 <div>{i18n.t("no_posts")}</div>
52 {this.props.showCommunity && (
53 <T i18nKey="subscribe_to_communities">
54 #<Link to="/communities">#</Link>
63 removeDuplicates(): PostView[] {
64 // Must use a spread to clone the props, because splice will fail below otherwise.
65 const posts = [...this.props.posts];
67 // A map from post url to list of posts (dupes)
68 const urlMap = new Map<string, PostView[]>();
70 // Loop over the posts, find ones with same urls
71 for (const pv of posts) {
72 const url = pv.post.url;
76 !pv.community.deleted &&
77 !pv.community.removed &&
80 if (!urlMap.get(url)) {
81 urlMap.set(url, [pv]);
83 urlMap.get(url)?.push(pv);
89 // Remove the ones that have no length
90 for (const e of urlMap.entries()) {
91 if (e[1].length == 1) {
94 e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
98 for (let i = 0; i < posts.length; i++) {
100 const url = pv.post.url;
102 const found = urlMap.get(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);