import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { Community, CommunityUser, FollowCommunityForm, CommunityForm as CommunityFormI, UserView, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { mdToHtml, getUnixTime, hostname } from '../utils'; import { CommunityForm } from './community-form'; import { UserListing } from './user-listing'; import { CommunityLink } from './community-link'; import { i18n } from '../i18next'; interface SidebarProps { community: Community; moderators: Array; admins: Array; online: number; enableNsfw: boolean; } interface SidebarState { showEdit: boolean; showRemoveDialog: boolean; removeReason: string; removeExpires: string; } export class Sidebar extends Component { private emptyState: SidebarState = { showEdit: false, showRemoveDialog: false, removeReason: null, removeExpires: null, }; constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; this.handleEditCommunity = this.handleEditCommunity.bind(this); this.handleEditCancel = this.handleEditCancel.bind(this); } render() { return (
{!this.state.showEdit ? ( this.sidebar() ) : ( )}
); } sidebar() { let community = this.props.community; let name_: string, link: string; if (community.local) { name_ = community.name; link = `/c/${community.name}`; } else { name_ = `${community.name}@${hostname(community.actor_id)}`; link = community.actor_id; } return (
{community.title} {community.removed && ( {i18n.t('removed')} )} {community.deleted && ( {i18n.t('deleted')} )}
    {this.canMod && ( <>
  • {this.amCreator && (
  • )} )} {this.canAdmin && (
  • {!this.props.community.removed ? ( {i18n.t('remove')} ) : ( {i18n.t('restore')} )}
  • )}
{this.state.showRemoveDialog && (
{/* TODO hold off on expires for now */} {/*
*/} {/* */} {/* */} {/*
*/}
)}
    {/*
  • {i18n.t('number_online', { count: this.props.online })}
  • */}
  • {i18n.t('number_of_subscribers', { count: community.number_of_subscribers, })}
  • {i18n.t('number_of_posts', { count: community.number_of_posts, })}
  • {i18n.t('number_of_comments', { count: community.number_of_comments, })}
  • {community.category_name}
  • {i18n.t('modlog')}
  • {i18n.t('mods')}:
  • {this.props.moderators.map(mod => (
  • ))}
{/* TODO the to= needs to be able to handle community_ids as well, since they're federated */} {i18n.t('create_a_post')}
{community.subscribed ? ( ) : ( )}
{community.description && (
)}
); } handleEditClick(i: Sidebar) { i.state.showEdit = true; i.setState(i.state); } handleEditCommunity() { this.state.showEdit = false; this.setState(this.state); } handleEditCancel() { this.state.showEdit = false; this.setState(this.state); } handleDeleteClick(i: Sidebar) { event.preventDefault(); let deleteForm: CommunityFormI = { name: i.props.community.name, title: i.props.community.title, category_id: i.props.community.category_id, edit_id: i.props.community.id, deleted: !i.props.community.deleted, nsfw: i.props.community.nsfw, auth: null, }; WebSocketService.Instance.editCommunity(deleteForm); } handleUnsubscribe(communityId: number) { let form: FollowCommunityForm = { community_id: communityId, follow: false, }; WebSocketService.Instance.followCommunity(form); } handleSubscribe(communityId: number) { let form: FollowCommunityForm = { community_id: communityId, follow: true, }; WebSocketService.Instance.followCommunity(form); } private get amCreator(): boolean { return this.props.community.creator_id == UserService.Instance.user.id; } get canMod(): boolean { return ( UserService.Instance.user && this.props.moderators .map(m => m.user_id) .includes(UserService.Instance.user.id) ); } get canAdmin(): boolean { return ( UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id) ); } handleModRemoveShow(i: Sidebar) { i.state.showRemoveDialog = true; i.setState(i.state); } handleModRemoveReasonChange(i: Sidebar, event: any) { i.state.removeReason = event.target.value; i.setState(i.state); } handleModRemoveExpiresChange(i: Sidebar, event: any) { console.log(event.target.value); i.state.removeExpires = event.target.value; i.setState(i.state); } handleModRemoveSubmit(i: Sidebar) { event.preventDefault(); let deleteForm: CommunityFormI = { name: i.props.community.name, title: i.props.community.title, category_id: i.props.community.category_id, edit_id: i.props.community.id, removed: !i.props.community.removed, reason: i.state.removeReason, expires: getUnixTime(i.state.removeExpires), nsfw: i.props.community.nsfw, auth: null, }; WebSocketService.Instance.editCommunity(deleteForm); i.state.showRemoveDialog = false; i.setState(i.state); } }