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