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,
FramelyData,
} from '../interfaces';
import { MomentTime } from './moment-time';
import { PostForm } from './post-form';
import { IFramelyCard } from './iframely-card';
import {
mdToHtml,
canMod,
isMod,
isImage,
isVideo,
getUnixTime,
pictshareAvatarThumbnail,
showAvatars,
imageThumbnailer,
setupTippy,
} from '../utils';
import { i18n } from '../i18next';
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;
url: string;
iframely: FramelyData;
thumbnail: string;
}
interface PostListingProps {
post: Post;
showCommunity?: boolean;
showBody?: boolean;
moderators?: Array<CommunityUser>;
admins?: Array<UserView>;
}
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,
url: this.props.post.url,
iframely: null,
thumbnail: null,
};
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);
if (this.state.url) {
this.setThumbnail();
this.fetchIframely();
}
}
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;
if (nextProps.post.url !== this.state.url) {
this.state.url = nextProps.post.url;
if (this.state.url) {
this.setThumbnail();
this.fetchIframely();
} else {
this.state.iframely = null;
this.state.thumbnail = null;
}
}
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}
/>
</div>
)}
</div>
);
}
body() {
return (
<div class="row">
<div class="col-12">
{this.state.url && this.props.showBody && this.state.iframely && (
<IFramelyCard iframely={this.state.iframely} />
)}
{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() {
let post = this.props.post;
return (
<img
className={`img-fluid thumbnail rounded ${(post.nsfw ||
post.community_nsfw) &&
'img-blur'}`}
src={imageThumbnailer(this.state.thumbnail)}
/>
);
}
thumbnail() {
let post = this.props.post;
if (isImage(this.state.url)) {
return (
<span
class="text-body pointer"
data-tippy-content={i18n.t('expand_here')}
onClick={linkEvent(this, this.handleImageExpandClick)}
>
{this.imgThumb()}
<svg class="icon mini-overlay">
<use xlinkHref="#icon-image"></use>
</svg>
</span>
);
} else if (this.state.thumbnail) {
return (
<a
className="text-body"
href={this.state.url}
target="_blank"
title={this.state.url}
>
{this.imgThumb()}
<svg class="icon mini-overlay">
<use xlinkHref="#icon-external-link"></use>
</svg>
</a>
);
} else if (this.state.url && !this.state.thumbnail) {
return (
<a
className="text-body"
href={this.state.url}
target="_blank"
title={this.state.url}
>
<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={`vote-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-up"></use>
</svg>
</button>
<div
class={`unselectable pointer font-weight-bold text-muted px-1`}
data-tippy-content={i18n.t('number_of_points', {
count: this.state.score,
})}
>
{this.state.score}
</div>
{WebSocketService.Instance.site.enable_downvotes && (
<button
className={`vote-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-down"></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>
)}
{this.state.url && isVideo(this.state.url) && (
<video
playsinline
muted
loop
controls
class="col-11 col-sm-2 pr-0 mt-1"
height="100"
width="150"
>
<source src={this.state.url} type="video/mp4" />
</video>
)}
<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 && this.state.url ? (
<a
className="text-body"
href={this.state.url}
target="_blank"
title={this.state.url}
>
{post.name}
</a>
) : (
<Link
className="text-body"
to={`/post/${post.id}`}
title={i18n.t('comments')}
>
{post.name}
</Link>
)}
</h5>
{this.state.url &&
!(
new URL(this.state.url).hostname == window.location.hostname
) && (
<small class="d-inline-block">
<a
className="ml-2 text-muted font-italic"
href={this.state.url}
target="_blank"
title={this.state.url}
>
{new URL(this.state.url).hostname}
<svg class="ml-1 icon icon-inline">
<use xlink