2017-10-29 19:46:48 -06:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-12-19 17:45:24 -07:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2017 Travis Ralston
|
2017-10-29 19:46:48 -06:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
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.
|
2017-10-29 19:46:48 -06:00
|
|
|
*/
|
|
|
|
|
|
2025-12-04 15:46:21 +00:00
|
|
|
import React, { type ChangeEvent } from "react";
|
2025-01-21 13:54:57 +00:00
|
|
|
import { secureRandomString } from "matrix-js-sdk/src/randomstring";
|
2025-12-04 15:46:21 +00:00
|
|
|
import { SettingsToggleInput } from "@vector-im/compound-web";
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2017-10-29 19:46:48 -06: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";
|
2017-10-29 19:46:48 -06:00
|
|
|
|
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
|
2023-08-22 16:32:05 +01:00
|
|
|
label?: string;
|
2020-06-10 15:57:28 +01:00
|
|
|
isExplicit?: boolean;
|
2022-06-10 22:38:50 +01:00
|
|
|
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
|
|
|
}
|
2017-10-29 19:46:48 -06: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)}`;
|
2024-01-02 09:21:53 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(props: IProps) {
|
2020-06-08 16:48:32 +01:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2023-03-15 08:37:41 +00:00
|
|
|
value: this.getSettingValue(),
|
2020-06-18 14:32:43 +01:00
|
|
|
};
|
2020-06-08 16:48:32 +01:00
|
|
|
}
|
2017-10-29 19:46:48 -06:00
|
|
|
|
2023-03-15 08:37:41 +00: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-15 08:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public componentWillUnmount(): void {
|
|
|
|
|
defaultWatchManager.unwatchSetting(this.onSettingChange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getSettingValue(): boolean {
|
2026-06-04 14:50:17 +01:00
|
|
|
// 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.
|
2024-06-14 12:00:30 +01:00
|
|
|
if (
|
2026-06-04 14:50:17 +01:00
|
|
|
!SettingsStore.doesSettingSupportLevel(this.props.name, this.props.level) ||
|
2024-06-14 12:00:30 +01:00
|
|
|
SettingsStore.settingIsOveriddenAtConfigLevel(this.props.name, this.props.roomId ?? null, this.props.level)
|
|
|
|
|
) {
|
|
|
|
|
return !!SettingsStore.getValue(this.props.name);
|
|
|
|
|
}
|
2023-04-17 17:09:45 +01:00
|
|
|
return !!SettingsStore.getValueAt(
|
2023-03-15 08:37:41 +00:00
|
|
|
this.props.level,
|
|
|
|
|
this.props.name,
|
|
|
|
|
this.props.roomId ?? null,
|
|
|
|
|
this.props.isExplicit,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
private onSettingChange = (): void => {
|
|
|
|
|
this.setState({
|
|
|
|
|
value: this.getSettingValue(),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-04 15:46:21 +00:00
|
|
|
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
|
|
|
|
2023-01-12 13:25:14 +00: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,
|
2023-03-13 15:07:20 +00:00
|
|
|
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
|
|
|
};
|
2017-11-04 16:32:13 -06:00
|
|
|
|
2023-02-13 17:01:43 +00: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);
|
2017-10-29 19:46:48 -06:00
|
|
|
|
2024-01-11 09:49:00 +00:00
|
|
|
if (disabled && this.props.hideIfCannotSet) return null;
|
2022-06-10 22:38:50 +01:00
|
|
|
|
2023-08-22 16:32:05 +01:00
|
|
|
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);
|
2022-11-30 22:20:26 +01:00
|
|
|
const shouldWarn = SettingsStore.shouldHaveWarning(this.props.name);
|
2025-12-04 15:46:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|