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

298 lines
8.1 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 { CategoryConversations } from './section/CategoryConversations';
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 { Data } from '../../data/data';
2022-03-31 01:22:13 +02:00
import { matchesHash } from '../../util/passwordUtils';
import { SettingsCategoryPermissions } from './section/CategoryPermissions';
import { SettingsCategoryHelp } from './section/CategoryHelp';
import styled from 'styled-components';
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 {
Privacy = 'privacy',
Notifications = 'notifications',
Conversations = 'conversations',
MessageRequests = 'messageRequests',
Appearance = 'appearance',
Permissions = 'permissions',
Help = 'help',
RecoveryPhrase = 'recoveryPhrase',
ClearData = 'ClearData',
}
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;
}
const StyledVersionInfo = styled.div`
display: flex;
justify-content: space-between;
padding: var(--margins-sm) var(--margins-md);
background: none;
font-size: var(--font-size-xs);
`;
const StyledSpanSessionInfo = styled.span`
opacity: 0.4;
transition: var(--default-duration);
user-select: text;
:hover {
opacity: 1;
}
`;
2021-10-22 01:39:05 +02:00
const SessionInfo = () => {
const openOxenWebsite = () => {
void shell.openExternal('https://oxen.io/');
};
return (
<StyledVersionInfo>
<StyledSpanSessionInfo>v{window.versionInfo.version}</StyledSpanSessionInfo>
<StyledSpanSessionInfo>
<SessionIconButton iconSize="medium" iconType="oxen" onClick={openOxenWebsite} />
</StyledSpanSessionInfo>
<StyledSpanSessionInfo>{window.versionInfo.commitHash}</StyledSpanSessionInfo>
</StyledVersionInfo>
2021-10-22 01:39:05 +02:00
);
};
const StyledPasswordInput = styled.input`
width: 100%;
background: var(--color-input-background);
color: var(--color-text);
padding: var(--margins-xs) var(--margins-md);
margin-bottom: var(--margins-lg);
outline: none;
border: none;
border-radius: 2px;
text-align: center;
font-size: 24px;
letter-spacing: 5px;
font-family: var(--font-default);
`;
const StyledH3 = styled.h3`
padding: 0px;
margin-bottom: var(--margins-lg);
`;
const PasswordLock = ({
pwdLockError,
validatePasswordLock,
}: {
pwdLockError: string | null;
validatePasswordLock: () => Promise<boolean>;
}) => {
return (
<div className="session-settings__password-lock">
<div className="session-settings__password-lock-box">
<StyledH3>{window.i18n('password')}</StyledH3>
<StyledPasswordInput
type="password"
id="password-lock-input"
defaultValue=""
placeholder="Password"
data-testid="password-lock-input"
autoFocus={true}
/>
{pwdLockError && <div className="session-label warning">{pwdLockError}</div>}
<SessionButton
buttonType={SessionButtonType.BrandOutline}
buttonColor={SessionButtonColor.Green}
text={window.i18n('ok')}
onClick={validatePasswordLock}
/>
</div>
</div>
);
};
const SettingInCategory = (props: {
category: SessionSettingCategory;
hasPassword: boolean;
onPasswordUpdated: (action: string) => void;
}) => {
const { category, hasPassword, onPasswordUpdated } = props;
if (hasPassword === null) {
return null;
}
switch (category) {
// special case for blocked user
case SessionSettingCategory.Conversations:
return <CategoryConversations />;
case SessionSettingCategory.Appearance:
return <SettingsCategoryAppearance hasPassword={hasPassword} />;
case SessionSettingCategory.Notifications:
return <SessionNotificationGroupSettings hasPassword={hasPassword} />;
case SessionSettingCategory.Privacy:
return (
<SettingsCategoryPrivacy onPasswordUpdated={onPasswordUpdated} hasPassword={hasPassword} />
);
case SessionSettingCategory.Help:
return <SettingsCategoryHelp hasPassword={hasPassword} />;
case SessionSettingCategory.Permissions:
return <SettingsCategoryPermissions hasPassword={hasPassword} />;
// those three down there have no options, they are just a button
case SessionSettingCategory.ClearData:
case SessionSettingCategory.MessageRequests:
case SessionSettingCategory.RecoveryPhrase:
default:
return null;
}
};
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 Data.getPasswordHash().then(hash => {
this.setState({
hasPassword: !!hash,
});
});
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-01-14 03:28:31 +01:00
}
public componentWillUnmount() {
window.removeEventListener('keyup', this.onKeyUp);
}
2020-01-21 03:35:47 +01:00
public async validatePasswordLock() {
const enteredPassword = String(
(document.getElementById('password-lock-input') as HTMLInputElement)?.value
);
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 Data.getPasswordHash();
2022-03-31 01:22:13 +02:00
if (hash && !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;
2020-01-21 03:35:47 +01:00
return (
<div className="session-settings">
<SettingsHeader category={category} />
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">
<SettingInCategory
category={category}
onPasswordUpdated={this.onPasswordUpdated}
hasPassword={Boolean(this.state.hasPassword)}
/>
2020-01-22 05:57:58 +01:00
</div>
)}
<SessionInfo />
2020-01-22 05:57:58 +01:00
</div>
</div>
);
}
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) {
const lockPasswordVisible = Boolean(document.getElementById('password-lock-input'));
2020-01-29 23:34:24 +01:00
if (event.key === 'Enter' && lockPasswordVisible) {
2020-01-29 23:34:24 +01:00
await this.validatePasswordLock();
}
event.preventDefault();
}
2020-01-21 03:39:34 +01:00
}