]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listings.tsx
Re-organized components folder. (#339)
[lemmy-ui.git] / src / shared / components / post / post-listings.tsx
1 import { Component } from "inferno";
2 import { T } from "inferno-i18next";
3 import { Link } from "inferno-router";
4 import { PostView } from "lemmy-js-client";
5 import { i18n } from "../../i18next";
6 import { PostListing } from "./post-listing";
7
8 interface PostListingsProps {
9   posts: PostView[];
10   showCommunity?: boolean;
11   removeDuplicates?: boolean;
12   enableDownvotes: boolean;
13   enableNsfw: boolean;
14 }
15
16 export class PostListings extends Component<PostListingsProps, any> {
17   private duplicatesMap = new Map<number, PostView[]>();
18
19   constructor(props: any, context: any) {
20     super(props, context);
21   }
22
23   render() {
24     return (
25       <div>
26         {this.props.posts.length > 0 ? (
27           this.outer().map(post_view => (
28             <>
29               <PostListing
30                 post_view={post_view}
31                 duplicates={this.duplicatesMap.get(post_view.post.id)}
32                 showCommunity={this.props.showCommunity}
33                 enableDownvotes={this.props.enableDownvotes}
34                 enableNsfw={this.props.enableNsfw}
35               />
36               <hr class="my-3" />
37             </>
38           ))
39         ) : (
40           <>
41             <div>{i18n.t("no_posts")}</div>
42             {this.props.showCommunity !== undefined && (
43               <T i18nKey="subscribe_to_communities">
44                 #<Link to="/communities">#</Link>
45               </T>
46             )}
47           </>
48         )}
49       </div>
50     );
51   }
52
53   outer(): PostView[] {
54     let out = this.props.posts;
55     if (this.props.removeDuplicates) {
56       out = this.removeDuplicates(out);
57     }
58
59     return out;
60   }
61
62   removeDuplicates(posts: PostView[]): PostView[] {
63     // A map from post url to list of posts (dupes)
64     let urlMap = new Map<string, PostView[]>();
65
66     // Loop over the posts, find ones with same urls
67     for (let pv of posts) {
68       if (
69         pv.post.url &&
70         !pv.post.deleted &&
71         !pv.post.removed &&
72         !pv.community.deleted &&
73         !pv.community.removed
74       ) {
75         if (!urlMap.get(pv.post.url)) {
76           urlMap.set(pv.post.url, [pv]);
77         } else {
78           urlMap.get(pv.post.url).push(pv);
79         }
80       }
81     }
82
83     // Sort by oldest
84     // Remove the ones that have no length
85     for (let e of urlMap.entries()) {
86       if (e[1].length == 1) {
87         urlMap.delete(e[0]);
88       } else {
89         e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
90       }
91     }
92
93     for (let i = 0; i < posts.length; i++) {
94       let pv = posts[i];
95       if (pv.post.url) {
96         let found = urlMap.get(pv.post.url);
97         if (found) {
98           // If its the oldest, add
99           if (pv.post.id == found[0].post.id) {
100             this.duplicatesMap.set(pv.post.id, found.slice(1));
101           }
102           // Otherwise, delete it
103           else {
104             posts.splice(i--, 1);
105           }
106         }
107       }
108     }
109
110     return posts;
111   }
112 }