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

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

324 lines
13 KiB
TypeScript
Raw Normal View History

2019-01-18 20:22:36 -07:00
/*
Copyright 2019 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
2019-01-18 20:22:36 -07: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.
*/
import React, { ReactNode } from "react";
import { HTTPError } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
import { UserFriendlyError, _t } from "../../../../../languageHandler";
import UserProfileSettings from "../../UserProfileSettings";
2019-02-22 10:47:18 -07:00
import * as languageHandler from "../../../../../languageHandler";
import SettingsStore from "../../../../../settings/SettingsStore";
import LanguageDropdown from "../../../elements/LanguageDropdown";
2020-12-01 20:36:25 +01:00
import SpellCheckSettings from "../../SpellCheckSettings";
2019-02-22 10:47:18 -07:00
import AccessibleButton from "../../../elements/AccessibleButton";
import DeactivateAccountDialog from "../../../dialogs/DeactivateAccountDialog";
2019-08-09 18:07:58 +01:00
import PlatformPeg from "../../../../../PlatformPeg";
import Modal from "../../../../../Modal";
2021-06-29 13:11:58 +01:00
import { SettingLevel } from "../../../../../settings/SettingLevel";
import { UIFeature } from "../../../../../settings/UIFeature";
import ErrorDialog, { extractErrorMessageFromError } from "../../../dialogs/ErrorDialog";
2021-09-21 13:45:38 +02:00
import ChangePassword from "../../ChangePassword";
import SetIntegrationManager from "../../SetIntegrationManager";
import ToggleSwitch from "../../../elements/ToggleSwitch";
import { IS_MAC } from "../../../../../Keyboard";
import SettingsTab from "../SettingsTab";
import { SettingsSection } from "../../shared/SettingsSection";
import SettingsSubsection, { SettingsSubsectionText } from "../../shared/SettingsSubsection";
import { SettingsSubsectionHeading } from "../../shared/SettingsSubsectionHeading";
import { SDKContext } from "../../../../../contexts/SDKContext";
import UserPersonalInfoSettings from "../../UserPersonalInfoSettings";
2021-09-21 13:45:38 +02:00
interface IProps {
closeSettingsFn: () => void;
}
interface IState {
language: string;
spellCheckEnabled?: boolean;
2021-09-21 13:45:38 +02:00
spellCheckLanguages: string[];
canChangePassword: boolean;
idServerName?: string;
externalAccountManagementUrl?: string;
canMake3pidChanges: boolean;
2021-09-21 13:45:38 +02:00
}
2019-01-18 20:22:36 -07:00
2021-09-21 13:45:38 +02:00
export default class GeneralUserSettingsTab extends React.Component<IProps, IState> {
public static contextType = SDKContext;
public context!: React.ContextType<typeof SDKContext>;
public constructor(props: IProps, context: React.ContextType<typeof SDKContext>) {
2021-09-21 13:45:38 +02:00
super(props);
this.context = context;
2019-01-23 14:43:45 -07:00
this.state = {
language: languageHandler.getCurrentLanguage(),
spellCheckEnabled: false,
spellCheckLanguages: [],
2021-09-21 13:45:38 +02:00
canChangePassword: false,
canMake3pidChanges: false,
2019-01-23 14:43:45 -07:00
};
this.getCapabilities();
}
2021-09-21 13:45:38 +02:00
public async componentDidMount(): Promise<void> {
const plat = PlatformPeg.get();
const [spellCheckEnabled, spellCheckLanguages] = await Promise.all([
plat?.getSpellCheckEnabled(),
plat?.getSpellCheckLanguages(),
]);
if (spellCheckLanguages) {
this.setState({
spellCheckEnabled,
spellCheckLanguages,
});
}
}
private async getCapabilities(): Promise<void> {
const cli = this.context.client!;
const capabilities = await cli.getCapabilities(); // this is cached
const changePasswordCap = capabilities["m.change_password"];
// You can change your password so long as the capability isn't explicitly disabled. The implicit
// behaviour is you can change your password when the capability is missing or has not-false as
// the enabled flag value.
const canChangePassword = !changePasswordCap || changePasswordCap["enabled"] !== false;
await this.context.oidcClientStore.readyPromise; // wait for the store to be ready
const externalAccountManagementUrl = this.context.oidcClientStore.accountManagementEndpoint;
// https://spec.matrix.org/v1.7/client-server-api/#m3pid_changes-capability
// We support as far back as v1.1 which doesn't have m.3pid_changes
// so the behaviour for when it is missing has to be assume true
const canMake3pidChanges = !capabilities["m.3pid_changes"] || capabilities["m.3pid_changes"].enabled === true;
this.setState({ canChangePassword, externalAccountManagementUrl, canMake3pidChanges });
}
2021-09-21 13:45:38 +02:00
private onLanguageChange = (newLanguage: string): void => {
2019-01-23 14:43:45 -07:00
if (this.state.language === newLanguage) return;
SettingsStore.setValue("language", null, SettingLevel.DEVICE, newLanguage);
2021-06-29 13:11:58 +01:00
this.setState({ language: newLanguage });
const platform = PlatformPeg.get();
if (platform) {
2021-09-21 13:45:38 +02:00
platform.setLanguage([newLanguage]);
platform.reload();
}
2019-01-23 14:43:45 -07:00
};
2021-09-21 13:45:38 +02:00
private onSpellCheckLanguagesChange = (languages: string[]): void => {
2021-06-29 13:11:58 +01:00
this.setState({ spellCheckLanguages: languages });
PlatformPeg.get()?.setSpellCheckLanguages(languages);
};
2020-12-01 20:36:25 +01:00
private onSpellCheckEnabledChange = (spellCheckEnabled: boolean): void => {
this.setState({ spellCheckEnabled });
PlatformPeg.get()?.setSpellCheckEnabled(spellCheckEnabled);
2020-11-26 13:51:03 +01:00
};
private onPasswordChangeError = (err: Error): void => {
logger.error("Failed to change password: " + err);
let underlyingError = err;
if (err instanceof UserFriendlyError && err.cause instanceof Error) {
underlyingError = err.cause;
2019-01-22 13:09:40 -07:00
}
const errorMessage = extractErrorMessageFromError(
err,
_t("settings|general|error_password_change_unknown", {
stringifiedError: String(err),
}),
);
let errorMessageToDisplay = errorMessage;
if (underlyingError instanceof HTTPError && underlyingError.httpStatus === 403) {
errorMessageToDisplay = _t("settings|general|error_password_change_403");
} else if (underlyingError instanceof HTTPError) {
errorMessageToDisplay = _t("settings|general|error_password_change_http", {
errorMessage,
httpStatus: underlyingError.httpStatus,
});
}
// TODO: Figure out a design that doesn't involve replacing the current dialog
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("settings|general|error_password_change_title"),
description: errorMessageToDisplay,
2019-01-22 13:09:40 -07:00
});
};
private onPasswordChanged = (): void => {
const description = _t("settings|general|password_change_success");
2019-01-22 13:09:40 -07:00
// TODO: Figure out a design that doesn't involve replacing the current dialog
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("common|success"),
description,
2019-01-22 13:09:40 -07:00
});
};
2021-09-21 13:45:38 +02:00
private onDeactivateClicked = (): void => {
2022-06-14 17:51:51 +01:00
Modal.createDialog(DeactivateAccountDialog, {
2019-07-11 14:54:49 -06:00
onFinished: (success) => {
if (success) this.props.closeSettingsFn();
},
});
2019-01-23 15:56:58 -07:00
};
2021-09-21 13:45:38 +02:00
private renderAccountSection(): JSX.Element {
let passwordChangeSection: ReactNode = null;
if (this.state.canChangePassword) {
passwordChangeSection = (
<>
<SettingsSubsectionText>{_t("settings|general|password_change_section")}</SettingsSubsectionText>
<ChangePassword
className="mx_GeneralUserSettingsTab_section--account_changePassword"
rowClassName=""
buttonKind="primary"
onError={this.onPasswordChangeError}
onFinished={this.onPasswordChanged}
/>
</>
);
}
let externalAccountManagement: JSX.Element | undefined;
if (this.state.externalAccountManagementUrl) {
const { hostname } = new URL(this.state.externalAccountManagementUrl);
externalAccountManagement = (
<>
<SettingsSubsectionText data-testid="external-account-management-outer">
{_t(
"settings|general|external_account_management",
{ hostname },
{ code: (sub) => <code>{sub}</code> },
)}
</SettingsSubsectionText>
<AccessibleButton
onClick={null}
element="a"
kind="primary"
target="_blank"
rel="noreferrer noopener"
href={this.state.externalAccountManagementUrl}
data-testid="external-account-management-link"
>
{_t("settings|general|oidc_manage_button")}
</AccessibleButton>
</>
);
}
2019-01-22 10:36:47 -07:00
return (
<>
<SettingsSubsection
heading={_t("settings|general|account_section")}
stretchContent
data-testid="accountSection"
>
{externalAccountManagement}
{passwordChangeSection}
</SettingsSubsection>
</>
2019-01-22 10:36:47 -07:00
);
}
2021-09-21 13:45:38 +02:00
private renderLanguageSection(): JSX.Element {
2019-01-23 14:43:45 -07:00
// TODO: Convert to new-styled Field
2019-01-22 10:36:47 -07:00
return (
<SettingsSubsection heading={_t("settings|general|language_section")} stretchContent>
2021-04-27 17:23:27 +02:00
<LanguageDropdown
className="mx_GeneralUserSettingsTab_section_languageInput"
2021-09-21 13:45:38 +02:00
onOptionChange={this.onLanguageChange}
2021-04-27 17:23:27 +02:00
value={this.state.language}
/>
</SettingsSubsection>
2019-01-22 10:36:47 -07:00
);
}
2021-09-21 13:45:38 +02:00
private renderSpellCheckSection(): JSX.Element {
const heading = (
<SettingsSubsectionHeading heading={_t("settings|general|spell_check_section")}>
<ToggleSwitch checked={!!this.state.spellCheckEnabled} onChange={this.onSpellCheckEnabledChange} />
</SettingsSubsectionHeading>
);
2020-11-26 13:51:03 +01:00
return (
<SettingsSubsection heading={heading} stretchContent>
{this.state.spellCheckEnabled && !IS_MAC && (
<SpellCheckSettings
2021-04-27 17:23:27 +02:00
languages={this.state.spellCheckLanguages}
2021-09-21 13:45:38 +02:00
onLanguagesChange={this.onSpellCheckLanguagesChange}
/>
)}
</SettingsSubsection>
2020-11-26 13:51:03 +01:00
);
}
2021-09-21 13:45:38 +02:00
private renderManagementSection(): JSX.Element {
2019-01-23 15:56:58 -07:00
// TODO: Improve warning text for account deactivation
2019-01-22 10:36:47 -07:00
return (
<SettingsSection heading={_t("settings|general|deactivate_section")}>
<SettingsSubsection
heading={_t("settings|general|account_management_section")}
data-testid="account-management-section"
description={_t("settings|general|deactivate_warning")}
>
<AccessibleButton onClick={this.onDeactivateClicked} kind="danger">
{_t("settings|general|deactivate_section")}
</AccessibleButton>
</SettingsSubsection>
</SettingsSection>
2019-01-22 10:36:47 -07:00
);
}
private renderIntegrationManagerSection(): ReactNode {
if (!SettingsStore.getValue(UIFeature.Widgets)) return null;
return <SetIntegrationManager />;
}
public render(): React.ReactNode {
const plaf = PlatformPeg.get();
const supportsMultiLanguageSpellCheck = plaf?.supportsSpellCheckSettings();
let accountManagementSection: JSX.Element | undefined;
const isAccountManagedExternally = !!this.state.externalAccountManagementUrl;
if (SettingsStore.getValue(UIFeature.Deactivate) && !isAccountManagedExternally) {
accountManagementSection = this.renderManagementSection();
}
2019-01-18 20:22:36 -07:00
return (
<SettingsTab data-testid="mx_GeneralUserSettingsTab">
<SettingsSection>
<UserProfileSettings />
<UserPersonalInfoSettings canMake3pidChanges={this.state.canMake3pidChanges} />
{this.renderAccountSection()}
{this.renderLanguageSection()}
{supportsMultiLanguageSpellCheck ? this.renderSpellCheckSection() : null}
</SettingsSection>
{this.renderIntegrationManagerSection()}
{accountManagementSection}
</SettingsTab>
2019-01-18 20:22:36 -07:00
);
}
}