import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { CommunityView, CommunityModeratorView, FollowCommunity, DeleteCommunity, RemoveCommunity, UserViewSafe, AddModToCommunity, Category, } from 'lemmy-js-client'; import { WebSocketService, UserService } from '../services'; import { mdToHtml, getUnixTime, wsClient, authField } from '../utils'; import { CommunityForm } from './community-form'; import { UserListing } from './user-listing'; import { CommunityLink } from './community-link'; import { BannerIconHeader } from './banner-icon-header'; import { Icon } from './icon'; import { i18n } from '../i18next'; interface SidebarProps { community_view: CommunityView; categories: Category[]; moderators: CommunityModeratorView[]; admins: UserViewSafe[]; 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_view.community; let subscribed = this.props.community_view.subscribed; return (
{this.props.showIcon && ( )} {community.title} {subscribed && ( {i18n.t('joined')} )} {community.removed && ( {i18n.t('removed')} )} {community.deleted && ( {i18n.t('deleted')} )} {community.nsfw && ( {i18n.t('nsfw')} )}
); } badges() { let community_view = this.props.community_view; let counts = community_view.counts; return ( ); } mods() { return ( ); } createPost() { let community_view = this.props.community_view; return ( community_view.subscribed && ( {i18n.t('create_a_post')} ) ); } subscribe() { let community_view = this.props.community_view; return (
{!community_view.subscribed && ( {i18n.t('subscribe')} )}
); } description() { let description = this.props.community_view.community.description; return ( description && (
) ); } adminButtons() { let community_view = this.props.community_view; 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: DeleteCommunity = { community_id: i.props.community_view.community.id, deleted: !i.props.community_view.community.deleted, auth: authField(), }; WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm)); } handleShowConfirmLeaveModTeamClick(i: Sidebar) { i.state.showConfirmLeaveModTeam = true; i.setState(i.state); } handleLeaveModTeamClick(i: Sidebar) { let form: AddModToCommunity = { user_id: UserService.Instance.user.id, community_id: i.props.community_view.community.id, added: false, auth: authField(), }; WebSocketService.Instance.send(wsClient.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: FollowCommunity = { community_id: communityId, follow: false, auth: authField(), }; WebSocketService.Instance.send(wsClient.followCommunity(form)); } handleSubscribe(communityId: number, event: any) { event.preventDefault(); let form: FollowCommunity = { community_id: communityId, follow: true, auth: authField(), }; WebSocketService.Instance.send(wsClient.followCommunity(form)); } private get amCreator(): boolean { return this.props.community_view.creator.id == UserService.Instance.user.id; } get canMod(): boolean { return ( UserService.Instance.user && this.props.moderators .map(m => m.moderator.id) .includes(UserService.Instance.user.id) ); } get canAdmin(): boolean { return ( UserService.Instance.user && this.props.admins .map(a => a.user.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: RemoveCommunity = { community_id: i.props.community_view.community.id, removed: !i.props.community_view.community.removed, reason: i.state.removeReason, expires: getUnixTime(i.state.removeExpires), auth: authField(), }; WebSocketService.Instance.send(wsClient.removeCommunity(removeForm)); i.state.showRemoveDialog = false; i.setState(i.state); } }