Files
ThreadNet-Web/src/components/views/settings/tabs/user/MjolnirUserSettingsTab.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

321 lines
12 KiB
TypeScript
Raw Normal View History

2019-10-31 13:19:54 -06:00
/*
2021-04-06 12:26:50 +01:00
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
2019-10-31 13:19:54 -06:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2023-02-13 11:39:16 +00:00
import React, { ChangeEvent, SyntheticEvent } from "react";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
2021-06-29 13:11:58 +01:00
import { _t } from "../../../../../languageHandler";
2020-07-10 19:07:11 +01:00
import SdkConfig from "../../../../../SdkConfig";
2021-06-29 13:11:58 +01:00
import { Mjolnir } from "../../../../../mjolnir/Mjolnir";
import { ListRule } from "../../../../../mjolnir/ListRule";
import { BanList, RULE_SERVER, RULE_USER } from "../../../../../mjolnir/BanList";
import Modal from "../../../../../Modal";
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
2021-07-03 10:06:42 +02:00
import ErrorDialog from "../../../dialogs/ErrorDialog";
import QuestionDialog from "../../../dialogs/QuestionDialog";
import AccessibleButton from "../../../elements/AccessibleButton";
import Field from "../../../elements/Field";
import SettingsTab from "../SettingsTab";
import { SettingsSection } from "../../shared/SettingsSection";
import SettingsSubsection, { SettingsSubsectionText } from "../../shared/SettingsSubsection";
2019-10-31 13:19:54 -06:00
2021-04-06 12:26:50 +01:00
interface IState {
busy: boolean;
newPersonalRule: string;
newList: string;
}
export default class MjolnirUserSettingsTab extends React.Component<{}, IState> {
2023-02-13 11:39:16 +00:00
public constructor(props: {}) {
2021-04-06 12:26:50 +01:00
super(props);
this.state = {
busy: false,
newPersonalRule: "",
newList: "",
};
}
2023-02-13 11:39:16 +00:00
private onPersonalRuleChanged = (e: ChangeEvent<HTMLInputElement>): void => {
2021-06-29 13:11:58 +01:00
this.setState({ newPersonalRule: e.target.value });
};
2023-02-13 11:39:16 +00:00
private onNewListChanged = (e: ChangeEvent<HTMLInputElement>): void => {
2021-06-29 13:11:58 +01:00
this.setState({ newList: e.target.value });
};
2023-02-13 11:39:16 +00:00
private onAddPersonalRule = async (e: SyntheticEvent): Promise<void> => {
e.preventDefault();
e.stopPropagation();
let kind = RULE_SERVER;
if (this.state.newPersonalRule.startsWith("@")) {
kind = RULE_USER;
}
2021-06-29 13:11:58 +01:00
this.setState({ busy: true });
try {
const list = await Mjolnir.sharedInstance().getOrCreatePersonalList();
await list.banEntity(kind, this.state.newPersonalRule, _t("labs_mjolnir|ban_reason"));
2021-06-29 13:11:58 +01:00
this.setState({ newPersonalRule: "" }); // this will also cause the new rule to be rendered
} catch (e) {
2021-10-15 16:30:53 +02:00
logger.error(e);
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("labs_mjolnir|error_adding_ignore"),
description: _t("labs_mjolnir|something_went_wrong"),
});
} finally {
2021-06-29 13:11:58 +01:00
this.setState({ busy: false });
}
};
2023-02-13 11:39:16 +00:00
private onSubscribeList = async (e: SyntheticEvent): Promise<void> => {
e.preventDefault();
e.stopPropagation();
2021-06-29 13:11:58 +01:00
this.setState({ busy: true });
try {
const room = await MatrixClientPeg.safeGet().joinRoom(this.state.newList);
await Mjolnir.sharedInstance().subscribeToList(room.roomId);
2021-06-29 13:11:58 +01:00
this.setState({ newList: "" }); // this will also cause the new rule to be rendered
} catch (e) {
2021-10-15 16:30:53 +02:00
logger.error(e);
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("labs_mjolnir|error_adding_list_title"),
description: _t("labs_mjolnir|error_adding_list_description"),
});
} finally {
2021-06-29 13:11:58 +01:00
this.setState({ busy: false });
}
};
private async removePersonalRule(rule: ListRule): Promise<void> {
2021-06-29 13:11:58 +01:00
this.setState({ busy: true });
try {
const list = Mjolnir.sharedInstance().getPersonalList();
await list!.unbanEntity(rule.kind, rule.entity);
} catch (e) {
2021-10-15 16:30:53 +02:00
logger.error(e);
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("labs_mjolnir|error_removing_ignore"),
description: _t("labs_mjolnir|something_went_wrong"),
});
} finally {
2021-06-29 13:11:58 +01:00
this.setState({ busy: false });
}
}
private async unsubscribeFromList(list: BanList): Promise<void> {
2021-06-29 13:11:58 +01:00
this.setState({ busy: true });
try {
await Mjolnir.sharedInstance().unsubscribeFromList(list.roomId);
await MatrixClientPeg.safeGet().leave(list.roomId);
} catch (e) {
2021-10-15 16:30:53 +02:00
logger.error(e);
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("labs_mjolnir|error_removing_list_title"),
description: _t("labs_mjolnir|error_removing_list_description"),
});
} finally {
2021-06-29 13:11:58 +01:00
this.setState({ busy: false });
}
}
private viewListRules(list: BanList): void {
const room = MatrixClientPeg.safeGet().getRoom(list.roomId);
2019-10-31 16:00:31 -06:00
const name = room ? room.name : list.roomId;
const renderRules = (rules: ListRule[]): JSX.Element => {
if (rules.length === 0) return <i>{_t("None")}</i>;
2019-10-31 16:00:31 -06:00
const tiles: JSX.Element[] = [];
2019-10-31 16:00:31 -06:00
for (const rule of rules) {
tiles.push(
<li key={rule.kind + rule.entity}>
<code>{rule.entity}</code>
</li>,
);
2019-10-31 16:00:31 -06:00
}
return <ul>{tiles}</ul>;
2019-10-31 16:00:31 -06:00
};
2022-06-14 17:51:51 +01:00
Modal.createDialog(QuestionDialog, {
title: _t("labs_mjolnir|rules_title", { roomName: name }),
2019-10-31 16:00:31 -06:00
description: (
<div>
<h3>{_t("labs_mjolnir|rules_server")}</h3>
{renderRules(list.serverRules)}
<h3>{_t("labs_mjolnir|rules_user")}</h3>
{renderRules(list.userRules)}
2019-10-31 16:00:31 -06:00
</div>
),
button: _t("action|close"),
2019-10-31 16:00:31 -06:00
hasCancelButton: false,
});
}
private renderPersonalBanListRules(): JSX.Element {
const list = Mjolnir.sharedInstance().getPersonalList();
const rules = list ? [...list.userRules, ...list.serverRules] : [];
if (!list || rules.length <= 0) return <i>{_t("labs_mjolnir|personal_empty")}</i>;
const tiles: JSX.Element[] = [];
for (const rule of rules) {
tiles.push(
<li key={rule.entity} className="mx_MjolnirUserSettingsTab_listItem">
<AccessibleButton
kind="danger_sm"
onClick={() => this.removePersonalRule(rule)}
disabled={this.state.busy}
>
{_t("action|remove")}
</AccessibleButton>
&nbsp;
<code>{rule.entity}</code>
</li>,
);
}
return (
<div>
<p>{_t("labs_mjolnir|personal_section")}</p>
<ul>{tiles}</ul>
</div>
);
}
private renderSubscribedBanLists(): JSX.Element {
const personalList = Mjolnir.sharedInstance().getPersonalList();
2019-10-31 16:27:45 -06:00
const lists = Mjolnir.sharedInstance().lists.filter((b) => {
return personalList ? personalList.roomId !== b.roomId : true;
});
if (!lists || lists.length <= 0) return <i>{_t("labs_mjolnir|no_lists")}</i>;
const tiles: JSX.Element[] = [];
for (const list of lists) {
const room = MatrixClientPeg.safeGet().getRoom(list.roomId);
const name = room ? (
<span>
{room.name} (<code>{list.roomId}</code>)
</span>
) : (
<code>list.roomId</code>
);
tiles.push(
<li key={list.roomId} className="mx_MjolnirUserSettingsTab_listItem">
<AccessibleButton
kind="danger_sm"
onClick={() => this.unsubscribeFromList(list)}
disabled={this.state.busy}
>
{_t("action|unsubscribe")}
</AccessibleButton>
&nbsp;
<AccessibleButton
kind="primary_sm"
onClick={() => this.viewListRules(list)}
disabled={this.state.busy}
>
{_t("labs_mjolnir|view_rules")}
</AccessibleButton>
&nbsp;
{name}
</li>,
);
}
return (
<div>
<p>{_t("labs_mjolnir|lists")}</p>
<ul>{tiles}</ul>
</div>
);
2019-10-31 13:19:54 -06:00
}
public render(): React.ReactNode {
2020-07-10 19:07:11 +01:00
const brand = SdkConfig.get().brand;
2019-10-31 13:19:54 -06:00
return (
<SettingsTab>
<SettingsSection heading={_t("labs_mjolnir|title")}>
<SettingsSubsectionText>
<span className="warning">{_t("labs_mjolnir|advanced_warning")}</span>
<p>{_t("labs_mjolnir|explainer_1", { brand }, { code: (s) => <code>{s}</code> })}</p>
<p>{_t("labs_mjolnir|explainer_2")}</p>
</SettingsSubsectionText>
<SettingsSubsection
heading={_t("labs_mjolnir|personal_heading")}
description={_t("labs_mjolnir|personal_description", {
myBanList: _t("labs_mjolnir|room_name"),
})}
>
{this.renderPersonalBanListRules()}
<form onSubmit={this.onAddPersonalRule} autoComplete="off">
<Field
type="text"
label={_t("labs_mjolnir|personal_new_label")}
placeholder={_t("labs_mjolnir|personal_new_placeholder")}
value={this.state.newPersonalRule}
onChange={this.onPersonalRuleChanged}
/>
<AccessibleButton
type="submit"
kind="primary"
onClick={this.onAddPersonalRule}
disabled={this.state.busy}
>
{_t("action|ignore")}
</AccessibleButton>
</form>
</SettingsSubsection>
<SettingsSubsection
heading={_t("labs_mjolnir|lists_heading")}
description={
<>
<span className="warning">{_t("labs_mjolnir|lists_description_1")}</span>
&nbsp;
<span>{_t("labs_mjolnir|lists_description_2")}</span>
</>
}
>
{this.renderSubscribedBanLists()}
<form onSubmit={this.onSubscribeList} autoComplete="off">
<Field
type="text"
label={_t("labs_mjolnir|lists_new_label")}
value={this.state.newList}
onChange={this.onNewListChanged}
/>
<AccessibleButton
type="submit"
kind="primary"
onClick={this.onSubscribeList}
disabled={this.state.busy}
>
{_t("action|subscribe")}
</AccessibleButton>
</form>
</SettingsSubsection>
</SettingsSection>
</SettingsTab>
2019-10-31 13:19:54 -06:00
);
}
}