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

218 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component, linkEvent } from "inferno";
import { i18n } from "../i18next";
2020-12-24 01:58:27 +00:00
import {
PostView,
CommentView,
SortType,
2021-03-15 18:09:31 +00:00
GetPersonDetailsResponse,
PersonViewSafe,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
2021-03-15 18:09:31 +00:00
import { PersonDetailsView } from "../interfaces";
2021-02-22 02:39:04 +00:00
import { commentsToFlatNodes, setupTippy } from "../utils";
import { PostListing } from "./post-listing";
import { CommentNodes } from "./comment-nodes";
2021-03-15 18:09:31 +00:00
interface PersonDetailsProps {
personRes: GetPersonDetailsResponse;
admins: PersonViewSafe[];
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);
}
2020-09-09 00:48:17 +00:00
// TODO needed here?
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)}
{this.paginator()}
</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 }]}
admins={this.props.admins}
noBorder
noIndent
showCommunity
showContext
enableDownvotes={this.props.enableDownvotes}
/>
);
}
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={this.props.admins}
showCommunity
enableDownvotes={this.props.enableDownvotes}
enableNsfw={this.props.enableNsfw}
/>
);
}
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>
2020-12-24 01:58:27 +00:00
{combined.map(i => [this.renderItemType(i), <hr class="my-3" />])}
</div>
);
}
comments() {
return (
<div>
<CommentNodes
2021-03-15 18:09:31 +00:00
nodes={commentsToFlatNodes(this.props.personRes.comments)}
2020-12-24 01:58:27 +00:00
admins={this.props.admins}
noIndent
showCommunity
showContext
enableDownvotes={this.props.enableDownvotes}
/>
</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={this.props.admins}
showCommunity
enableDownvotes={this.props.enableDownvotes}
enableNsfw={this.props.enableNsfw}
/>
<hr class="my-3" />
</>
))}
</div>
);
}
paginator() {
return (
<div class="my-2">
{this.props.page > 1 && (
<button
class="btn btn-secondary mr-1"
onClick={linkEvent(this, this.prevPage)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("prev")}
</button>
)}
2021-03-15 18:09:31 +00:00
{this.props.personRes.comments.length +
this.props.personRes.posts.length >
2020-09-09 00:48:17 +00:00
0 && (
<button
class="btn btn-secondary"
onClick={linkEvent(this, this.nextPage)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("next")}
</button>
)}
</div>
);
}
2021-03-15 18:09:31 +00:00
nextPage(i: PersonDetails) {
i.props.onPageChange(i.props.page + 1);
}
2021-03-15 18:09:31 +00:00
prevPage(i: PersonDetails) {
i.props.onPageChange(i.props.page - 1);
}
}