import { Component, linkEvent } from "inferno"; import { Link } from "inferno-router"; import { AddModToCommunity, BlockCommunity, CommunityModeratorView, CommunityView, DeleteCommunity, FollowCommunity, Language, PersonView, PurgeCommunity, RemoveCommunity, } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService, WebSocketService } from "../../services"; import { amAdmin, amMod, amTopMod, getUnixTime, hostname, mdToHtml, myAuth, numToSI, wsClient, } from "../../utils"; import { BannerIconHeader } from "../common/banner-icon-header"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { CommunityForm } from "../community/community-form"; import { CommunityLink } from "../community/community-link"; import { PersonListing } from "../person/person-listing"; interface SidebarProps { community_view: CommunityView; moderators: CommunityModeratorView[]; admins: PersonView[]; allLanguages: Language[]; siteLanguages: number[]; communityLanguages?: number[]; online: number; enableNsfw?: boolean; showIcon?: boolean; editable?: boolean; } interface SidebarState { removeReason?: string; removeExpires?: string; showEdit: boolean; showRemoveDialog: boolean; showPurgeDialog: boolean; purgeReason?: string; purgeLoading: boolean; showConfirmLeaveModTeam: boolean; } export class Sidebar extends Component { state: SidebarState = { showEdit: false, showRemoveDialog: false, showPurgeDialog: false, purgeLoading: false, showConfirmLeaveModTeam: false, }; constructor(props: any, context: any) { super(props, context); this.handleEditCommunity = this.handleEditCommunity.bind(this); this.handleEditCancel = this.handleEditCancel.bind(this); } render() { return (
{!this.state.showEdit ? ( this.sidebar() ) : ( )}
); } sidebar() { const myUSerInfo = UserService.Instance.myUserInfo; const { name, actor_id } = this.props.community_view.community; return (
{this.communityTitle()} {this.props.editable && this.adminButtons()} {myUSerInfo && this.subscribe()} {this.canPost && this.createPost()} {myUSerInfo && this.blockCommunity()} {!myUSerInfo && (
{i18n.t("community_not_logged_in_alert", { community: name, instance: hostname(actor_id), })}
)}
{this.description()} {this.badges()} {this.mods()}
); } communityTitle() { const community = this.props.community_view.community; const subscribed = this.props.community_view.subscribed; return (
{this.props.showIcon && !community.removed && ( )} {community.title} {subscribed === "Subscribed" && ( )} {subscribed === "Pending" && ( )} {community.removed && ( {i18n.t("removed")} )} {community.deleted && ( {i18n.t("deleted")} )} {community.nsfw && ( {i18n.t("nsfw")} )}
); } badges() { const community_view = this.props.community_view; const counts = community_view.counts; return ( ); } mods() { return ( ); } createPost() { const cv = this.props.community_view; return ( {i18n.t("create_a_post")} ); } subscribe() { const community_view = this.props.community_view; return (
{community_view.subscribed == "NotSubscribed" && ( )}
); } blockCommunity() { const community_view = this.props.community_view; const blocked = this.props.community_view.blocked; return (
{community_view.subscribed == "NotSubscribed" && (blocked ? ( ) : ( ))}
); } description() { const desc = this.props.community_view.community.description; return ( desc && (
) ); } adminButtons() { const community_view = this.props.community_view; return ( <> {this.state.showRemoveDialog && (
{/* TODO hold off on expires for now */} {/*
*/} {/* */} {/* */} {/*
*/}
)} {this.state.showPurgeDialog && (
{this.state.purgeLoading ? ( ) : ( )}
)} ); } handleEditClick(i: Sidebar) { i.setState({ showEdit: true }); } handleEditCommunity() { this.setState({ showEdit: false }); } handleEditCancel() { this.setState({ showEdit: false }); } handleDeleteClick(i: Sidebar, event: any) { event.preventDefault(); const auth = myAuth(); if (auth) { const deleteForm: DeleteCommunity = { community_id: i.props.community_view.community.id, deleted: !i.props.community_view.community.deleted, auth, }; WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm)); } } handleShowConfirmLeaveModTeamClick(i: Sidebar) { i.setState({ showConfirmLeaveModTeam: true }); } handleLeaveModTeamClick(i: Sidebar) { const mui = UserService.Instance.myUserInfo; const auth = myAuth(); if (auth && mui) { const form: AddModToCommunity = { person_id: mui.local_user_view.person.id, community_id: i.props.community_view.community.id, added: false, auth, }; WebSocketService.Instance.send(wsClient.addModToCommunity(form)); i.setState({ showConfirmLeaveModTeam: false }); } } handleCancelLeaveModTeamClick(i: Sidebar) { i.setState({ showConfirmLeaveModTeam: false }); } handleUnsubscribe(i: Sidebar, event: any) { event.preventDefault(); const community_id = i.props.community_view.community.id; const auth = myAuth(); if (auth) { const form: FollowCommunity = { community_id, follow: false, auth, }; WebSocketService.Instance.send(wsClient.followCommunity(form)); } // Update myUserInfo const mui = UserService.Instance.myUserInfo; if (mui) { mui.follows = mui.follows.filter(i => i.community.id != community_id); } } handleSubscribe(i: Sidebar, event: any) { event.preventDefault(); const community_id = i.props.community_view.community.id; const auth = myAuth(); if (auth) { const form: FollowCommunity = { community_id, follow: true, auth, }; WebSocketService.Instance.send(wsClient.followCommunity(form)); } // Update myUserInfo const mui = UserService.Instance.myUserInfo; if (mui) { mui.follows.push({ community: i.props.community_view.community, follower: mui.local_user_view.person, }); } } get canPost(): boolean { return ( !this.props.community_view.community.posting_restricted_to_mods || amMod(this.props.moderators) || amAdmin() ); } handleModRemoveShow(i: Sidebar) { i.setState({ showRemoveDialog: true }); } handleModRemoveReasonChange(i: Sidebar, event: any) { i.setState({ removeReason: event.target.value }); } handleModRemoveExpiresChange(i: Sidebar, event: any) { i.setState({ removeExpires: event.target.value }); } handleModRemoveSubmit(i: Sidebar, event: any) { event.preventDefault(); const auth = myAuth(); if (auth) { const 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, }; WebSocketService.Instance.send(wsClient.removeCommunity(removeForm)); i.setState({ showRemoveDialog: false }); } } handlePurgeCommunityShow(i: Sidebar) { i.setState({ showPurgeDialog: true, showRemoveDialog: false }); } handlePurgeReasonChange(i: Sidebar, event: any) { i.setState({ purgeReason: event.target.value }); } handlePurgeSubmit(i: Sidebar, event: any) { event.preventDefault(); const auth = myAuth(); if (auth) { const form: PurgeCommunity = { community_id: i.props.community_view.community.id, reason: i.state.purgeReason, auth, }; WebSocketService.Instance.send(wsClient.purgeCommunity(form)); i.setState({ purgeLoading: true }); } } handleBlock(i: Sidebar, event: any) { event.preventDefault(); const auth = myAuth(); if (auth) { const blockCommunityForm: BlockCommunity = { community_id: i.props.community_view.community.id, block: true, auth, }; WebSocketService.Instance.send( wsClient.blockCommunity(blockCommunityForm) ); } } handleUnblock(i: Sidebar, event: any) { event.preventDefault(); const auth = myAuth(); if (auth) { const blockCommunityForm: BlockCommunity = { community_id: i.props.community_view.community.id, block: false, auth, }; WebSocketService.Instance.send( wsClient.blockCommunity(blockCommunityForm) ); } } }