1 import autosize from "autosize";
2 import { Component, linkEvent } from "inferno";
6 GetFederatedInstancesResponse,
13 } from "lemmy-js-client";
14 import { Subscription } from "rxjs";
15 import { i18n } from "../../i18next";
16 import { InitialFetchRequest } from "../../interfaces";
17 import { WebSocketService } from "../../services";
19 capitalizeFirstLetter,
29 import { HtmlTags } from "../common/html-tags";
30 import { Spinner } from "../common/icon";
31 import { PersonListing } from "../person/person-listing";
32 import { EmojiForm } from "./emojis-form";
33 import { SiteForm } from "./site-form";
34 import { TaglineForm } from "./tagline-form";
36 interface AdminSettingsState {
37 siteRes: GetSiteResponse;
38 instancesRes?: GetFederatedInstancesResponse;
41 leaveAdminTeamLoading: boolean;
45 export class AdminSettings extends Component<any, AdminSettingsState> {
46 private siteConfigTextAreaId = `site-config-${randomStr()}`;
47 private isoData = setIsoData(this.context);
48 private subscription?: Subscription;
49 state: AdminSettingsState = {
50 siteRes: this.isoData.site_res,
53 leaveAdminTeamLoading: false,
57 constructor(props: any, context: any) {
58 super(props, context);
60 this.parseMessage = this.parseMessage.bind(this);
61 this.subscription = wsSubscribe(this.parseMessage);
63 // Only fetch the data if coming from another route
64 if (this.isoData.path == this.context.router.route.match.url) {
67 banned: (this.isoData.routeData[0] as BannedPersonsResponse).banned,
68 instancesRes: this.isoData
69 .routeData[1] as GetFederatedInstancesResponse,
75 WebSocketService.Instance.send(
76 wsClient.getBannedPersons({
80 WebSocketService.Instance.send(
81 wsClient.getFederatedInstances({ auth: cAuth })
87 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
88 let promises: Promise<any>[] = [];
92 let bannedPersonsForm: GetBannedPersons = { auth };
93 promises.push(req.client.getBannedPersons(bannedPersonsForm));
94 promises.push(req.client.getFederatedInstances({ auth }));
100 componentDidMount() {
102 var textarea: any = document.getElementById(this.siteConfigTextAreaId);
107 componentWillUnmount() {
109 this.subscription?.unsubscribe();
113 get documentTitle(): string {
114 return `${i18n.t("admin_settings")} - ${
115 this.state.siteRes.site_view.site.name
121 <div className="container-lg">
122 {this.state.loading ? (
129 title={this.documentTitle}
130 path={this.context.router.route.match.url}
132 <ul className="nav nav-tabs mb-2">
133 <li className="nav-item">
135 className={`nav-link btn ${
136 this.state.currentTab == "site" && "active"
139 { ctx: this, tab: "site" },
146 <li className="nav-item">
148 className={`nav-link btn ${
149 this.state.currentTab == "taglines" && "active"
152 { ctx: this, tab: "taglines" },
159 <li className="nav-item">
161 className={`nav-link btn ${
162 this.state.currentTab == "emojis" && "active"
165 { ctx: this, tab: "emojis" },
173 {this.state.currentTab == "site" && (
174 <div className="row">
175 <div className="col-12 col-md-6">
177 siteRes={this.state.siteRes}
178 instancesRes={this.state.instancesRes}
179 showLocal={showLocal(this.isoData)}
182 <div className="col-12 col-md-6">
188 {this.state.currentTab == "taglines" && (
189 <div className="row">
190 <TaglineForm siteRes={this.state.siteRes}></TaglineForm>
193 {this.state.currentTab == "emojis" && (
194 <div className="row">
195 <EmojiForm></EmojiForm>
207 <h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
208 <ul className="list-unstyled">
209 {this.state.siteRes.admins.map(admin => (
210 <li key={admin.person.id} className="list-inline-item">
211 <PersonListing person={admin.person} />
223 onClick={linkEvent(this, this.handleLeaveAdminTeam)}
224 className="btn btn-danger mb-2"
226 {this.state.leaveAdminTeamLoading ? (
229 i18n.t("leave_admin_team")
238 <h5>{i18n.t("banned_users")}</h5>
239 <ul className="list-unstyled">
240 {this.state.banned.map(banned => (
241 <li key={banned.person.id} className="list-inline-item">
242 <PersonListing person={banned.person} />
250 handleSwitchTab(i: { ctx: AdminSettings; tab: string }) {
251 i.ctx.setState({ currentTab: i.tab });
254 handleLeaveAdminTeam(i: AdminSettings) {
257 i.setState({ leaveAdminTeamLoading: true });
258 WebSocketService.Instance.send(wsClient.leaveAdmin({ auth }));
262 parseMessage(msg: any) {
263 let op = wsUserOp(msg);
266 toast(i18n.t(msg.error), "danger");
267 this.context.router.history.push("/");
268 this.setState({ loading: false });
270 } else if (op == UserOperation.EditSite) {
271 let data = wsJsonToRes<SiteResponse>(msg);
272 this.setState(s => ((s.siteRes.site_view = data.site_view), s));
273 toast(i18n.t("site_saved"));
274 } else if (op == UserOperation.GetBannedPersons) {
275 let data = wsJsonToRes<BannedPersonsResponse>(msg);
276 this.setState({ banned: data.banned, loading: false });
277 } else if (op == UserOperation.LeaveAdmin) {
278 let data = wsJsonToRes<GetSiteResponse>(msg);
279 this.setState(s => ((s.siteRes.site_view = data.site_view), s));
280 this.setState({ leaveAdminTeamLoading: false });
281 toast(i18n.t("left_admin_team"));
282 this.context.router.history.push("/");
283 } else if (op == UserOperation.GetFederatedInstances) {
284 let data = wsJsonToRes<GetFederatedInstancesResponse>(msg);
285 this.setState({ instancesRes: data });