From d24da3323450f415e010a2a38eda1e8704511148 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 21 Jan 2020 13:17:38 +1100 Subject: [PATCH] Conflicts management --- .../session/settings/SessionSettings.tsx | 292 ++---------------- 1 file changed, 29 insertions(+), 263 deletions(-) diff --git a/ts/components/session/settings/SessionSettings.tsx b/ts/components/session/settings/SessionSettings.tsx index b568c6b21..08d9d3261 100644 --- a/ts/components/session/settings/SessionSettings.tsx +++ b/ts/components/session/settings/SessionSettings.tsx @@ -32,20 +32,6 @@ interface State { hasPassword: boolean | null; shouldLockSettings: boolean | null; pwdLockError: string | null; - linkedPubKeys: Array; -} - -interface LocalSettingType { - category: SessionSettingCategory; - description: string | undefined; - comparisonValue: string | undefined; - id: any; - content: any | undefined; - hidden: any; - title: string; - type: SessionSettingType | undefined; - setFn: any; - onClick: any; } export class SettingsView extends React.Component { @@ -58,7 +44,6 @@ export class SettingsView extends React.Component { hasPassword: null, pwdLockError: null, shouldLockSettings: true, - linkedPubKeys: new Array(), }; this.settingsViewRef = React.createRef(); @@ -66,159 +51,16 @@ export class SettingsView extends React.Component { this.validatePasswordLock = this.validatePasswordLock.bind(this); this.hasPassword(); - this.refreshLinkedDevice = this.refreshLinkedDevice.bind(this); - } - - public componentDidMount() { - window.Whisper.events.on('refreshLinkedDeviceList', async () => { - setTimeout(() => { - this.refreshLinkedDevice(); - }, 1000); - }); - this.refreshLinkedDevice(); - } - - public componentWillUnmount() { - window.Whisper.events.off('refreshLinkedDeviceList'); } /* tslint:disable-next-line:max-func-body-length */ - public renderSettingInCategory(): JSX.Element { - const { category } = this.props; - - let settings: Array; - - if (category === SessionSettingCategory.Devices) { - // special case for linked devices - settings = this.getLinkedDeviceSettings(); - } 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; - const value = - window.getSettingValue(setting.id, comparisonValue) || - (setting.content && setting.content.defaultValue); - - const sliderFn = - setting.type === SessionSettingType.Slider - ? (settingValue: any) => - window.setSettingValue(setting.id, settingValue) - : () => null; - - const onClickFn = - setting.onClick || - (() => { - this.updateSetting(setting); - }); - - return ( -
- {shouldRenderSettings && - !setting.hidden && ( - - )} -
- ); - })} - - ); - } - - public render() { - const { category } = this.props; - - return ( -
- -
- {this.renderSettingInCategory()} -
-
- ); - } - - public setOptionsSetting(settingID: string) { - const selectedValue = $(`#${settingID} .session-radio input:checked`).val(); - window.setSettingValue(settingID, selectedValue); - } - - public hasPassword() { - const hashPromise = window.Signal.Data.getPasswordHash(); - - hashPromise.then((hash: any) => { - this.setState({ - hasPassword: !!hash, - }); - }); - } - - public updateSetting(item: any) { - // If there's a custom afterClick function, - // execute it instead of automatically updating settings - if (item.setFn) { - item.setFn(); - } else { - if (item.type === SessionSettingType.Toggle) { - // If no custom afterClick function given, alter values in storage here - // Switch to opposite state - const newValue = !window.getSettingValue(item.id); - window.setSettingValue(item.id, newValue); - } - } - } - - public onPasswordUpdated(action: string) { - if (action === 'set') { - this.setState({ - hasPassword: true, - }); - } - - if (action === 'remove') { - this.setState({ - hasPassword: false, - }); - } - } - - private getPubkeyName(pubKey: string | null) { - if (!pubKey) { - return {}; - } - - const secretWords = window.mnemonic.pubkey_to_secret_words(pubKey); - const conv = window.ConversationController.get(pubKey); - const deviceAlias = conv ? conv.getNickname() : 'Unnamed Device'; - - return { deviceAlias, secretWords }; - } - - // tslint:disable-next-line: max-func-body-length - private getLocalSettings(): Array { + public renderSettingInCategory() { const { Settings } = window.Signal.Types; - return [ + // Grab initial values from database on startup + // ID corresponds to instalGetter parameters in preload.js + // They are NOT arbitrary; add with caution + const localSettings = [ { id: 'theme-setting', title: window.i18n('themeToggleTitle'), @@ -228,8 +70,7 @@ export class SettingsView extends React.Component { type: SessionSettingType.Toggle, category: SessionSettingCategory.General, setFn: window.toggleTheme, - content: undefined, - onClick: undefined, + content: {}, }, { id: 'hide-menu-bar', @@ -239,9 +80,7 @@ export class SettingsView extends React.Component { type: SessionSettingType.Toggle, category: SessionSettingCategory.General, setFn: window.toggleMenuBar, - content: undefined, - comparisonValue: undefined, - onClick: undefined, + content: {}, }, { id: 'spell-check', @@ -251,9 +90,7 @@ export class SettingsView extends React.Component { type: SessionSettingType.Toggle, category: SessionSettingCategory.General, setFn: window.toggleSpellCheck, - content: undefined, - comparisonValue: undefined, - onClick: undefined, + content: {}, }, { id: 'link-preview-setting', @@ -263,19 +100,13 @@ export class SettingsView extends React.Component { type: SessionSettingType.Toggle, category: SessionSettingCategory.General, setFn: window.toggleLinkPreview, - content: undefined, - comparisonValue: undefined, - onClick: undefined, + content: {}, }, { id: 'notification-setting', title: window.i18n('notificationSettingsDialog'), type: SessionSettingType.Options, category: SessionSettingCategory.Notifications, - comparisonValue: undefined, - description: undefined, - hidden: undefined, - onClick: undefined, setFn: () => { this.setOptionsSetting('notification-setting'); }, @@ -313,9 +144,7 @@ export class SettingsView extends React.Component { type: SessionSettingType.Toggle, category: SessionSettingCategory.Permissions, setFn: window.toggleMediaPermissions, - content: undefined, - comparisonValue: undefined, - onClick: undefined, + content: {}, }, { id: 'message-ttl', @@ -325,8 +154,6 @@ export class SettingsView extends React.Component { type: SessionSettingType.Slider, category: SessionSettingCategory.Privacy, setFn: undefined, - comparisonValue: undefined, - onClick: undefined, content: { defaultValue: 24, }, @@ -339,7 +166,6 @@ export class SettingsView extends React.Component { type: SessionSettingType.Button, category: SessionSettingCategory.Privacy, setFn: undefined, - comparisonValue: undefined, content: { buttonText: window.i18n('setPassword'), buttonColor: SessionButtonColor.Primary, @@ -358,7 +184,6 @@ export class SettingsView extends React.Component { type: SessionSettingType.Button, category: SessionSettingCategory.Privacy, setFn: undefined, - comparisonValue: undefined, content: { buttonText: window.i18n('changePassword'), buttonColor: SessionButtonColor.Primary, @@ -377,7 +202,6 @@ export class SettingsView extends React.Component { type: SessionSettingType.Button, category: SessionSettingCategory.Privacy, setFn: undefined, - comparisonValue: undefined, content: { buttonText: window.i18n('removePassword'), buttonColor: SessionButtonColor.Danger, @@ -389,8 +213,6 @@ export class SettingsView extends React.Component { }), }, ]; - } - return ( <> @@ -405,7 +227,19 @@ export class SettingsView extends React.Component { const value = window.getSettingValue(setting.id, comparisonValue) || setting.content.defaultValue; - + + const sliderFn = + setting.type === SessionSettingType.Slider + ? (settingValue: any) => + window.setSettingValue(setting.id, settingValue) + : () => null; + + const onClickFn = + setting.onClick || + (() => { + this.updateSetting(setting); + }); + return (
{shouldRenderSettings && @@ -426,7 +260,7 @@ export class SettingsView extends React.Component { ); } - + public renderPasswordLock() { return (
@@ -436,7 +270,7 @@ export class SettingsView extends React.Component { type="password" id="password-lock-input" defaultValue="" - placeholder=" " + placeholder={" "} /> {this.state.pwdLockError && ( @@ -466,7 +300,6 @@ export class SettingsView extends React.Component { this.setState({ pwdLockError: window.i18n('noGivenPassword'), }); - return false; } @@ -488,7 +321,7 @@ export class SettingsView extends React.Component { return true; } -public hasPassword() { + public hasPassword() { const hashPromise = window.Signal.Data.getPasswordHash(); hashPromise.then((hash: any) => { @@ -545,77 +378,10 @@ public hasPassword() { }); } - - private getLinkedDeviceSettings(): Array { - const { linkedPubKeys } = this.state; - - if (linkedPubKeys && linkedPubKeys.length > 0) { - return linkedPubKeys.map((pubkey: any) => { - const { deviceAlias, secretWords } = this.getPubkeyName(pubkey); - const description = `${secretWords} ${window.shortenPubkey(pubkey)}`; - - if (window.lokiFeatureFlags.multiDeviceUnpairing) { - return { - id: pubkey, - title: deviceAlias, - description: description, - type: SessionSettingType.Button, - category: SessionSettingCategory.Devices, - content: { - buttonColor: SessionButtonColor.Danger, - buttonText: window.i18n('unpairDevice'), - }, - comparisonValue: undefined, - setFn: () => { - window.Whisper.events.trigger('showDevicePairingDialog', { - pubKeyToUnpair: pubkey, - }); - }, - hidden: undefined, - onClick: undefined, - }; - } else { - return { - id: pubkey, - title: deviceAlias, - description: description, - type: undefined, - category: SessionSettingCategory.Devices, - content: {}, - comparisonValue: undefined, - setFn: undefined, - hidden: undefined, - onClick: undefined, - }; - } + if (action === 'remove') { + this.setState({ + hasPassword: false, }); - } else { - return [ - { - id: 'no-linked-device', - title: window.i18n('noPairedDevices'), - type: undefined, - description: '', - category: SessionSettingCategory.Devices, - content: {}, - comparisonValue: undefined, - onClick: undefined, - setFn: undefined, - hidden: undefined, - }, - ]; } } - - private refreshLinkedDevice() { - const ourPubKey = window.textsecure.storage.user.getNumber(); - - window.libloki.storage - .getSecondaryDevicesFor(ourPubKey) - .then((pubKeys: any) => { - this.setState({ - linkedPubKeys: pubKeys, - }); - }); - } }