import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { Community, CommunityUser, FollowCommunityForm, DeleteCommunityForm, RemoveCommunityForm, UserView, AddModToCommunityForm, Category, } from 'lemmy-js-client'; import { WebSocketService, UserService } from '../services'; import { mdToHtml, getUnixTime } from '../utils'; import { CommunityForm } from './community-form'; import { UserListing } from './user-listing'; import { CommunityLink } from './community-link'; import { BannerIconHeader } from './banner-icon-header'; import { i18n } from '../i18next'; interface SidebarProps { community: Community; categories: Category[]; moderators: CommunityUser[]; admins: UserView[]; online: number; enableNsfw: boolean; showIcon?: boolean; } interface SidebarState { showEdit: boolean; showRemoveDialog: boolean; removeReason: string; removeExpires: string; showConfirmLeaveModTeam: boolean; } export class Sidebar extends Component { private emptyState: SidebarState = { showEdit: false, showRemoveDialog: false, removeReason: null, removeExpires: null, showConfirmLeaveModTeam: false, }; 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() { return (
{this.communityTitle()} {this.adminButtons()} {this.subscribe()} {this.createPost()}
{this.description()} {this.badges()} {this.mods()}
); } communityTitle() { let community = this.props.community; return (
{this.props.showIcon && ( )} {community.title} {community.subscribed && ( {i18n.t('joined')} )} {community.removed && ( {i18n.t('removed')} )} {community.deleted && ( {i18n.t('deleted')} )} {community.nsfw && ( {i18n.t('nsfw')} )}
); } badges() { let community = this.props.community; return ( ); } mods() { return ( ); } createPost() { let community = this.props.community; return ( community.subscribed && ( {i18n.t('create_a_post')} ) ); } subscribe() { let community = this.props.community; return (
{!community.subscribed && ( {i18n.t('subscribe')} )}
); } description() { let community = this.props.community; return ( community.description && (
) ); } adminButtons() { let community = this.props.community; return ( <> {this.state.showRemoveDialog && (
{/* TODO hold off on expires for now */} {/*
*/} {/* */} {/* */} {/*
*/}
)} ); } 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: any) { event.preventDefault(); let deleteForm: DeleteCommunityForm = { edit_id: i.props.community.id, deleted: !i.props.community.deleted, }; WebSocketService.Instance.deleteCommunity(deleteForm); } handleShowConfirmLeaveModTeamClick(i: Sidebar) { i.state.showConfirmLeaveModTeam = true; i.setState(i.state); } handleLeaveModTeamClick(i: Sidebar) { let form: AddModToCommunityForm = { user_id: UserService.Instance.user.id, community_id: i.props.community.id, added: false, }; WebSocketService.Instance.addModToCommunity(form); i.state.showConfirmLeaveModTeam = false; i.setState(i.state); } handleCancelLeaveModTeamClick(i: Sidebar) { i.state.showConfirmLeaveModTeam = false; i.setState(i.state); } handleUnsubscribe(communityId: number, event: any) { event.preventDefault(); let form: FollowCommunityForm = { community_id: communityId, follow: false, }; WebSocketService.Instance.followCommunity(form); } handleSubscribe(communityId: number, event: any) { event.preventDefault(); 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: any) { event.preventDefault(); let removeForm: RemoveCommunityForm = { edit_id: i.props.community.id, removed: !i.props.community.removed, reason: i.state.removeReason, expires: getUnixTime(i.state.removeExpires), }; WebSocketService.Instance.removeCommunity(removeForm); i.state.showRemoveDialog = false; i.setState(i.state); } }