1 import { Component, linkEvent } from "inferno";
2 import { Subscription } from "rxjs";
10 } from "lemmy-js-client";
11 import { WebSocketService } from "../services";
14 capitalizeFirstLetter,
24 import autosize from "autosize";
25 import { SiteForm } from "./site-form";
26 import { UserListing } from "./user-listing";
27 import { HtmlTags } from "./html-tags";
28 import { Spinner } from "./icon";
29 import { i18n } from "../i18next";
30 import { InitialFetchRequest } from "shared/interfaces";
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 = this.state.siteConfigRes.config_hjson;
69 this.state.siteConfigLoading = false;
70 this.state.loading = false;
72 WebSocketService.Instance.send(
73 wsClient.getSiteConfig({
80 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
81 let form: GetSiteConfig = { auth: req.auth };
82 return [req.client.getSiteConfig(form)];
87 var textarea: any = document.getElementById(this.siteConfigTextAreaId);
92 componentWillUnmount() {
94 this.subscription.unsubscribe();
98 get documentTitle(): string {
99 return `${i18n.t("admin_settings")} - ${
100 this.state.siteRes.site_view.site.name
106 <div class="container">
108 title={this.documentTitle}
109 path={this.context.router.route.match.url}
111 {this.state.loading ? (
117 <div class="col-12 col-md-6">
118 {this.state.siteRes.site_view.site.id && (
119 <SiteForm site={this.state.siteRes.site_view.site} />
124 <div class="col-12 col-md-6">{this.adminSettings()}</div>
134 <h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
135 <ul class="list-unstyled">
136 {this.state.siteRes.admins.map(admin => (
137 <li class="list-inline-item">
138 <UserListing user={admin.user} />
149 <h5>{i18n.t("banned_users")}</h5>
150 <ul class="list-unstyled">
151 {this.state.siteRes.banned.map(banned => (
152 <li class="list-inline-item">
153 <UserListing user={banned.user} />
164 <h5>{i18n.t("admin_settings")}</h5>
165 <form onSubmit={linkEvent(this, this.handleSiteConfigSubmit)}>
166 <div class="form-group row">
168 class="col-12 col-form-label"
169 htmlFor={this.siteConfigTextAreaId}
171 {i18n.t("site_config")}
175 id={this.siteConfigTextAreaId}
176 value={this.state.siteConfigForm.config_hjson}
177 onInput={linkEvent(this, this.handleSiteConfigHjsonChange)}
178 class="form-control text-monospace"
183 <div class="form-group row">
185 <button type="submit" class="btn btn-secondary mr-2">
186 {this.state.siteConfigLoading ? (
189 capitalizeFirstLetter(i18n.t("save"))
199 handleSiteConfigSubmit(i: AdminSettings, event: any) {
200 event.preventDefault();
201 i.state.siteConfigLoading = true;
202 WebSocketService.Instance.send(
203 wsClient.saveSiteConfig(i.state.siteConfigForm)
208 handleSiteConfigHjsonChange(i: AdminSettings, event: any) {
209 i.state.siteConfigForm.config_hjson = event.target.value;
213 parseMessage(msg: any) {
214 let op = wsUserOp(msg);
216 toast(i18n.t(msg.error), "danger");
217 this.context.router.history.push("/");
218 this.state.loading = false;
219 this.setState(this.state);
221 } else if (op == UserOperation.EditSite) {
222 let data = wsJsonToRes<SiteResponse>(msg).data;
223 this.state.siteRes.site_view = data.site_view;
224 this.setState(this.state);
225 toast(i18n.t("site_saved"));
226 } else if (op == UserOperation.GetSiteConfig) {
227 let data = wsJsonToRes<GetSiteConfigResponse>(msg).data;
228 this.state.siteConfigRes = data;
229 this.state.loading = false;
230 this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
231 this.setState(this.state);
232 var textarea: any = document.getElementById(this.siteConfigTextAreaId);
234 } else if (op == UserOperation.SaveSiteConfig) {
235 let data = wsJsonToRes<GetSiteConfigResponse>(msg).data;
236 this.state.siteConfigRes = data;
237 this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
238 this.state.siteConfigLoading = false;
239 toast(i18n.t("site_saved"));
240 this.setState(this.state);