1 import autosize from "autosize";
2 import { Component, linkEvent } from "inferno";
10 } from "lemmy-js-client";
11 import { Subscription } from "rxjs";
12 import { i18n } from "../../i18next";
13 import { InitialFetchRequest } from "../../interfaces";
14 import { WebSocketService } from "../../services";
17 capitalizeFirstLetter,
27 import { HtmlTags } from "../common/html-tags";
28 import { Spinner } from "../common/icon";
29 import { PersonListing } from "../person/person-listing";
30 import { SiteForm } from "./site-form";
32 interface AdminSettingsState {
33 siteRes: GetSiteResponse;
34 siteConfigRes: GetSiteConfigResponse;
35 siteConfigForm: SaveSiteConfig;
37 siteConfigLoading: boolean;
40 export class AdminSettings extends Component<any, AdminSettingsState> {
41 private siteConfigTextAreaId = `site-config-${randomStr()}`;
42 private isoData = setIsoData(this.context);
43 private subscription: Subscription;
44 private emptyState: AdminSettingsState = {
45 siteRes: this.isoData.site_res,
54 siteConfigLoading: null,
57 constructor(props: any, context: any) {
58 super(props, context);
60 this.state = this.emptyState;
62 this.parseMessage = this.parseMessage.bind(this);
63 this.subscription = wsSubscribe(this.parseMessage);
65 // Only fetch the data if coming from another route
66 if (this.isoData.path == this.context.router.route.match.url) {
67 this.state.siteConfigRes = this.isoData.routeData[0];
68 this.state.siteConfigForm.config_hjson =
69 this.state.siteConfigRes.config_hjson;
70 this.state.siteConfigLoading = false;
71 this.state.loading = false;
73 WebSocketService.Instance.send(
74 wsClient.getSiteConfig({
81 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
82 let form: GetSiteConfig = { auth: req.auth };
83 return [req.client.getSiteConfig(form)];
88 var textarea: any = document.getElementById(this.siteConfigTextAreaId);
93 componentWillUnmount() {
95 this.subscription.unsubscribe();
99 get documentTitle(): string {
100 return `${i18n.t("admin_settings")} - ${
101 this.state.siteRes.site_view.site.name
107 <div class="container">
109 title={this.documentTitle}
110 path={this.context.router.route.match.url}
112 {this.state.loading ? (
118 <div class="col-12 col-md-6">
119 {this.state.siteRes.site_view.site.id && (
120 <SiteForm site={this.state.siteRes.site_view.site} />
125 <div class="col-12 col-md-6">{this.adminSettings()}</div>
135 <h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
136 <ul class="list-unstyled">
137 {this.state.siteRes.admins.map(admin => (
138 <li class="list-inline-item">
139 <PersonListing person={admin.person} />
150 <h5>{i18n.t("banned_users")}</h5>
151 <ul class="list-unstyled">
152 {this.state.siteRes.banned.map(banned => (
153 <li class="list-inline-item">
154 <PersonListing person={banned.person} />
165 <h5>{i18n.t("admin_settings")}</h5>
166 <form onSubmit={linkEvent(this, this.handleSiteConfigSubmit)}>
167 <div class="form-group row">
169 class="col-12 col-form-label"
170 htmlFor={this.siteConfigTextAreaId}
172 {i18n.t("site_config")}
176 id={this.siteConfigTextAreaId}
177 value={this.state.siteConfigForm.config_hjson}
178 onInput={linkEvent(this, this.handleSiteConfigHjsonChange)}
179 class="form-control text-monospace"
184 <div class="form-group row">
186 <button type="submit" class="btn btn-secondary mr-2">
187 {this.state.siteConfigLoading ? (
190 capitalizeFirstLetter(i18n.t("save"))
200 handleSiteConfigSubmit(i: AdminSettings, event: any) {
201 event.preventDefault();
202 i.state.siteConfigLoading = true;
203 WebSocketService.Instance.send(
204 wsClient.saveSiteConfig(i.state.siteConfigForm)
209 handleSiteConfigHjsonChange(i: AdminSettings, event: any) {
210 i.state.siteConfigForm.config_hjson = event.target.value;
214 parseMessage(msg: any) {
215 let op = wsUserOp(msg);
218 toast(i18n.t(msg.error), "danger");
219 this.context.router.history.push("/");
220 this.state.loading = false;
221 this.setState(this.state);
223 } else if (op == UserOperation.EditSite) {
224 let data = wsJsonToRes<SiteResponse>(msg).data;
225 this.state.siteRes.site_view = data.site_view;
226 this.setState(this.state);
227 toast(i18n.t("site_saved"));
228 } else if (op == UserOperation.GetSiteConfig) {
229 let data = wsJsonToRes<GetSiteConfigResponse>(msg).data;
230 this.state.siteConfigRes = data;
231 this.state.loading = false;
232 this.state.siteConfigForm.config_hjson =
233 this.state.siteConfigRes.config_hjson;
234 this.setState(this.state);
235 var textarea: any = document.getElementById(this.siteConfigTextAreaId);
237 } else if (op == UserOperation.SaveSiteConfig) {
238 let data = wsJsonToRes<GetSiteConfigResponse>(msg).data;
239 this.state.siteConfigRes = data;
240 this.state.siteConfigForm.config_hjson =
241 this.state.siteConfigRes.config_hjson;
242 this.state.siteConfigLoading = false;
243 toast(i18n.t("site_saved"));
244 this.setState(this.state);