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

247 lines
6.8 KiB
TypeScript
Raw Normal View History

2020-01-14 03:28:31 +01:00
import React from 'react';
import { SettingsHeader } from './SessionSettingsHeader';
2021-10-22 01:39:05 +02:00
import { shell } from 'electron';
import { SessionIconButton } from '../icon';
2021-10-20 05:18:57 +02:00
import autoBind from 'auto-bind';
import { SessionNotificationGroupSettings } from './SessionNotificationGroupSettings';
// tslint:disable-next-line: no-submodule-imports
import { BlockedUserSettings } from './BlockedUserSettings';
2021-10-22 01:39:05 +02:00
import { SettingsCategoryPrivacy } from './section/CategoryPrivacy';
import { SettingsCategoryAppearance } from './section/CategoryAppearance';
import { SessionButton, SessionButtonColor, SessionButtonType } from '../basic/SessionButton';
import { getPasswordHash } from '../../data/data';
import { LocalizerKeys } from '../../types/LocalizerKeys';
import { PasswordUtil } from '../../util';
export function getMediaPermissionsSettings() {
return window.getSettingValue('media-permissions');
2020-01-14 03:28:31 +01:00
}
export function getCallMediaPermissionsSettings() {
return window.getSettingValue('call-media-permissions');
2020-01-14 03:28:31 +01:00
}
export enum SessionSettingCategory {
Appearance = 'appearance',
Privacy = 'privacy',
Notifications = 'notifications',
Blocked = 'blocked',
}
2020-01-14 03:28:31 +01:00
export interface SettingsViewProps {
category: SessionSettingCategory;
}
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;
callMediaSetting: boolean | null;
2020-01-21 03:35:47 +01:00
shouldLockSettings: boolean | null;
}
2021-10-22 01:39:05 +02:00
const SessionInfo = () => {
const openOxenWebsite = () => {
void shell.openExternal('https://oxen.io/');
};
return (
<div className="session-settings__version-info">
<span className="text-selectable">v{window.versionInfo.version}</span>
<span>
<SessionIconButton iconSize="medium" iconType="oxen" onClick={openOxenWebsite} />
2021-10-22 01:39:05 +02:00
</span>
<span className="text-selectable">{window.versionInfo.commitHash}</span>
</div>
);
};
export const PasswordLock = ({
pwdLockError,
validatePasswordLock,
}: {
pwdLockError: string | null;
validatePasswordLock: () => Promise<boolean>;
}) => {
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" />
{pwdLockError && <div className="session-label warning">{pwdLockError}</div>}
<SessionButton
buttonType={SessionButtonType.BrandOutline}
buttonColor={SessionButtonColor.Green}
text={window.i18n('ok')}
onClick={validatePasswordLock}
/>
</div>
</div>
);
};
2021-10-22 01:39:05 +02:00
export class SessionSettingsView 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,
callMediaSetting: 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();
2021-10-20 05:18:57 +02:00
autoBind(this);
void this.hasPassword();
2020-01-21 03:35:47 +01:00
}
public componentDidMount() {
window.addEventListener('keyup', this.onKeyUp);
const mediaSetting = getMediaPermissionsSettings();
const callMediaSetting = getCallMediaPermissionsSettings();
this.setState({ mediaSetting, callMediaSetting });
2020-02-27 23:49:03 +01:00
setTimeout(() => ($('#password-lock-input') as any).focus(), 100);
2020-01-14 03:28:31 +01:00
}
public componentWillUnmount() {
window.removeEventListener('keyup', this.onKeyUp);
}
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() {
const { category } = this.props;
if (this.state.hasPassword === null) {
return null;
}
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
return <BlockedUserSettings />;
}
2020-01-21 03:35:47 +01:00
if (category === SessionSettingCategory.Appearance) {
return <SettingsCategoryAppearance hasPassword={this.state.hasPassword} />;
2020-01-21 03:35:47 +01:00
}
if (category === SessionSettingCategory.Notifications) {
return <SessionNotificationGroupSettings hasPassword={this.state.hasPassword} />;
}
if (category === SessionSettingCategory.Privacy) {
return (
<SettingsCategoryPrivacy
onPasswordUpdated={this.onPasswordUpdated}
hasPassword={this.state.hasPassword}
/>
);
}
2021-10-22 01:39:05 +02:00
return null;
2020-01-21 03:35:47 +01:00
}
2020-01-21 03:39:34 +01:00
2020-01-21 03:35:47 +01:00
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;
2021-04-22 10:03:58 +02:00
const shouldRenderPasswordLock = this.state.shouldLockSettings && this.state.hasPassword;
const categoryLocalized: LocalizerKeys =
category === SessionSettingCategory.Appearance
? 'appearanceSettingsTitle'
: category === SessionSettingCategory.Blocked
? 'blockedSettingsTitle'
: category === SessionSettingCategory.Notifications
? 'notificationsSettingsTitle'
: 'privacySettingsTitle';
2020-01-21 03:35:47 +01:00
return (
<div className="session-settings">
<SettingsHeader category={category} categoryTitle={window.i18n(categoryLocalized)} />
2020-02-19 10:47:37 +01:00
2020-01-22 05:57:58 +01:00
<div className="session-settings-view">
{shouldRenderPasswordLock ? (
<PasswordLock
pwdLockError={this.state.pwdLockError}
validatePasswordLock={this.validatePasswordLock}
/>
2020-01-22 05:57:58 +01:00
) : (
<div ref={this.settingsViewRef} className="session-settings-list">
{this.renderSettingInCategory()}
</div>
)}
<SessionInfo />
2020-01-22 05:57:58 +01:00
</div>
</div>
);
}
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 onPasswordUpdated(action: string) {
if (action === 'set' || action === 'change') {
this.setState({
hasPassword: true,
shouldLockSettings: true,
pwdLockError: null,
});
}
if (action === 'remove') {
this.setState({
hasPassword: false,
});
}
}
2020-01-29 23:34:24 +01:00
private async onKeyUp(event: any) {
2021-04-22 10:03:58 +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
}