import { ChangeEvent } from 'react'; import useRunDebounced from '../../../hooks/useRunDebounced'; import React, { FC, memo, useCallback, useEffect, } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; 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; 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, onReset, hasPrivateChatsNotifications, hasPrivateChatsMessagePreview, hasGroupNotifications, hasGroupMessagePreview, hasBroadcastNotifications, hasBroadcastMessagePreview, hasContactJoinedNotifications, hasPushNotifications, hasWebNotifications, notificationSoundVolume, }) => { const { loadNotificationSettings, updateContactSignUpNotification, updateNotificationSettings, updateWebNotificationSettings, } = getActions(); useEffect(() => { loadNotificationSettings(); }, [loadNotificationSettings]); const runDebounced = useRunDebounced(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 handleWebNotificationsChange = useCallback((e: ChangeEvent) => { updateWebNotificationSettings({ hasWebNotifications: e.target.checked, }); }, [updateWebNotificationSettings]); const handlePushNotificationsChange = useCallback((e: ChangeEvent) => { updateWebNotificationSettings({ hasPushNotifications: e.target.checked, }); }, [updateWebNotificationSettings]); const handlePrivateChatsNotificationsChange = useCallback((e: ChangeEvent) => { handleSettingsChange(e, 'contact', 'silent'); }, [handleSettingsChange]); const handlePrivateChatsPreviewChange = useCallback((e: ChangeEvent) => { handleSettingsChange(e, 'contact', 'showPreviews'); }, [handleSettingsChange]); const handleGroupsNotificationsChange = useCallback((e: ChangeEvent) => { handleSettingsChange(e, 'group', 'silent'); }, [handleSettingsChange]); const handleGroupsPreviewChange = useCallback((e: ChangeEvent) => { handleSettingsChange(e, 'group', 'showPreviews'); }, [handleSettingsChange]); const handleChannelsNotificationsChange = useCallback((e: ChangeEvent) => { handleSettingsChange(e, 'broadcast', 'silent'); }, [handleSettingsChange]); const handleChannelsPreviewChange = useCallback((e: ChangeEvent) => { handleSettingsChange(e, 'broadcast', 'showPreviews'); }, [handleSettingsChange]); const handleContactNotificationChange = useCallback((e: ChangeEvent) => { updateContactSignUpNotification({ isSilent: !e.target.checked, }); }, [updateContactSignUpNotification]); const handleVolumeChange = useCallback((volume: number) => { updateWebNotificationSettings({ notificationSoundVolume: volume, }); runDebounced(() => playNotifySound(undefined, volume)); }, [runDebounced, updateWebNotificationSettings]); const lang = useLang(); useHistoryBack({ isActive, onBack: onReset, }); return (

Web notifications

{lang('AutodownloadPrivateChats')}

{lang('FilterGroups')}

{lang('FilterChannels')}

{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));