session-desktop/ts/components/session/settings/SessionSettings.tsx

642 lines
19 KiB
TypeScript
Raw Normal View History

2020-01-14 03:28:31 +01:00
import React from 'react';
import { SettingsHeader } from './SessionSettingsHeader';
import { SessionSettingListItem } from './SessionSettingListItem';
2020-01-21 02:58:49 +01:00
import {
SessionButton,
SessionButtonColor,
SessionButtonType,
} from '../SessionButton';
import { BlockedNumberController, PasswordUtil } from '../../../util';
import { ToastUtils } from '../../../session/utils';
import { ConversationLookupType } from '../../../state/ducks/conversations';
import { StateType } from '../../../state/reducer';
import { ConversationController } from '../../../session/conversations';
import {
getConversationLookup,
getConversations,
} from '../../../state/selectors/conversations';
2021-01-20 06:58:59 +01:00
import { connect } from 'react-redux';
2021-02-15 05:16:38 +01:00
import { getPasswordHash } from '../../../../ts/data/data';
2020-01-14 07:42:04 +01:00
2020-01-14 03:28:31 +01:00
export enum SessionSettingCategory {
2020-02-04 01:55:01 +01:00
Appearance = 'appearance',
2020-01-14 03:28:31 +01:00
Account = 'account',
Privacy = 'privacy',
2020-01-15 02:26:14 +01:00
Permissions = 'permissions',
2020-01-14 03:28:31 +01:00
Notifications = 'notifications',
2020-01-24 07:10:42 +01:00
Blocked = 'blocked',
2020-01-14 03:28:31 +01:00
}
export enum SessionSettingType {
Toggle = 'toggle',
Options = 'options',
Button = 'button',
2020-01-14 07:42:04 +01:00
Slider = 'slider',
2020-01-14 03:28:31 +01:00
}
export interface SettingsViewProps {
category: SessionSettingCategory;
// pass the conversation as props, so our render is called everytime they change.
// we have to do this to make the list refresh on unblock()
conversations?: ConversationLookupType;
2020-01-14 03:28:31 +01:00
}
interface State {
hasPassword: boolean | null;
2020-01-21 02:58:49 +01:00
pwdLockError: string | null;
2020-02-27 23:49:03 +01:00
mediaSetting: boolean | null;
2020-01-21 03:35:47 +01:00
shouldLockSettings: boolean | null;
}
interface LocalSettingType {
category: SessionSettingCategory;
description: string | undefined;
comparisonValue: string | undefined;
id: any;
2020-02-27 23:49:03 +01:00
value?: any;
2020-01-21 03:35:47 +01:00
content: any | undefined;
hidden: any;
title?: string;
2020-01-21 03:35:47 +01:00
type: SessionSettingType | undefined;
setFn: any;
onClick: any;
2020-02-03 04:19:37 +01:00
confirmationDialogParams: any | undefined;
}
class SettingsViewInner extends React.Component<SettingsViewProps, State> {
2020-01-14 03:28:31 +01:00
public settingsViewRef: React.RefObject<HTMLDivElement>;
public constructor(props: any) {
super(props);
this.state = {
hasPassword: null,
2020-01-21 02:58:49 +01:00
pwdLockError: null,
2020-02-27 23:49:03 +01:00
mediaSetting: null,
2020-01-21 02:58:49 +01:00
shouldLockSettings: true,
2020-01-20 04:36:46 +01:00
};
2020-01-14 03:28:31 +01:00
this.settingsViewRef = React.createRef();
this.onPasswordUpdated = this.onPasswordUpdated.bind(this);
2020-01-21 02:58:49 +01:00
this.validatePasswordLock = this.validatePasswordLock.bind(this);
void this.hasPassword();
2020-01-29 23:34:24 +01:00
this.onKeyUp = this.onKeyUp.bind(this);
window.addEventListener('keyup', this.onKeyUp);
2020-01-21 03:35:47 +01:00
}
2020-02-27 23:49:03 +01:00
public async componentWillMount() {
2020-04-06 07:34:35 +02:00
const mediaSetting = await window.getSettingValue('media-permissions');
2020-03-13 01:10:27 +01:00
this.setState({ mediaSetting });
2020-02-27 23:49:03 +01:00
}
2020-01-21 03:35:47 +01:00
public componentDidMount() {
setTimeout(() => ($('#password-lock-input') as any).focus(), 100);
2020-01-14 03:28:31 +01:00
}
2020-01-20 04:36:46 +01:00
/* tslint:disable-next-line:max-func-body-length */
2020-02-27 23:49:03 +01:00
public renderSettingInCategory() {
2020-01-21 03:35:47 +01:00
const { category } = this.props;
let settings: Array<LocalSettingType>;
2021-01-20 06:58:59 +01:00
if (category === SessionSettingCategory.Blocked) {
2020-07-30 16:49:11 +02:00
// special case for blocked user
settings = this.getBlockedUserSettings();
2020-01-21 03:35:47 +01:00
} else {
// Grab initial values from database on startup
// ID corresponds to installGetter parameters in preload.js
// They are NOT arbitrary; add with caution
settings = this.getLocalSettings();
}
return (
<>
{this.state.hasPassword !== null &&
settings.map(setting => {
const content = setting.content || undefined;
const shouldRenderSettings = setting.category === category;
const description = setting.description || '';
const comparisonValue = setting.comparisonValue || null;
2020-03-30 06:42:52 +02:00
const storedSetting = window.getSettingValue(
setting.id,
comparisonValue
);
const value =
storedSetting !== undefined
? storedSetting
: setting.content && setting.content.defaultValue;
2020-01-21 03:35:47 +01:00
const sliderFn =
setting.type === SessionSettingType.Slider
? (settingValue: any) =>
window.setSettingValue(setting.id, settingValue)
: () => null;
const onClickFn =
setting.onClick ||
((settingValue?: string) => {
this.updateSetting(setting, settingValue);
2020-01-21 03:35:47 +01:00
});
return (
<div key={setting.id}>
2020-05-29 08:25:15 +02:00
{shouldRenderSettings && !setting.hidden && (
<SessionSettingListItem
title={setting.title}
description={description}
type={setting.type}
value={value}
onClick={onClickFn}
onSliderChange={sliderFn}
content={content}
confirmationDialogParams={setting.confirmationDialogParams}
/>
)}
2020-01-21 03:35:47 +01:00
</div>
);
})}
</>
);
}
2020-01-21 03:39:34 +01:00
2020-01-21 03:35:47 +01:00
public renderPasswordLock() {
return (
<div className="session-settings__password-lock">
<div className="session-settings__password-lock-box">
<h3>{window.i18n('password')}</h3>
<input
type="password"
id="password-lock-input"
defaultValue=""
placeholder="Password"
2020-01-21 03:35:47 +01:00
/>
2020-01-21 04:59:18 +01:00
<div className="spacer-xs" />
2020-01-21 03:35:47 +01:00
{this.state.pwdLockError && (
<>
<div className="session-label warning">
{this.state.pwdLockError}
</div>
2020-01-21 04:59:18 +01:00
<div className="spacer-lg" />
2020-01-21 03:35:47 +01:00
</>
)}
<SessionButton
buttonType={SessionButtonType.BrandOutline}
buttonColor={SessionButtonColor.Green}
2020-08-17 03:21:57 +02:00
text={window.i18n('ok')}
2020-01-21 03:35:47 +01:00
onClick={this.validatePasswordLock}
/>
</div>
</div>
);
}
public async validatePasswordLock() {
const enteredPassword = String(jQuery('#password-lock-input').val());
2020-01-21 03:35:47 +01:00
if (!enteredPassword) {
this.setState({
pwdLockError: window.i18n('noGivenPassword'),
});
2020-01-21 04:59:18 +01:00
2020-01-21 03:35:47 +01:00
return false;
}
// Check if the password matches the hash we have stored
const hash = await getPasswordHash();
if (hash && !PasswordUtil.matchesHash(enteredPassword, hash)) {
2020-01-21 03:35:47 +01:00
this.setState({
pwdLockError: window.i18n('invalidPassword'),
});
return false;
}
// Unlocked settings
this.setState({
shouldLockSettings: false,
pwdLockError: null,
});
return true;
}
public render() {
2021-01-20 06:58:59 +01:00
const { category } = this.props;
2020-01-21 03:35:47 +01:00
const shouldRenderPasswordLock =
this.state.shouldLockSettings && this.state.hasPassword;
return (
<div className="session-settings">
2020-01-22 04:28:29 +01:00
<SettingsHeader
category={category}
categoryTitle={window.i18n(`${category}SettingsTitle`)}
2020-01-22 04:28:29 +01:00
/>
2020-02-19 10:47:37 +01:00
2020-01-22 05:57:58 +01:00
<div className="session-settings-view">
{shouldRenderPasswordLock ? (
this.renderPasswordLock()
) : (
<div ref={this.settingsViewRef} className="session-settings-list">
{this.renderSettingInCategory()}
</div>
)}
{this.renderSessionInfo()}
</div>
</div>
);
}
public renderSessionInfo(): JSX.Element {
return (
<div className="session-settings__version-info">
<span className="text-selectable">v{window.versionInfo.version}</span>
<span className="text-selectable">{window.versionInfo.commitHash}</span>
2020-01-21 03:35:47 +01:00
</div>
);
}
public setOptionsSetting(settingID: string, selectedValue: string) {
2020-01-21 03:35:47 +01:00
window.setSettingValue(settingID, selectedValue);
}
public async hasPassword() {
const hash = await getPasswordHash();
2020-01-21 03:35:47 +01:00
this.setState({
hasPassword: !!hash,
2020-01-21 03:35:47 +01:00
});
}
public updateSetting(item: any, value?: string) {
2020-01-21 03:35:47 +01:00
// If there's a custom afterClick function,
// execute it instead of automatically updating settings
2020-02-03 04:19:37 +01:00
2020-01-21 03:35:47 +01:00
if (item.setFn) {
if (value) {
item.setFn(value);
} else {
item.setFn();
}
2020-01-21 03:35:47 +01:00
} else {
if (item.type === SessionSettingType.Toggle) {
// If no custom afterClick function given, alter values in storage here
2020-02-03 04:19:37 +01:00
2020-01-21 03:35:47 +01:00
// Switch to opposite state
const newValue = !window.getSettingValue(item.id);
window.setSettingValue(item.id, newValue);
}
}
}
public onPasswordUpdated(action: string) {
if (action === 'set' || action === 'change') {
this.setState({
hasPassword: true,
shouldLockSettings: true,
pwdLockError: null,
});
}
if (action === 'remove') {
this.setState({
hasPassword: false,
});
}
}
// tslint:disable-next-line: max-func-body-length
private getLocalSettings(): Array<LocalSettingType> {
2020-01-14 05:37:52 +01:00
const { Settings } = window.Signal.Types;
2020-01-14 07:42:04 +01:00
2020-01-21 03:35:47 +01:00
return [
2020-01-15 03:49:07 +01:00
{
id: 'hide-menu-bar',
title: window.i18n('hideMenuBarTitle'),
description: window.i18n('hideMenuBarDescription'),
hidden: !Settings.isHideMenuBarSupported(),
type: SessionSettingType.Toggle,
2020-02-04 01:55:01 +01:00
category: SessionSettingCategory.Appearance,
2020-01-15 03:49:07 +01:00
setFn: window.toggleMenuBar,
content: { defaultValue: true },
2020-01-21 03:35:47 +01:00
comparisonValue: undefined,
onClick: undefined,
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
2020-01-15 03:49:07 +01:00
},
{
id: 'spell-check',
title: window.i18n('spellCheckTitle'),
description: window.i18n('spellCheckDescription'),
hidden: false,
type: SessionSettingType.Toggle,
2020-02-04 01:55:01 +01:00
category: SessionSettingCategory.Appearance,
2020-01-15 03:49:07 +01:00
setFn: window.toggleSpellCheck,
2020-03-30 03:38:26 +02:00
content: { defaultValue: true },
2020-01-21 03:35:47 +01:00
comparisonValue: undefined,
onClick: undefined,
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
2020-01-15 03:49:07 +01:00
},
{
id: 'link-preview-setting',
title: window.i18n('linkPreviewsTitle'),
description: window.i18n('linkPreviewDescription'),
hidden: false,
type: SessionSettingType.Toggle,
2020-02-04 01:55:01 +01:00
category: SessionSettingCategory.Appearance,
2020-01-15 03:49:07 +01:00
setFn: window.toggleLinkPreview,
2020-01-21 03:35:47 +01:00
content: undefined,
comparisonValue: undefined,
onClick: undefined,
2020-02-03 04:19:37 +01:00
confirmationDialogParams: {
shouldShowConfirm: () =>
!window.getSettingValue('link-preview-setting'),
title: window.i18n('linkPreviewsTitle'),
2020-02-03 04:19:37 +01:00
message: window.i18n('linkPreviewsConfirmMessage'),
okTheme: 'danger',
},
2020-01-15 03:49:07 +01:00
},
{
id: 'notification-setting',
title: window.i18n('notificationSettingsDialog'),
type: SessionSettingType.Options,
category: SessionSettingCategory.Notifications,
2020-01-21 03:35:47 +01:00
comparisonValue: undefined,
description: undefined,
hidden: undefined,
onClick: undefined,
setFn: (selectedValue: string) => {
this.setOptionsSetting('notification-setting', selectedValue);
},
2020-01-15 03:49:07 +01:00
content: {
options: {
group: 'notification-setting',
2020-01-21 02:58:49 +01:00
initalItem:
window.getSettingValue('notification-setting') || 'message',
2020-01-15 03:49:07 +01:00
items: [
{
label: window.i18n('nameAndMessage'),
value: 'message',
},
{
label: window.i18n('nameOnly'),
value: 'name',
},
{
label: window.i18n('noNameOrMessage'),
value: 'count',
},
{
label: window.i18n('disableNotifications'),
value: 'off',
},
],
},
2020-01-14 07:42:04 +01:00
},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
2020-01-15 02:26:14 +01:00
},
2020-01-16 02:08:31 +01:00
{
2020-01-15 03:49:07 +01:00
id: 'media-permissions',
title: window.i18n('mediaPermissionsTitle'),
description: window.i18n('mediaPermissionsDescription'),
2020-01-30 00:55:28 +01:00
hidden: false,
2020-01-15 03:49:07 +01:00
type: SessionSettingType.Toggle,
category: SessionSettingCategory.Permissions,
setFn: window.toggleMediaPermissions,
2020-01-21 03:35:47 +01:00
content: undefined,
comparisonValue: undefined,
onClick: undefined,
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
2020-01-16 02:08:31 +01:00
},
2020-02-19 14:08:47 +01:00
{
id: 'zoom-factor-setting',
title: window.i18n('zoomFactorSettingTitle'),
2020-02-21 00:53:57 +01:00
description: undefined,
2020-02-19 14:08:47 +01:00
hidden: false,
type: SessionSettingType.Slider,
category: SessionSettingCategory.Appearance,
setFn: undefined,
comparisonValue: undefined,
onClick: undefined,
content: {
dotsEnabled: true,
step: 20,
min: 60,
max: 200,
defaultValue: 100,
2020-03-02 01:35:42 +01:00
info: (value: number) => `${value}%`,
},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
2020-01-16 02:08:31 +01:00
},
2020-02-27 23:49:03 +01:00
{
id: 'media-permissions',
title: window.i18n('mediaPermissionsTitle'),
description: window.i18n('mediaPermissionsDescription'),
hidden: false,
type: SessionSettingType.Toggle,
category: SessionSettingCategory.Privacy,
setFn: window.toggleMediaPermissions,
content: undefined,
comparisonValue: undefined,
onClick: undefined,
confirmationDialogParams: undefined,
},
{
id: 'read-receipt-setting',
title: window.i18n('readReceiptSettingTitle'),
description: window.i18n('readReceiptSettingDescription'),
hidden: false,
type: SessionSettingType.Toggle,
category: SessionSettingCategory.Privacy,
setFn: undefined,
comparisonValue: undefined,
onClick: undefined,
content: {},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
},
{
id: 'typing-indicators-setting',
title: window.i18n('typingIndicatorsSettingTitle'),
description: window.i18n('typingIndicatorsSettingDescription'),
hidden: false,
type: SessionSettingType.Toggle,
category: SessionSettingCategory.Privacy,
setFn: undefined,
comparisonValue: undefined,
onClick: undefined,
content: {},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
},
{
id: 'auto-update',
title: window.i18n('autoUpdateSettingTitle'),
description: window.i18n('autoUpdateSettingDescription'),
hidden: false,
type: SessionSettingType.Toggle,
category: SessionSettingCategory.Privacy,
setFn: undefined,
comparisonValue: undefined,
onClick: undefined,
content: {},
confirmationDialogParams: undefined,
},
{
id: 'set-password',
title: window.i18n('setAccountPasswordTitle'),
description: window.i18n('setAccountPasswordDescription'),
hidden: this.state.hasPassword,
type: SessionSettingType.Button,
category: SessionSettingCategory.Privacy,
setFn: undefined,
2020-01-21 03:35:47 +01:00
comparisonValue: undefined,
content: {
buttonText: window.i18n('setPassword'),
buttonColor: SessionButtonColor.Primary,
},
2020-11-19 03:49:53 +01:00
onClick: () => {
window.Whisper.events.trigger('showPasswordDialog', {
2020-01-20 04:36:46 +01:00
action: 'set',
onSuccess: this.onPasswordUpdated,
2020-11-19 03:49:53 +01:00
});
},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
},
{
id: 'change-password',
title: window.i18n('changeAccountPasswordTitle'),
description: window.i18n('changeAccountPasswordDescription'),
hidden: !this.state.hasPassword,
type: SessionSettingType.Button,
category: SessionSettingCategory.Privacy,
setFn: undefined,
2020-01-21 03:35:47 +01:00
comparisonValue: undefined,
content: {
2020-01-20 04:36:46 +01:00
buttonText: window.i18n('changePassword'),
buttonColor: SessionButtonColor.Primary,
},
2020-11-19 03:49:53 +01:00
onClick: () => {
window.Whisper.events.trigger('showPasswordDialog', {
action: 'change',
onSuccess: this.onPasswordUpdated,
});
2020-11-19 03:49:53 +01:00
},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
},
{
id: 'remove-password',
title: window.i18n('removeAccountPasswordTitle'),
description: window.i18n('removeAccountPasswordDescription'),
hidden: !this.state.hasPassword,
type: SessionSettingType.Button,
category: SessionSettingCategory.Privacy,
setFn: undefined,
2020-01-21 03:35:47 +01:00
comparisonValue: undefined,
content: {
buttonText: window.i18n('removePassword'),
buttonColor: SessionButtonColor.Danger,
},
2020-11-19 03:49:53 +01:00
onClick: () => {
window.Whisper.events.trigger('showPasswordDialog', {
2020-01-20 04:36:46 +01:00
action: 'remove',
onSuccess: this.onPasswordUpdated,
2020-11-19 03:49:53 +01:00
});
},
2020-02-03 04:19:37 +01:00
confirmationDialogParams: undefined,
},
2020-01-14 07:42:04 +01:00
];
2020-01-14 03:28:31 +01:00
}
2020-01-21 03:17:38 +01:00
2020-07-30 16:49:11 +02:00
private getBlockedUserSettings(): Array<LocalSettingType> {
const results: Array<LocalSettingType> = [];
const blockedNumbers = BlockedNumberController.getBlockedNumbers();
for (const blockedNumber of blockedNumbers) {
let title: string;
const currentModel = ConversationController.getInstance().get(
blockedNumber
);
2020-08-15 13:21:35 +02:00
if (currentModel) {
title =
2020-08-15 13:58:24 +02:00
currentModel.getProfileName() ||
currentModel.getName() ||
window.i18n('anonymous');
2020-08-15 13:21:35 +02:00
} else {
title = window.i18n('anonymous');
}
2020-08-15 13:58:24 +02:00
results.push({
id: blockedNumber,
title,
description: '',
type: SessionSettingType.Button,
category: SessionSettingCategory.Blocked,
content: {
buttonColor: SessionButtonColor.Danger,
buttonText: window.i18n('unblockUser'),
},
comparisonValue: undefined,
2020-08-06 10:30:22 +02:00
setFn: async () => {
2020-08-15 13:21:35 +02:00
if (currentModel) {
await currentModel.unblock();
} else {
2020-08-15 13:58:24 +02:00
await BlockedNumberController.unblock(blockedNumber);
this.forceUpdate();
2020-08-15 13:21:35 +02:00
}
ToastUtils.pushToastSuccess('unblocked', window.i18n('unblocked'));
},
hidden: false,
onClick: undefined,
confirmationDialogParams: undefined,
});
}
if (blockedNumbers.length === 0) {
return [
{
id: 'noBlockedContacts',
title: '',
description: window.i18n('noBlockedContacts'),
type: undefined,
category: SessionSettingCategory.Blocked,
content: undefined,
comparisonValue: undefined,
setFn: undefined,
hidden: false,
onClick: undefined,
confirmationDialogParams: undefined,
},
];
}
2020-07-30 16:49:11 +02:00
return results;
}
2020-01-29 23:34:24 +01:00
private async onKeyUp(event: any) {
2020-07-29 03:27:20 +02:00
const lockPasswordFocussed = ($('#password-lock-input') as any).is(
':focus'
);
2020-01-29 23:34:24 +01:00
if (event.key === 'Enter' && lockPasswordFocussed) {
await this.validatePasswordLock();
}
event.preventDefault();
}
2020-01-21 03:39:34 +01:00
}
const mapStateToProps = (state: StateType) => {
return {
conversations: getConversationLookup(state),
};
};
const smart = connect(mapStateToProps);
export const SmartSettingsView = smart(SettingsViewInner);