import { ChangeEvent } from 'react'; import useDebounce from '../../../hooks/useDebounce'; import React, { FC, memo, useCallback, useEffect, } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../modules'; import { SettingsScreens } from '../../../types'; import useLang from '../../../hooks/useLang'; import useHistoryBack from '../../../hooks/useHistoryBack'; import { playNotifySound } from '../../../util/notifications'; import Checkbox from '../../ui/Checkbox'; import RangeSlider from '../../ui/RangeSlider'; type OwnProps = { isActive?: boolean; onScreenSelect: (screen: SettingsScreens) => void; onReset: () => void; }; type StateProps = { hasPrivateChatsNotifications: boolean; hasPrivateChatsMessagePreview: boolean; hasGroupNotifications: boolean; hasGroupMessagePreview: boolean; hasBroadcastNotifications: boolean; hasBroadcastMessagePreview: boolean; hasContactJoinedNotifications: boolean; hasWebNotifications: boolean; hasPushNotifications: boolean; notificationSoundVolume: number; }; const SettingsNotifications: FC = ({ isActive, onScreenSelect, onReset, hasPrivateChatsNotifications, hasPrivateChatsMessagePreview, hasGroupNotifications, hasGroupMessagePreview, hasBroadcastNotifications, hasBroadcastMessagePreview, hasContactJoinedNotifications, hasPushNotifications, hasWebNotifications, notificationSoundVolume, }) => { const { loadNotificationSettings, updateContactSignUpNotification, updateNotificationSettings, updateWebNotificationSettings, } = getActions(); useEffect(() => { loadNotificationSettings(); }, [loadNotificationSettings]); const runDebounced = useDebounce(500, true); const handleSettingsChange = useCallback(( e: ChangeEvent, peerType: 'contact' | 'group' | 'broadcast', setting: 'silent' | 'showPreviews', ) => { const currentIsSilent = peerType === 'contact' ? !hasPrivateChatsNotifications : !(peerType === 'group' ? hasGroupNotifications : hasBroadcastNotifications); const currentShouldShowPreviews = peerType === 'contact' ? hasPrivateChatsMessagePreview : (peerType === 'group' ? hasGroupMessagePreview : hasBroadcastMessagePreview); updateNotificationSettings({ peerType, ...(setting === 'silent' && { isSilent: !e.target.checked, shouldShowPreviews: currentShouldShowPreviews }), ...(setting === 'showPreviews' && { shouldShowPreviews: e.target.checked, isSilent: currentIsSilent }), }); }, [ hasBroadcastMessagePreview, hasBroadcastNotifications, hasGroupMessagePreview, hasGroupNotifications, hasPrivateChatsMessagePreview, hasPrivateChatsNotifications, updateNotificationSettings, ]); const handleContactNotificationChange = useCallback((e: ChangeEvent) => { updateContactSignUpNotification({ isSilent: !e.target.checked, }); }, [updateContactSignUpNotification]); const lang = useLang(); useHistoryBack(isActive, onReset, onScreenSelect, SettingsScreens.Notifications); return (

Web notifications

{ updateWebNotificationSettings({ hasWebNotifications: e.target.checked }); }} /> { updateWebNotificationSettings({ hasPushNotifications: e.target.checked }); }} />
{ updateWebNotificationSettings({ notificationSoundVolume: volume }); runDebounced(() => playNotifySound(undefined, volume)); }} />

{lang('AutodownloadPrivateChats')}

{ handleSettingsChange(e, 'contact', 'silent'); }} /> { handleSettingsChange(e, 'contact', 'showPreviews'); }} />

{lang('FilterGroups')}

{ handleSettingsChange(e, 'group', 'silent'); }} /> { handleSettingsChange(e, 'group', 'showPreviews'); }} />

{lang('FilterChannels')}

{ handleSettingsChange(e, 'broadcast', 'silent'); }} /> { handleSettingsChange(e, 'broadcast', 'showPreviews'); }} />

{lang('PhoneOther')}

); }; export default memo(withGlobal( (global): StateProps => { return { hasPrivateChatsNotifications: Boolean(global.settings.byKey.hasPrivateChatsNotifications), hasPrivateChatsMessagePreview: Boolean(global.settings.byKey.hasPrivateChatsMessagePreview), hasGroupNotifications: Boolean(global.settings.byKey.hasGroupNotifications), hasGroupMessagePreview: Boolean(global.settings.byKey.hasGroupMessagePreview), hasBroadcastNotifications: Boolean(global.settings.byKey.hasBroadcastNotifications), hasBroadcastMessagePreview: Boolean(global.settings.byKey.hasBroadcastMessagePreview), hasContactJoinedNotifications: Boolean(global.settings.byKey.hasContactJoinedNotifications), hasWebNotifications: global.settings.byKey.hasWebNotifications, hasPushNotifications: global.settings.byKey.hasPushNotifications, notificationSoundVolume: global.settings.byKey.notificationSoundVolume, }; }, )(SettingsNotifications));