Files
ThreadNet-Web/apps/web/src/components/views/elements/SettingsFlag.tsx
T

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

137 lines
4.8 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
Copyright 2017 Travis Ralston
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
*/
import React, { type ChangeEvent } from "react";
2025-01-21 13:54:57 +00:00
import { secureRandomString } from "matrix-js-sdk/src/randomstring";
import { SettingsToggleInput } from "@vector-im/compound-web";
import { logger } from "matrix-js-sdk/src/logger";
2021-10-22 17:23:32 -05:00
import SettingsStore from "../../../settings/SettingsStore";
import { _t } from "../../../languageHandler";
2025-02-05 13:25:06 +00:00
import { type SettingLevel } from "../../../settings/SettingLevel";
2026-07-14 09:09:03 +01:00
import { type NullableBooleanSettingKey, defaultWatchManager } from "../../../settings/Settings";
2020-06-08 16:48:32 +01:00
interface IProps {
2020-06-10 14:11:36 +01:00
// The setting must be a boolean
2026-07-14 09:09:03 +01:00
name: NullableBooleanSettingKey;
2020-07-29 11:03:43 -06:00
level: SettingLevel;
2020-06-10 15:57:28 +01:00
roomId?: string; // for per-room settings
label?: string;
2020-06-10 15:57:28 +01:00
isExplicit?: boolean;
hideIfCannotSet?: boolean;
2026-07-14 09:09:03 +01:00
requires?: NullableBooleanSettingKey[];
2020-06-10 15:57:28 +01:00
onChange?(checked: boolean): void;
2020-06-08 16:48:32 +01:00
}
2020-06-08 16:48:32 +01:00
interface IState {
2020-06-10 14:11:36 +01:00
value: boolean;
2020-06-08 16:48:32 +01:00
}
export default class SettingsFlag extends React.Component<IProps, IState> {
2025-01-21 13:54:57 +00:00
private readonly id = `mx_SettingsFlag_${secureRandomString(12)}`;
public constructor(props: IProps) {
2020-06-08 16:48:32 +01:00
super(props);
this.state = {
value: this.getSettingValue(),
2020-06-18 14:32:43 +01:00
};
2020-06-08 16:48:32 +01:00
}
public componentDidMount(): void {
defaultWatchManager.watchSetting(this.props.name, this.props.roomId ?? null, this.onSettingChange);
2026-04-09 13:32:50 +01:00
if (this.props.requires) {
// If we have any dependencies for this feature, also watch those features to ensure we catch the disabled state.
for (const flag of this.props.requires) {
defaultWatchManager.watchSetting(flag, this.props.roomId ?? null, this.onSettingChange);
}
}
}
public componentWillUnmount(): void {
defaultWatchManager.unwatchSetting(this.onSettingChange);
}
private getSettingValue(): boolean {
// If a level defined in props is overridden by a level at a high precedence,
// or the setting lacks support for the desired level, it gets disabled and we should show the overriding value.
if (
!SettingsStore.doesSettingSupportLevel(this.props.name, this.props.level) ||
SettingsStore.settingIsOveriddenAtConfigLevel(this.props.name, this.props.roomId ?? null, this.props.level)
) {
return !!SettingsStore.getValue(this.props.name);
}
return !!SettingsStore.getValueAt(
this.props.level,
this.props.name,
this.props.roomId ?? null,
this.props.isExplicit,
);
}
private onSettingChange = (): void => {
this.setState({
value: this.getSettingValue(),
});
};
private onChange = async (evt: ChangeEvent<HTMLInputElement>): Promise<void> => {
const newValue = evt.target.checked;
try {
await this.save(newValue);
} catch (ex) {
logger.info(`Failed to save setting ${this.props.name}`, ex);
return;
}
this.setState({ value: newValue });
this.props.onChange?.(newValue);
2020-06-18 14:32:43 +01:00
};
2020-06-08 16:48:32 +01:00
private save = async (val?: boolean): Promise<void> => {
2020-07-29 11:03:43 -06:00
await SettingsStore.setValue(
2017-11-04 19:15:55 -07:00
this.props.name,
this.props.roomId ?? null,
2017-11-04 19:15:55 -07:00
this.props.level,
2017-11-05 15:37:06 -07:00
val !== undefined ? val : this.state.value,
2017-11-04 19:15:55 -07:00
);
2020-06-18 14:32:43 +01:00
};
public render(): React.ReactNode {
2024-01-11 09:49:00 +00:00
const disabled = !SettingsStore.canSetValue(this.props.name, this.props.roomId ?? null, this.props.level);
2024-01-11 09:49:00 +00:00
if (disabled && this.props.hideIfCannotSet) return null;
const label = this.props.label ?? SettingsStore.getDisplayName(this.props.name, this.props.level);
2021-06-17 16:22:40 +01:00
const description = SettingsStore.getDescription(this.props.name);
const shouldWarn = SettingsStore.shouldHaveWarning(this.props.name);
const helpMessage = shouldWarn
? _t(
"settings|warning",
{},
{
w: (sub) => <span className="mx_SettingsTab_microcopy_warning">{sub}</span>,
description,
},
)
: description;
const disabledMessage = SettingsStore.disabledMessage(this.props.name);
return (
<SettingsToggleInput
id={this.id}
checked={this.state.value}
onChange={disabledMessage ? undefined : this.onChange}
name={this.props.name}
disabled={disabled}
label={label ?? this.props.name}
helpMessage={helpMessage as string}
disabledMessage={disabledMessage}
/>
);
2020-06-08 16:48:32 +01:00
}
}