/* @flow */ import React from 'react'; import Flux from '../../lib/flux'; import lodash from 'lodash'; import i18n from '../../i18n'; import TTS from '../../lib/TTS'; import * as SoundUtils from '../../utils/SoundUtils'; import type {Sound} from '../../utils/SoundUtils'; import {FormSection, FormTitle, FormTitleTags, FormItem, FormDivider, FormText, FormTextTypes} from '../../uikit/form'; import Flex from '../../uikit/Flex'; import Switch from '../../uikit/Switch'; import Select from '../../uikit/Select'; import SwitchItem from '../../uikit/SwitchItem'; import RadioGroup from '../../uikit/RadioGroup'; import NotificationActionCreators from '../../actions/NotificationActionCreators'; import UserSettingsActionCreators from '../../actions/UserSettingsActionCreators'; import NotificationStore from '../../stores/NotificationStore'; import UserSettingsStore from '../../stores/UserSettingsStore'; import {DesktopNotificationTypes, TTSNotificationTypes} from '../../Constants'; import '../../styles/user_settings_notifications.styl'; class UserSettingsNotifications extends React.PureComponent { props: { disabledSounds: Array, desktopType: string, ttsType: string, afkTimeout: number, }; constructor(props) { super(props); lodash.bindAll(this, [ 'handlePreviewSound', 'handleSoundChange', 'renderSounds', 'renderTTS', 'handleAFKTimeoutChange', ]); } _currentPlayingSound: ?Sound; handlePreviewSound(sound: string, e: Event) { e.stopPropagation(); if (this._currentPlayingSound != null) { this._currentPlayingSound.stop(); } if (e.target.type == null) { this._currentPlayingSound = SoundUtils.playSound(sound); } } handleSoundChange(sound: string, e: Event) { const disabledSounds = this.props.disabledSounds.filter(s => s !== sound); if (!e.currentTarget.checked) { disabledSounds.push(sound); } NotificationActionCreators.setDisabledSounds(disabledSounds); } handleDesktopChange(e: Event) { const desktopType = e.currentTarget.checked ? DesktopNotificationTypes.ALL : DesktopNotificationTypes.NEVER; if (desktopType !== DesktopNotificationTypes.NEVER) { NotificationActionCreators.requestPermission('UserSettingsModal'); } else { NotificationActionCreators.setDesktopType(desktopType); } } handleTTSChange(option: {value: string}) { const ttsType = option.value; NotificationActionCreators.setTTSType(ttsType); } handleAFKTimeoutChange(option: {value: number}) { const afkTimeout = option.value; UserSettingsActionCreators.updateRemoteSettings({afkTimeout}); } renderSounds() { const {disabledSounds} = this.props; return [ {label: i18n.Messages.SOUND_MESSAGE, sound: 'message1'}, {label: i18n.Messages.SOUND_DEAFEN, sound: 'deafen'}, {label: i18n.Messages.SOUND_UNDEAFEN, sound: 'undeafen'}, {label: i18n.Messages.MUTE, sound: 'mute'}, {label: i18n.Messages.UNMUTE, sound: 'unmute'}, {label: i18n.Messages.SOUND_VOICE_DISCONNECTED, sound: 'disconnect'}, {label: i18n.Messages.SOUND_PTT_ACTIVATE, sound: 'ptt_start'}, {label: i18n.Messages.SOUND_PTT_DEACTIVATE, sound: 'ptt_stop'}, {label: i18n.Messages.SOUND_USER_JOIN, sound: 'user_join'}, {label: i18n.Messages.SOUND_USER_LEAVE, sound: 'user_leave'}, {label: i18n.Messages.SOUND_OUTGOING_RING, sound: 'call_calling'}, {label: i18n.Messages.SOUND_INCOMING_RING, sound: 'call_ringing'}, ].map(({label, sound}) => {label} ); } renderTTS() { if (!TTS.supported) { return null; } const TTSOptions = [ { name: i18n.Messages.TTS_ALLS, value: TTSNotificationTypes.ALL_CHANNELS, }, { name: i18n.Messages.TTS_CURRENT, value: TTSNotificationTypes.SELECTED_CHANNEL, }, { name: i18n.Messages.TTS_NEVER, value: TTSNotificationTypes.NEVER, }, ]; return ( {i18n.Messages.FORM_DESCRIPTION_TTS} ); } render() { const {desktopType, afkTimeout} = this.props; const afkTimeoutOptions = lodash .range(1, 11) .map(minutes => ({value: 60 * minutes, label: i18n.Messages.DURATION_MINUTES.format({minutes})})); return ( {i18n.Messages.DESKTOP_NOTIFICATIONS_ENABLE}