1 import { Component } from "inferno";
2 import { T } from "inferno-i18next-dess";
3 import { Link } from "inferno-router";
4 import { PostView } from "lemmy-js-client";
5 import { i18n } from "../../i18next";
6 import { PostListing } from "./post-listing";
8 interface PostListingsProps {
10 showCommunity?: boolean;
11 removeDuplicates?: boolean;
12 enableDownvotes: boolean;
16 interface PostListingsState {
20 export class PostListings extends Component<
24 duplicatesMap = new Map<number, PostView[]>();
26 private emptyState: PostListingsState = {
30 constructor(props: any, context: any) {
31 super(props, context);
32 this.state = this.emptyState;
33 if (this.props.removeDuplicates) {
34 this.state.posts = this.removeDuplicates();
36 this.state.posts = this.props.posts;
43 {this.state.posts.length > 0 ? (
44 this.state.posts.map(post_view => (
48 duplicates={this.duplicatesMap.get(post_view.post.id)}
49 showCommunity={this.props.showCommunity}
50 enableDownvotes={this.props.enableDownvotes}
51 enableNsfw={this.props.enableNsfw}
58 <div>{i18n.t("no_posts")}</div>
59 {this.props.showCommunity !== undefined && (
60 <T i18nKey="subscribe_to_communities">
61 #<Link to="/communities">#</Link>
70 removeDuplicates(): PostView[] {
71 // Must use a spread to clone the props, because splice will fail below otherwise.
72 let posts = [...this.props.posts];
74 // A map from post url to list of posts (dupes)
75 let urlMap = new Map<string, PostView[]>();
77 // Loop over the posts, find ones with same urls
78 for (let pv of posts) {
83 !pv.community.deleted &&
86 if (!urlMap.get(pv.post.url)) {
87 urlMap.set(pv.post.url, [pv]);
89 urlMap.get(pv.post.url).push(pv);
95 // Remove the ones that have no length
96 for (let e of urlMap.entries()) {
97 if (e[1].length == 1) {
100 e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
104 for (let i = 0; i < posts.length; i++) {
107 let found = urlMap.get(pv.post.url);
109 // If its the oldest, add
110 if (pv.post.id == found[0].post.id) {
111 this.duplicatesMap.set(pv.post.id, found.slice(1));
113 // Otherwise, delete it
115 posts.splice(i--, 1);