lemmy-ui/src/shared/components/person/person-details.tsx

210 lines
5.1 KiB
TypeScript
Raw Normal View History

import { None, Some } from "@sniptt/monads/build";
import { Component } from "inferno";
2020-12-24 01:58:27 +00:00
import {
CommentView,
2021-03-15 18:09:31 +00:00
GetPersonDetailsResponse,
Language,
2021-03-15 18:09:31 +00:00
PersonViewSafe,
PostView,
SortType,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import { CommentViewType, PersonDetailsView } from "../../interfaces";
import { commentsToFlatNodes, setupTippy } from "../../utils";
import { CommentNodes } from "../comment/comment-nodes";
import { Paginator } from "../common/paginator";
import { PostListing } from "../post/post-listing";
2021-03-15 18:09:31 +00:00
interface PersonDetailsProps {
personRes: GetPersonDetailsResponse;
admins: PersonViewSafe[];
allLanguages: Language[];
page: number;
limit: number;
sort: SortType;
enableDownvotes: boolean;
enableNsfw: boolean;
2021-03-15 18:09:31 +00:00
view: PersonDetailsView;
onPageChange(page: number): number | any;
}
2020-12-24 01:58:27 +00:00
enum ItemEnum {
Comment,
Post,
}
type ItemType = {
id: number;
type_: ItemEnum;
view: CommentView | PostView;
published: string;
score: number;
};
2021-03-15 18:09:31 +00:00
export class PersonDetails extends Component<PersonDetailsProps, any> {
constructor(props: any, context: any) {
super(props, context);
this.handlePageChange = this.handlePageChange.bind(this);
}
componentDidMount() {
setupTippy();
}
2020-09-09 00:48:17 +00:00
// TODO wut?
// componentDidUpdate(lastProps: UserDetailsProps) {
// for (const key of Object.keys(lastProps)) {
// if (lastProps[key] !== this.props[key]) {
// this.fetchUserData();
// break;
// }
// }
// }
render() {
return (
<div>
{this.viewSelector(this.props.view)}
<Paginator page={this.props.page} onChange={this.handlePageChange} />
</div>
);
}
2021-03-15 18:09:31 +00:00
viewSelector(view: PersonDetailsView) {
if (
view === PersonDetailsView.Overview ||
view === PersonDetailsView.Saved
) {
return this.overview();
2021-03-15 18:09:31 +00:00
} else if (view === PersonDetailsView.Comments) {
return this.comments();
2021-03-15 18:09:31 +00:00
} else if (view === PersonDetailsView.Posts) {
return this.posts();
2020-09-09 00:48:17 +00:00
} else {
return null;
}
}
2020-12-24 01:58:27 +00:00
renderItemType(i: ItemType) {
switch (i.type_) {
case ItemEnum.Comment: {
2020-12-24 01:58:27 +00:00
let c = i.view as CommentView;
return (
<CommentNodes
key={i.id}
nodes={[{ comment_view: c, children: [], depth: 0 }]}
viewType={CommentViewType.Flat}
admins={Some(this.props.admins)}
moderators={None}
maxCommentsShown={None}
2020-12-24 01:58:27 +00:00
noBorder
noIndent
showCommunity
showContext
enableDownvotes={this.props.enableDownvotes}
allLanguages={this.props.allLanguages}
2020-12-24 01:58:27 +00:00
/>
);
}
case ItemEnum.Post: {
2020-12-24 01:58:27 +00:00
let p = i.view as PostView;
return (
<PostListing
key={i.id}
post_view={p}
admins={Some(this.props.admins)}
duplicates={None}
moderators={None}
2020-12-24 01:58:27 +00:00
showCommunity
enableDownvotes={this.props.enableDownvotes}
enableNsfw={this.props.enableNsfw}
allLanguages={this.props.allLanguages}
2020-12-24 01:58:27 +00:00
/>
);
}
2020-12-24 01:58:27 +00:00
default:
return <div />;
}
}
overview() {
2020-12-24 01:58:27 +00:00
let id = 0;
2021-03-15 18:09:31 +00:00
let comments: ItemType[] = this.props.personRes.comments.map(r => ({
2020-12-24 01:58:27 +00:00
id: id++,
type_: ItemEnum.Comment,
view: r,
published: r.comment.published,
score: r.counts.score,
}));
2021-03-15 18:09:31 +00:00
let posts: ItemType[] = this.props.personRes.posts.map(r => ({
2020-12-24 01:58:27 +00:00
id: id++,
2020-12-26 03:28:05 +00:00
type_: ItemEnum.Post,
2020-12-24 01:58:27 +00:00
view: r,
published: r.post.published,
score: r.counts.score,
}));
let combined = [...comments, ...posts];
// Sort it
if (this.props.sort === SortType.New) {
2020-12-24 01:58:27 +00:00
combined.sort((a, b) => b.published.localeCompare(a.published));
} else {
2020-12-24 01:58:27 +00:00
combined.sort((a, b) => b.score - a.score);
}
return (
<div>
{combined.map(i => [
this.renderItemType(i),
<hr key={i.type_} className="my-3" />,
])}
</div>
);
}
comments() {
return (
<div>
<CommentNodes
2021-03-15 18:09:31 +00:00
nodes={commentsToFlatNodes(this.props.personRes.comments)}
viewType={CommentViewType.Flat}
admins={Some(this.props.admins)}
moderators={None}
maxCommentsShown={None}
noIndent
showCommunity
showContext
enableDownvotes={this.props.enableDownvotes}
allLanguages={this.props.allLanguages}
/>
</div>
);
}
posts() {
return (
<div>
2021-03-15 18:09:31 +00:00
{this.props.personRes.posts.map(post => (
<>
<PostListing
2020-12-24 01:58:27 +00:00
post_view={post}
admins={Some(this.props.admins)}
showCommunity
duplicates={None}
moderators={None}
enableDownvotes={this.props.enableDownvotes}
enableNsfw={this.props.enableNsfw}
allLanguages={this.props.allLanguages}
/>
<hr className="my-3" />
</>
))}
</div>
);
}
handlePageChange(val: number) {
this.props.onPageChange(val);
}
}