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