lemmy-ui/src/shared/components/community/sidebar.tsx

541 lines
16 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component, linkEvent } from "inferno";
import { Link } from "inferno-router";
import {
AddModToCommunity,
2020-12-24 01:58:27 +00:00
CommunityModeratorView,
CommunityView,
2020-12-24 01:58:27 +00:00
DeleteCommunity,
FollowCommunity,
2021-03-15 18:09:31 +00:00
PersonViewSafe,
RemoveCommunity,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import { authField, getUnixTime, mdToHtml, wsClient } from "../../utils";
import { BannerIconHeader } from "../common/banner-icon-header";
import { Icon } from "../common/icon";
import { CommunityForm } from "../community/community-form";
import { CommunityLink } from "../community/community-link";
import { PersonListing } from "../person/person-listing";
interface SidebarProps {
2020-12-24 01:58:27 +00:00
community_view: CommunityView;
moderators: CommunityModeratorView[];
2021-03-15 18:09:31 +00:00
admins: PersonViewSafe[];
online: number;
enableNsfw: boolean;
showIcon?: boolean;
}
interface SidebarState {
showEdit: boolean;
showRemoveDialog: boolean;
removeReason: string;
removeExpires: string;
showConfirmLeaveModTeam: boolean;
}
export class Sidebar extends Component<SidebarProps, SidebarState> {
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 (
<div>
{!this.state.showEdit ? (
this.sidebar()
) : (
<CommunityForm
2020-12-24 01:58:27 +00:00
community_view={this.props.community_view}
onEdit={this.handleEditCommunity}
onCancel={this.handleEditCancel}
enableNsfw={this.props.enableNsfw}
/>
)}
</div>
);
}
sidebar() {
return (
<div>
<div class="card border-secondary mb-3">
<div class="card-body">
{this.communityTitle()}
{this.adminButtons()}
{this.subscribe()}
{this.createPost()}
</div>
</div>
<div class="card border-secondary mb-3">
<div class="card-body">
{this.description()}
{this.badges()}
{this.mods()}
</div>
</div>
</div>
);
}
communityTitle() {
2020-12-24 01:58:27 +00:00
let community = this.props.community_view.community;
let subscribed = this.props.community_view.subscribed;
return (
<div>
<h5 className="mb-0">
{this.props.showIcon && (
<BannerIconHeader icon={community.icon} banner={community.banner} />
)}
<span class="mr-2">{community.title}</span>
2020-12-24 01:58:27 +00:00
{subscribed && (
<a
class="btn btn-secondary btn-sm mr-2"
href="#"
onClick={linkEvent(community.id, this.handleUnsubscribe)}
>
2021-02-11 20:35:27 +00:00
<Icon icon="check" classes="icon-inline text-success mr-1" />
2021-02-22 02:39:04 +00:00
{i18n.t("joined")}
</a>
)}
{community.removed && (
<small className="mr-2 text-muted font-italic">
2021-02-22 02:39:04 +00:00
{i18n.t("removed")}
</small>
)}
{community.deleted && (
<small className="mr-2 text-muted font-italic">
2021-02-22 02:39:04 +00:00
{i18n.t("deleted")}
</small>
)}
{community.nsfw && (
<small className="mr-2 text-muted font-italic">
2021-02-22 02:39:04 +00:00
{i18n.t("nsfw")}
</small>
)}
</h5>
<CommunityLink
community={community}
realLink
useApubName
muted
hideAvatar
/>
</div>
);
}
badges() {
2020-12-24 01:58:27 +00:00
let community_view = this.props.community_view;
let counts = community_view.counts;
return (
<ul class="my-1 list-inline">
<li className="list-inline-item badge badge-secondary">
2021-02-22 02:39:04 +00:00
{i18n.t("number_online", { count: this.props.online })}
</li>
<li
className="list-inline-item badge badge-secondary pointer"
2021-02-22 02:39:04 +00:00
data-tippy-content={`${i18n.t("number_of_users", {
count: counts.users_active_day,
2021-02-22 02:39:04 +00:00
})} ${i18n.t("active_in_the_last")} ${i18n.t("day")}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_users", {
count: counts.users_active_day,
2021-02-22 02:39:04 +00:00
})}{" "}
/ {i18n.t("day")}
</li>
<li
className="list-inline-item badge badge-secondary pointer"
2021-02-22 02:39:04 +00:00
data-tippy-content={`${i18n.t("number_of_users", {
count: counts.users_active_week,
2021-02-22 02:39:04 +00:00
})} ${i18n.t("active_in_the_last")} ${i18n.t("week")}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_users", {
count: counts.users_active_week,
2021-02-22 02:39:04 +00:00
})}{" "}
/ {i18n.t("week")}
</li>
<li
className="list-inline-item badge badge-secondary pointer"
2021-02-22 02:39:04 +00:00
data-tippy-content={`${i18n.t("number_of_users", {
count: counts.users_active_month,
2021-02-22 02:39:04 +00:00
})} ${i18n.t("active_in_the_last")} ${i18n.t("month")}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_users", {
count: counts.users_active_month,
2021-02-22 02:39:04 +00:00
})}{" "}
/ {i18n.t("month")}
</li>
<li
className="list-inline-item badge badge-secondary pointer"
2021-02-22 02:39:04 +00:00
data-tippy-content={`${i18n.t("number_of_users", {
count: counts.users_active_half_year,
2021-02-22 02:39:04 +00:00
})} ${i18n.t("active_in_the_last")} ${i18n.t("number_of_months", {
count: 6,
})}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_users", {
count: counts.users_active_half_year,
2021-02-22 02:39:04 +00:00
})}{" "}
/ {i18n.t("number_of_months", { count: 6 })}
</li>
<li className="list-inline-item badge badge-secondary">
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_subscribers", {
count: counts.subscribers,
})}
</li>
<li className="list-inline-item badge badge-secondary">
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_posts", {
count: counts.posts,
})}
</li>
<li className="list-inline-item badge badge-secondary">
2021-02-22 02:39:04 +00:00
{i18n.t("number_of_comments", {
count: counts.comments,
})}
</li>
<li className="list-inline-item">
<Link
className="badge badge-secondary"
2020-12-24 01:58:27 +00:00
to={`/modlog/community/${this.props.community_view.community.id}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("modlog")}
</Link>
</li>
</ul>
);
}
mods() {
return (
<ul class="list-inline small">
2021-02-22 02:39:04 +00:00
<li class="list-inline-item">{i18n.t("mods")}: </li>
{this.props.moderators.map(mod => (
<li class="list-inline-item">
2021-03-15 18:09:31 +00:00
<PersonListing person={mod.moderator} />
</li>
))}
</ul>
);
}
createPost() {
2020-12-24 01:58:27 +00:00
let community_view = this.props.community_view;
return (
2020-12-24 01:58:27 +00:00
community_view.subscribed && (
<Link
className={`btn btn-secondary btn-block mb-2 ${
2020-12-24 01:58:27 +00:00
community_view.community.deleted || community_view.community.removed
2021-02-22 02:39:04 +00:00
? "no-click"
: ""
}`}
2020-12-24 01:58:27 +00:00
to={`/create_post?community_id=${community_view.community.id}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("create_a_post")}
</Link>
)
);
}
subscribe() {
2020-12-24 01:58:27 +00:00
let community_view = this.props.community_view;
return (
<div class="mb-2">
2020-12-24 01:58:27 +00:00
{!community_view.subscribed && (
<a
class="btn btn-secondary btn-block"
href="#"
2020-12-24 01:58:27 +00:00
onClick={linkEvent(
community_view.community.id,
this.handleSubscribe
)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("subscribe")}
</a>
)}
</div>
);
}
description() {
2020-12-24 01:58:27 +00:00
let description = this.props.community_view.community.description;
return (
2020-12-24 01:58:27 +00:00
description && (
<div
className="md-div"
2020-12-24 01:58:27 +00:00
dangerouslySetInnerHTML={mdToHtml(description)}
/>
)
);
}
adminButtons() {
2020-12-24 01:58:27 +00:00
let community_view = this.props.community_view;
return (
<>
<ul class="list-inline mb-1 text-muted font-weight-bold">
{this.canMod && (
<>
<li className="list-inline-item-action">
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(this, this.handleEditClick)}
2021-02-22 02:39:04 +00:00
data-tippy-content={i18n.t("edit")}
aria-label={i18n.t("edit")}
>
2021-02-11 20:35:27 +00:00
<Icon icon="edit" classes="icon-inline" />
</button>
</li>
{!this.amTopMod &&
(!this.state.showConfirmLeaveModTeam ? (
<li className="list-inline-item-action">
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(
this,
this.handleShowConfirmLeaveModTeamClick
)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("leave_mod_team")}
</button>
</li>
) : (
<>
<li className="list-inline-item-action">
2021-02-22 02:39:04 +00:00
{i18n.t("are_you_sure")}
</li>
<li className="list-inline-item-action">
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(this, this.handleLeaveModTeamClick)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("yes")}
</button>
</li>
<li className="list-inline-item-action">
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(
this,
this.handleCancelLeaveModTeamClick
)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("no")}
</button>
</li>
</>
))}
{this.amTopMod && (
<li className="list-inline-item-action">
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(this, this.handleDeleteClick)}
data-tippy-content={
2020-12-24 01:58:27 +00:00
!community_view.community.deleted
2021-02-22 02:39:04 +00:00
? i18n.t("delete")
: i18n.t("restore")
}
aria-label={
!community_view.community.deleted
2021-02-22 02:39:04 +00:00
? i18n.t("delete")
: i18n.t("restore")
}
>
2021-02-11 20:35:27 +00:00
<Icon
icon="trash"
classes={`icon-inline ${
2021-02-22 02:39:04 +00:00
community_view.community.deleted && "text-danger"
}`}
2021-02-11 20:35:27 +00:00
/>
</button>
</li>
)}
</>
)}
{this.canAdmin && (
<li className="list-inline-item">
2020-12-24 01:58:27 +00:00
{!this.props.community_view.community.removed ? (
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(this, this.handleModRemoveShow)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("remove")}
</button>
) : (
<button
class="btn btn-link text-muted d-inline-block"
onClick={linkEvent(this, this.handleModRemoveSubmit)}
>
2021-02-22 02:39:04 +00:00
{i18n.t("restore")}
</button>
)}
</li>
)}
</ul>
{this.state.showRemoveDialog && (
<form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
<div class="form-group row">
<label class="col-form-label" htmlFor="remove-reason">
2021-02-22 02:39:04 +00:00
{i18n.t("reason")}
</label>
<input
type="text"
id="remove-reason"
class="form-control mr-2"
2021-02-22 02:39:04 +00:00
placeholder={i18n.t("optional")}
value={this.state.removeReason}
onInput={linkEvent(this, this.handleModRemoveReasonChange)}
/>
</div>
{/* TODO hold off on expires for now */}
{/* <div class="form-group row"> */}
{/* <label class="col-form-label">Expires</label> */}
{/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
{/* </div> */}
<div class="form-group row">
<button type="submit" class="btn btn-secondary">
2021-02-22 02:39:04 +00:00
{i18n.t("remove_community")}
</button>
</div>
</form>
)}
</>
);
}
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();
2020-12-24 01:58:27 +00:00
let deleteForm: DeleteCommunity = {
community_id: i.props.community_view.community.id,
2020-12-24 01:58:27 +00:00
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) {
2020-12-24 01:58:27 +00:00
let form: AddModToCommunity = {
2021-03-15 18:09:31 +00:00
person_id: UserService.Instance.localUserView.person.id,
2020-12-24 01:58:27 +00:00
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();
2020-12-24 01:58:27 +00:00
let form: FollowCommunity = {
community_id: communityId,
follow: false,
auth: authField(),
};
WebSocketService.Instance.send(wsClient.followCommunity(form));
}
handleSubscribe(communityId: number, event: any) {
event.preventDefault();
2020-12-24 01:58:27 +00:00
let form: FollowCommunity = {
community_id: communityId,
follow: true,
auth: authField(),
};
WebSocketService.Instance.send(wsClient.followCommunity(form));
}
private get amTopMod(): boolean {
2021-03-15 18:09:31 +00:00
return (
this.props.moderators[0].moderator.id ==
2021-03-15 18:09:31 +00:00
UserService.Instance.localUserView.person.id
);
}
get canMod(): boolean {
return (
2021-03-15 18:09:31 +00:00
UserService.Instance.localUserView &&
this.props.moderators
2020-12-24 01:58:27 +00:00
.map(m => m.moderator.id)
2021-03-15 18:09:31 +00:00
.includes(UserService.Instance.localUserView.person.id)
);
}
get canAdmin(): boolean {
return (
2021-03-15 18:09:31 +00:00
UserService.Instance.localUserView &&
2020-12-24 01:58:27 +00:00
this.props.admins
2021-03-15 18:09:31 +00:00
.map(a => a.person.id)
.includes(UserService.Instance.localUserView.person.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();
2020-12-24 01:58:27 +00:00
let removeForm: RemoveCommunity = {
community_id: i.props.community_view.community.id,
2020-12-24 01:58:27 +00:00
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);
}
}