import { Component, linkEvent } from 'inferno'; import { i18n } from '../i18next'; import { PostView, CommentView, SortType, GetUserDetailsResponse, UserViewSafe, } from 'lemmy-js-client'; import { UserDetailsView } from '../interfaces'; import { commentsToFlatNodes, setupTippy } from '../utils'; import { PostListing } from './post-listing'; import { CommentNodes } from './comment-nodes'; interface UserDetailsProps { userRes: GetUserDetailsResponse; admins: UserViewSafe[]; page: number; limit: number; sort: SortType; enableDownvotes: boolean; enableNsfw: boolean; view: UserDetailsView; onPageChange(page: number): number | any; } interface UserDetailsState {} enum ItemEnum { Comment, Post, } type ItemType = { id: number; type_: ItemEnum; view: CommentView | PostView; published: string; score: number; }; export class UserDetails extends Component { constructor(props: any, context: any) { super(props, context); } // TODO needed here? componentDidMount() { setupTippy(); } // TODO wut? // componentDidUpdate(lastProps: UserDetailsProps) { // for (const key of Object.keys(lastProps)) { // if (lastProps[key] !== this.props[key]) { // this.fetchUserData(); // break; // } // } // } render() { return (
{this.viewSelector(this.props.view)} {this.paginator()}
); } viewSelector(view: UserDetailsView) { if (view === UserDetailsView.Overview || view === UserDetailsView.Saved) { return this.overview(); } else if (view === UserDetailsView.Comments) { return this.comments(); } else if (view === UserDetailsView.Posts) { return this.posts(); } else { return null; } } renderItemType(i: ItemType) { switch (i.type_) { case ItemEnum.Comment: let c = i.view as CommentView; return ( ); case ItemEnum.Post: let p = i.view as PostView; return ( ); default: return
; } } overview() { let id = 0; let comments: ItemType[] = this.props.userRes.comments.map(r => ({ id: id++, type_: ItemEnum.Comment, view: r, published: r.comment.published, score: r.counts.score, })); let posts: ItemType[] = this.props.userRes.posts.map(r => ({ id: id++, type_: ItemEnum.Comment, view: r, published: r.post.published, score: r.counts.score, })); let combined = [...comments, ...posts]; // Sort it if (this.props.sort === SortType.New) { combined.sort((a, b) => b.published.localeCompare(a.published)); } else { combined.sort((a, b) => b.score - a.score); } return (
{combined.map(i => [this.renderItemType(i),
])}
); } comments() { return (
); } posts() { return (
{this.props.userRes.posts.map(post => ( <>
))}
); } paginator() { return (
{this.props.page > 1 && ( )} {this.props.userRes.comments.length + this.props.userRes.posts.length > 0 && ( )}
); } nextPage(i: UserDetails) { i.props.onPageChange(i.props.page + 1); } prevPage(i: UserDetails) { i.props.onPageChange(i.props.page - 1); } }