Fixing cross-posts showing on initial load. Fixes #457

This commit is contained in:
Dessalines 2021-10-17 21:40:50 -04:00
parent 99c9a608d9
commit e038e763ec

View file

@ -13,18 +13,35 @@ interface PostListingsProps {
enableNsfw: boolean; enableNsfw: boolean;
} }
export class PostListings extends Component<PostListingsProps, any> { interface PostListingsState {
private duplicatesMap = new Map<number, PostView[]>(); posts: PostView[];
}
export class PostListings extends Component<
PostListingsProps,
PostListingsState
> {
duplicatesMap = new Map<number, PostView[]>();
private emptyState: PostListingsState = {
posts: [],
};
constructor(props: any, context: any) { constructor(props: any, context: any) {
super(props, context); super(props, context);
this.state = this.emptyState;
if (this.props.removeDuplicates) {
this.state.posts = this.removeDuplicates();
} else {
this.state.posts = this.props.posts;
}
} }
render() { render() {
return ( return (
<div> <div>
{this.props.posts.length > 0 ? ( {this.state.posts.length > 0 ? (
this.outer().map(post_view => ( this.state.posts.map(post_view => (
<> <>
<PostListing <PostListing
post_view={post_view} post_view={post_view}
@ -50,16 +67,10 @@ export class PostListings extends Component<PostListingsProps, any> {
); );
} }
outer(): PostView[] { removeDuplicates(): PostView[] {
let out = this.props.posts; // Must use a spread to clone the props, because splice will fail below otherwise.
if (this.props.removeDuplicates) { let posts = [...this.props.posts];
out = this.removeDuplicates(out);
}
return out;
}
removeDuplicates(posts: PostView[]): PostView[] {
// A map from post url to list of posts (dupes) // A map from post url to list of posts (dupes)
let urlMap = new Map<string, PostView[]>(); let urlMap = new Map<string, PostView[]>();