import { Component, linkEvent } from 'inferno';
import { Link } from 'inferno-router';
import { WebSocketService, UserService } from '../services';
import {
Post,
CreatePostLikeForm,
PostForm as PostFormI,
SavePostForm,
CommunityUser,
UserView,
BanType,
BanFromCommunityForm,
BanUserForm,
AddModToCommunityForm,
AddAdminForm,
TransferSiteForm,
TransferCommunityForm,
} from '../interfaces';
import { MomentTime } from './moment-time';
import { PostForm } from './post-form';
import { IFramelyCard } from './iframely-card';
import { UserListing } from './user-listing';
import { CommunityLink } from './community-link';
import {
md,
mdToHtml,
canMod,
isMod,
isImage,
isVideo,
getUnixTime,
pictrsImage,
setupTippy,
hostname,
previewLines,
isCakeDay,
} from '../utils';
import { i18n } from '../i18next';
import { CakeDay } from './cake-day';
interface PostListingState {
showEdit: boolean;
showRemoveDialog: boolean;
removeReason: string;
showBanDialog: boolean;
banReason: string;
banExpires: string;
banType: BanType;
showConfirmTransferSite: boolean;
showConfirmTransferCommunity: boolean;
imageExpanded: boolean;
viewSource: boolean;
showAdvanced: boolean;
my_vote: number;
score: number;
upvotes: number;
downvotes: number;
}
interface PostListingProps {
post: Post;
showCommunity?: boolean;
showBody?: boolean;
moderators?: Array<CommunityUser>;
admins?: Array<UserView>;
enableDownvotes: boolean;
enableNsfw: boolean;
}
export class PostListing extends Component<PostListingProps, PostListingState> {
private emptyState: PostListingState = {
showEdit: false,
showRemoveDialog: false,
removeReason: null,
showBanDialog: false,
banReason: null,
banExpires: null,
banType: BanType.Community,
showConfirmTransferSite: false,
showConfirmTransferCommunity: false,
imageExpanded: false,
viewSource: false,
showAdvanced: false,
my_vote: this.props.post.my_vote,
score: this.props.post.score,
upvotes: this.props.post.upvotes,
downvotes: this.props.post.downvotes,
};
constructor(props: any, context: any) {
super(props, context);
this.state = this.emptyState;
this.handlePostLike = this.handlePostLike.bind(this);
this.handlePostDisLike = this.handlePostDisLike.bind(this);
this.handleEditPost = this.handleEditPost.bind(this);
this.handleEditCancel = this.handleEditCancel.bind(this);
}
componentWillReceiveProps(nextProps: PostListingProps) {
this.state.my_vote = nextProps.post.my_vote;
this.state.upvotes = nextProps.post.upvotes;
this.state.downvotes = nextProps.post.downvotes;
this.state.score = nextProps.post.score;
this.setState(this.state);
}
render() {
return (
<div class="">
{!this.state.showEdit ? (
<>
{this.listing()}
{this.body()}
</>
) : (
<div class="col-12">
<PostForm
post={this.props.post}
onEdit={this.handleEditPost}
onCancel={this.handleEditCancel}
enableNsfw={this.props.enableNsfw}
enableDownvotes={this.props.enableDownvotes}
/>
</div>
)}
</div>
);
}
body() {
return (
<div class="row">
<div class="col-12">
{this.props.post.url &&
this.props.showBody &&
this.props.post.embed_title && (
<IFramelyCard post={this.props.post} />
)}
{this.props.showBody && this.props.post.body && (
<>
{this.state.viewSource ? (
<pre>{this.props.post.body}</pre>
) : (
<div
className="md-div"
dangerouslySetInnerHTML={mdToHtml(this.props.post.body)}
/>
)}
</>
)}
</div>
</div>
);
}
imgThumb(src: string) {
let post = this.props.post;
return (
<img
className={`img-fluid thumbnail rounded ${
(post.nsfw || post.community_nsfw) && 'img-blur'
}`}
src={src}
/>
);
}
getImage(thumbnail: boolean = false) {
let post = this.props.post;
if (isImage(post.url)) {
if (post.url.includes('pictrs')) {
return pictrsImage(post.url, thumbnail);
} else if (post.thumbnail_url) {
return pictrsImage(post.thumbnail_url, thumbnail);
} else {
return post.url;
}
} else if (post.thumbnail_url) {
return pictrsImage(post.thumbnail_url, thumbnail);
}
}
thumbnail() {
let post = this.props.post;
if (isImage(post.url)) {
return (
<span
class="text-body pointer"
data-tippy-content={i18n.t('expand_here')}
onClick={linkEvent(this, this.handleImageExpandClick)}
>
{this.imgThumb(this.getImage(true))}
<svg class="icon mini-overlay">
<use xlinkHref="#icon-image"></use>
</svg>
</span>
);
} else if (post.thumbnail_url) {
return (
<a
className="text-body"
href={post.url}
target="_blank"
rel="noopener"
title={post.url}
>
{this.imgThumb(this.getImage(true))}
<svg class="icon mini-overlay">
<use xlinkHref="#icon-external-link"></use>
</svg>
</a>
);
} else if (post.url) {
if (isVideo(post.url)) {
return (
<div class="embed-responsive embed-responsive-16by9">
<video
playsinline
muted
loop
controls
class="embed-responsive-item"
>
<source src={post.url} type="video/mp4" />
</video>
</div>
);
} else {
return (
<a
className="text-body"
href={post.url}
target="_blank"
title={post.url}
rel="noopener"
>
<svg class="icon thumbnail">
<use xlinkHref="#icon-external-link"></use>
</svg>
</a>
);
}
} else {
return (
<Link
className="text-body"
to={`/post/${post.id}`}
title={i18n.t('comments')}
>
<svg class="icon thumbnail">
<use xlinkHref="#icon-message-square"></use>
</svg>
</Link>
);
}
}
listing() {
let post = this.props.post;
return (
<div class="row">
<div className={`vote-bar col-1 pr-0 small text-center`}>
<button
className={`btn-animate btn btn-link p-0 ${
this.state.my_vote == 1 ? 'text-info' : 'text-muted'
}`}
onClick={linkEvent(this, this.handlePostLike)}
data-tippy-content={i18n.t('upvote')}
>
<svg class="icon upvote">
<use xlinkHref="#icon-arrow-up1"></use>
</svg>
</button>
<div
class={`unselectable pointer font-weight-bold text-muted px-1`}
data-tippy-content={this.pointsTippy}
>
{this.state.score}
</div>
{this.props.enableDownvotes && (
<button
className={`btn-animate btn btn-link p-0 ${
this.state.my_vote == -1 ? 'text-danger' : 'text-muted'
}`}
onClick={linkEvent(this, this.handlePostDisLike)}
data-tippy-content={i18n.t('downvote')}
>
<svg class="icon downvote">
<use xlinkHref="#icon-arrow-down1"></use>
</svg>
</button>
)}
</div>
{!this.state.imageExpanded && (
<div class="col-3 col-sm-2 pr-0 mt-1">
<div class="position-relative">{this.thumbnail()}</div>
</div>
)}
<div
class={`${this.state.imageExpanded ? 'col-12' : 'col-8 col-sm-9'}`}
>
<div class="row">
<div className="col-12">
<div className="post-title">
<h5 className="mb-0 d-inline">
{this.props.showBody && post.url ? (
<a
className="text-body"
href={post.url}
target="_blank"
title={post.url}
rel="noopener"
>
{post.name}
</a>
) : (
<Link
className="text-body"