mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-01 18:19:55 +00:00
258 lines
7.3 KiB
TypeScript
258 lines
7.3 KiB
TypeScript
import { Component, linkEvent } from 'inferno';
|
|
import { Subscription } from 'rxjs';
|
|
import {
|
|
UserOperation,
|
|
SiteResponse,
|
|
GetSiteResponse,
|
|
SiteConfigForm,
|
|
GetSiteConfigResponse,
|
|
WebSocketJsonResponse,
|
|
GetSiteConfig,
|
|
} from 'lemmy-js-client';
|
|
import { WebSocketService } from '../services';
|
|
import {
|
|
wsJsonToRes,
|
|
capitalizeFirstLetter,
|
|
toast,
|
|
randomStr,
|
|
setIsoData,
|
|
wsSubscribe,
|
|
isBrowser,
|
|
lemmyHttp,
|
|
setAuth,
|
|
} from '../utils';
|
|
import autosize from 'autosize';
|
|
import { SiteForm } from './site-form';
|
|
import { UserListing } from './user-listing';
|
|
import { HtmlTags } from './html-tags';
|
|
import { i18n } from '../i18next';
|
|
|
|
interface AdminSettingsState {
|
|
siteRes: GetSiteResponse;
|
|
siteConfigRes: GetSiteConfigResponse;
|
|
siteConfigForm: SiteConfigForm;
|
|
loading: boolean;
|
|
siteConfigLoading: boolean;
|
|
}
|
|
|
|
export class AdminSettings extends Component<any, AdminSettingsState> {
|
|
private siteConfigTextAreaId = `site-config-${randomStr()}`;
|
|
private isoData = setIsoData(this.context);
|
|
private subscription: Subscription;
|
|
private emptyState: AdminSettingsState = {
|
|
siteRes: this.isoData.site,
|
|
siteConfigForm: {
|
|
config_hjson: null,
|
|
auth: null,
|
|
},
|
|
siteConfigRes: {
|
|
config_hjson: null,
|
|
},
|
|
loading: true,
|
|
siteConfigLoading: null,
|
|
};
|
|
|
|
constructor(props: any, context: any) {
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
// Only fetch the data if coming from another route
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
this.state.siteConfigRes = this.isoData.routeData[0];
|
|
this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
|
|
this.state.siteConfigLoading = false;
|
|
this.state.loading = false;
|
|
} else {
|
|
WebSocketService.Instance.getSiteConfig();
|
|
}
|
|
}
|
|
|
|
static fetchInitialData(auth: string, _path: string): Promise<any>[] {
|
|
let form: GetSiteConfig = {};
|
|
setAuth(form, auth);
|
|
return [lemmyHttp.getSiteConfig(form)];
|
|
}
|
|
|
|
componentDidMount() {
|
|
if (isBrowser()) {
|
|
var textarea: any = document.getElementById(this.siteConfigTextAreaId);
|
|
autosize(textarea);
|
|
}
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (isBrowser()) {
|
|
this.subscription.unsubscribe();
|
|
}
|
|
}
|
|
|
|
get documentTitle(): string {
|
|
return `${i18n.t('admin_settings')} - ${this.state.siteRes.site.name}`;
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<div class="container">
|
|
<HtmlTags
|
|
title={this.documentTitle}
|
|
path={this.context.router.route.match.url}
|
|
/>
|
|
{this.state.loading ? (
|
|
<h5>
|
|
<svg class="icon icon-spinner spin">
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
</svg>
|
|
</h5>
|
|
) : (
|
|
<div class="row">
|
|
<div class="col-12 col-md-6">
|
|
{this.state.siteRes.site.id && (
|
|
<SiteForm site={this.state.siteRes.site} />
|
|
)}
|
|
{this.admins()}
|
|
{this.bannedUsers()}
|
|
</div>
|
|
<div class="col-12 col-md-6">{this.adminSettings()}</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
admins() {
|
|
return (
|
|
<>
|
|
<h5>{capitalizeFirstLetter(i18n.t('admins'))}</h5>
|
|
<ul class="list-unstyled">
|
|
{this.state.siteRes.admins.map(admin => (
|
|
<li class="list-inline-item">
|
|
<UserListing
|
|
user={{
|
|
name: admin.name,
|
|
preferred_username: admin.preferred_username,
|
|
avatar: admin.avatar,
|
|
id: admin.id,
|
|
local: admin.local,
|
|
actor_id: admin.actor_id,
|
|
}}
|
|
/>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</>
|
|
);
|
|
}
|
|
|
|
bannedUsers() {
|
|
return (
|
|
<>
|
|
<h5>{i18n.t('banned_users')}</h5>
|
|
<ul class="list-unstyled">
|
|
{this.state.siteRes.banned.map(banned => (
|
|
<li class="list-inline-item">
|
|
<UserListing
|
|
user={{
|
|
name: banned.name,
|
|
preferred_username: banned.preferred_username,
|
|
avatar: banned.avatar,
|
|
id: banned.id,
|
|
local: banned.local,
|
|
actor_id: banned.actor_id,
|
|
}}
|
|
/>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</>
|
|
);
|
|
}
|
|
|
|
adminSettings() {
|
|
return (
|
|
<div>
|
|
<h5>{i18n.t('admin_settings')}</h5>
|
|
<form onSubmit={linkEvent(this, this.handleSiteConfigSubmit)}>
|
|
<div class="form-group row">
|
|
<label
|
|
class="col-12 col-form-label"
|
|
htmlFor={this.siteConfigTextAreaId}
|
|
>
|
|
{i18n.t('site_config')}
|
|
</label>
|
|
<div class="col-12">
|
|
<textarea
|
|
id={this.siteConfigTextAreaId}
|
|
value={this.state.siteConfigForm.config_hjson}
|
|
onInput={linkEvent(this, this.handleSiteConfigHjsonChange)}
|
|
class="form-control text-monospace"
|
|
rows={3}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div class="form-group row">
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-secondary mr-2">
|
|
{this.state.siteConfigLoading ? (
|
|
<svg class="icon icon-spinner spin">
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
</svg>
|
|
) : (
|
|
capitalizeFirstLetter(i18n.t('save'))
|
|
)}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
handleSiteConfigSubmit(i: AdminSettings, event: any) {
|
|
event.preventDefault();
|
|
i.state.siteConfigLoading = true;
|
|
WebSocketService.Instance.saveSiteConfig(i.state.siteConfigForm);
|
|
i.setState(i.state);
|
|
}
|
|
|
|
handleSiteConfigHjsonChange(i: AdminSettings, event: any) {
|
|
i.state.siteConfigForm.config_hjson = event.target.value;
|
|
i.setState(i.state);
|
|
}
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
console.log(msg);
|
|
let res = wsJsonToRes(msg);
|
|
if (msg.error) {
|
|
toast(i18n.t(msg.error), 'danger');
|
|
this.context.router.history.push('/');
|
|
this.state.loading = false;
|
|
this.setState(this.state);
|
|
return;
|
|
} else if (msg.reconnect) {
|
|
} else if (res.op == UserOperation.EditSite) {
|
|
let data = res.data as SiteResponse;
|
|
this.state.siteRes.site = data.site;
|
|
this.setState(this.state);
|
|
toast(i18n.t('site_saved'));
|
|
} else if (res.op == UserOperation.GetSiteConfig) {
|
|
let data = res.data as GetSiteConfigResponse;
|
|
this.state.siteConfigRes = data;
|
|
this.state.loading = false;
|
|
this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
|
|
this.setState(this.state);
|
|
var textarea: any = document.getElementById(this.siteConfigTextAreaId);
|
|
autosize(textarea);
|
|
} else if (res.op == UserOperation.SaveSiteConfig) {
|
|
let data = res.data as GetSiteConfigResponse;
|
|
this.state.siteConfigRes = data;
|
|
this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
|
|
this.state.siteConfigLoading = false;
|
|
toast(i18n.t('site_saved'));
|
|
this.setState(this.state);
|
|
}
|
|
}
|
|
}
|