import React from "react" import * as actions from "./controls/actions" import LikesModal from "misago/components/post-likes" import modal from "misago/services/modal" import posting from "misago/services/posting" export default function(props) { if (!isVisible(props.post)) return null return (
) } export function isVisible(post) { return ( (!post.is_hidden || post.acl.can_see_hidden) && (post.acl.can_reply || post.acl.can_edit || (post.acl.can_see_likes && (post.last_likes || []).length) || post.acl.can_like) ) } export class MarkAsBestAnswer extends React.Component { onClick = () => { actions.markAsBestAnswer(this.props) } render() { const { post, thread } = this.props if (!thread.acl.can_mark_best_answer) return null if (!post.acl.can_mark_as_best_answer) return null if (thread.best_answer && !thread.acl.can_change_best_answer) return null return ( ) } } export class MarkAsBestAnswerCompact extends React.Component { onClick = () => { actions.markAsBestAnswer(this.props) } render() { const { post, thread } = this.props if (!thread.acl.can_mark_best_answer) return null if (!post.acl.can_mark_as_best_answer) return null if (thread.best_answer && !thread.acl.can_change_best_answer) return null return ( ) } } export class Like extends React.Component { onClick = () => { if (this.props.post.is_liked) { actions.unlike(this.props) } else { actions.like(this.props) } } render() { if (!this.props.post.acl.can_like) return null let className = "btn btn-default btn-sm pull-left" if (this.props.post.is_liked) { className = "btn btn-success btn-sm pull-left" } return ( ) } } export class Likes extends React.Component { onClick = () => { modal.show() } render() { const hasLikes = (this.props.post.last_likes || []).length > 0 if (!this.props.post.acl.can_see_likes || !hasLikes) return null if (this.props.post.acl.can_see_likes === 2) { return ( ) } return (

{getLikesMessage(this.props.likes, this.props.lastLikes)}

) } } export class LikesCompact extends Likes { render() { const hasLikes = (this.props.post.last_likes || []).length > 0 if (!this.props.post.acl.can_see_likes || !hasLikes) return null if (this.props.post.acl.can_see_likes === 2) { return ( ) } return (

favorite {this.props.likes}

) } } export function getLikesMessage(likes, users) { const usernames = users.slice(0, 3).map(u => u.username) if (usernames.length == 1) { return interpolate( gettext("%(user)s likes this."), { user: usernames[0] }, true ) } const hiddenLikes = likes - usernames.length const otherUsers = usernames.slice(0, -1).join(", ") const lastUser = usernames.slice(-1)[0] const usernamesList = interpolate( gettext("%(users)s and %(last_user)s"), { users: otherUsers, last_user: lastUser }, true ) if (hiddenLikes === 0) { return interpolate( gettext("%(users)s like this."), { users: usernamesList }, true ) } const message = ngettext( "%(users)s and %(likes)s other user like this.", "%(users)s and %(likes)s other users like this.", hiddenLikes ) return interpolate( message, { users: usernames.join(", "), likes: hiddenLikes }, true ) } export class Reply extends React.Component { onClick = () => { posting.open({ mode: "REPLY", config: this.props.thread.api.editor, submit: this.props.thread.api.posts.index, context: { reply: this.props.post.id } }) } render() { if (this.props.post.acl.can_reply) { return ( ) } else { return null } } } export class Edit extends React.Component { onClick = () => { posting.open({ mode: "EDIT", config: this.props.post.api.editor, submit: this.props.post.api.index }) } render() { if (this.props.post.acl.can_edit) { return ( ) } else { return null } } }