Files
ThreadNet-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.

146 lines
5.1 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
2024-09-09 14:57:16 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import React from "react";
import { randomString } from "matrix-js-sdk/src/randomstring";
2021-10-22 17:23:32 -05:00
import SettingsStore from "../../../settings/SettingsStore";
import { _t } from "../../../languageHandler";
import ToggleSwitch from "./ToggleSwitch";
2020-06-08 16:48:32 +01:00
import StyledCheckbox from "./StyledCheckbox";
2020-07-29 11:03:43 -06:00
import { SettingLevel } from "../../../settings/SettingLevel";
import { 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
2020-06-10 15:57:28 +01:00
name: string;
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;
2020-06-08 16:48:32 +01:00
// XXX: once design replaces all toggles make this the default
2020-06-10 15:57:28 +01:00
useCheckbox?: boolean;
hideIfCannotSet?: boolean;
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> {
private readonly id = `mx_SettingsFlag_${randomString(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);
}
public componentWillUnmount(): void {
defaultWatchManager.unwatchSetting(this.onSettingChange);
}
private getSettingValue(): boolean {
// If a level defined in props is overridden by a level at a high presedence, it gets disabled
// and we should show the overridding value.
if (
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 (checked: boolean): Promise<void> => {
2020-07-29 11:03:43 -06:00
await this.save(checked);
2019-10-09 22:33:14 +01:00
this.setState({ value: checked });
this.props.onChange?.(checked);
2020-06-18 14:32:43 +01:00
};
private checkBoxOnChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
2020-06-08 16:48:32 +01:00
this.onChange(e.target.checked);
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);
2020-06-08 16:48:32 +01:00
if (this.props.useCheckbox) {
2020-06-24 16:06:50 +01:00
return (
<StyledCheckbox checked={this.state.value} onChange={this.checkBoxOnChange} disabled={disabled}>
{label}
2020-06-08 16:48:32 +01:00
</StyledCheckbox>
);
} else {
return (
<div className="mx_SettingsFlag">
<label className="mx_SettingsFlag_label" htmlFor={this.id}>
<span className="mx_SettingsFlag_labelText">{label}</span>
2022-01-04 10:57:29 +01:00
{description && (
<div className="mx_SettingsFlag_microcopy">
{shouldWarn
? _t(
"settings|warning",
{},
{
w: (sub) => (
<span className="mx_SettingsTab_microcopy_warning">{sub}</span>
),
description,
},
)
: description}
2022-01-04 10:57:29 +01:00
</div>
)}
</label>
2020-06-24 16:06:50 +01:00
<ToggleSwitch
id={this.id}
2020-06-24 16:06:50 +01:00
checked={this.state.value}
onChange={this.onChange}
disabled={disabled}
tooltip={disabled ? SettingsStore.disabledMessage(this.props.name) : undefined}
title={label ?? undefined}
2020-06-24 16:06:50 +01:00
/>
2020-06-08 16:48:32 +01:00
</div>
);
}
}
}