From 9068a6415df4bf50bdb9200ebd17269b9b22e184 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 17 Oct 2021 21:44:39 -0400 Subject: [PATCH] Fixing cross-posts showing on initial load. Fixes #457 (#464) --- src/shared/components/post/post-listings.tsx | 37 +++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/shared/components/post/post-listings.tsx b/src/shared/components/post/post-listings.tsx index 12d5193..8bdb569 100644 --- a/src/shared/components/post/post-listings.tsx +++ b/src/shared/components/post/post-listings.tsx @@ -13,18 +13,35 @@ interface PostListingsProps { enableNsfw: boolean; } -export class PostListings extends Component { - private duplicatesMap = new Map(); +interface PostListingsState { + posts: PostView[]; +} + +export class PostListings extends Component< + PostListingsProps, + PostListingsState +> { + duplicatesMap = new Map(); + + private emptyState: PostListingsState = { + posts: [], + }; constructor(props: any, context: any) { super(props, context); + this.state = this.emptyState; + if (this.props.removeDuplicates) { + this.state.posts = this.removeDuplicates(); + } else { + this.state.posts = this.props.posts; + } } render() { return (
- {this.props.posts.length > 0 ? ( - this.outer().map(post_view => ( + {this.state.posts.length > 0 ? ( + this.state.posts.map(post_view => ( <> { ); } - outer(): PostView[] { - let out = this.props.posts; - if (this.props.removeDuplicates) { - out = this.removeDuplicates(out); - } - - return out; - } + removeDuplicates(): PostView[] { + // Must use a spread to clone the props, because splice will fail below otherwise. + let posts = [...this.props.posts]; - removeDuplicates(posts: PostView[]): PostView[] { // A map from post url to list of posts (dupes) let urlMap = new Map(); -- 2.44.1