import type { FC } from '../../../lib/teact/teact'; import React, { memo, useCallback, useEffect } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; import type { ApiPrivacySettings } from '../../../types'; import { SettingsScreens } from '../../../types'; import { selectCanSetPasscode, selectIsCurrentUserPremium } from '../../../global/selectors'; import useLang from '../../../hooks/useLang'; import useHistoryBack from '../../../hooks/useHistoryBack'; import ListItem from '../../ui/ListItem'; import Checkbox from '../../ui/Checkbox'; type OwnProps = { isActive?: boolean; onScreenSelect: (screen: SettingsScreens) => void; onReset: () => void; }; type StateProps = { isCurrentUserPremium?: boolean; hasPassword?: boolean; hasPasscode?: boolean; canSetPasscode?: boolean; blockedCount: number; webAuthCount: number; isSensitiveEnabled?: boolean; canChangeSensitive?: boolean; canDisplayAutoarchiveSetting: boolean; shouldArchiveAndMuteNewNonContact?: boolean; canDisplayChatInTitle?: boolean; privacyPhoneNumber?: ApiPrivacySettings; privacyLastSeen?: ApiPrivacySettings; privacyProfilePhoto?: ApiPrivacySettings; privacyForwarding?: ApiPrivacySettings; privacyVoiceMessages?: ApiPrivacySettings; privacyGroupChats?: ApiPrivacySettings; privacyPhoneCall?: ApiPrivacySettings; privacyPhoneP2P?: ApiPrivacySettings; }; const SettingsPrivacy: FC = ({ isActive, isCurrentUserPremium, hasPassword, hasPasscode, blockedCount, webAuthCount, isSensitiveEnabled, canChangeSensitive, canDisplayAutoarchiveSetting, shouldArchiveAndMuteNewNonContact, canDisplayChatInTitle, privacyPhoneNumber, privacyLastSeen, privacyProfilePhoto, privacyForwarding, privacyVoiceMessages, privacyGroupChats, privacyPhoneCall, privacyPhoneP2P, onScreenSelect, onReset, canSetPasscode, }) => { const { loadPrivacySettings, loadBlockedContacts, loadAuthorizations, loadContentSettings, updateContentSettings, loadGlobalPrivacySettings, updateGlobalPrivacySettings, loadWebAuthorizations, showNotification, setSettingOption, } = getActions(); useEffect(() => { loadBlockedContacts(); loadAuthorizations(); loadPrivacySettings(); loadContentSettings(); loadWebAuthorizations(); }, [loadBlockedContacts, loadAuthorizations, loadPrivacySettings, loadContentSettings, loadWebAuthorizations]); useEffect(() => { if (isActive) { loadGlobalPrivacySettings(); } }, [isActive, loadGlobalPrivacySettings]); const lang = useLang(); useHistoryBack({ isActive, onBack: onReset, }); const handleArchiveAndMuteChange = useCallback((isEnabled: boolean) => { updateGlobalPrivacySettings({ shouldArchiveAndMuteNewNonContact: isEnabled, }); }, [updateGlobalPrivacySettings]); const handleVoiceMessagesClick = useCallback(() => { if (isCurrentUserPremium) { onScreenSelect(SettingsScreens.PrivacyVoiceMessages); } else { showNotification({ message: lang('PrivacyVoiceMessagesPremiumOnly'), }); } }, [isCurrentUserPremium, lang, onScreenSelect, showNotification]); const handleChatInTitleChange = useCallback((isChecked: boolean) => { setSettingOption({ canDisplayChatInTitle: isChecked, }); }, []); const handleUpdateContentSettings = useCallback((isChecked: boolean) => { updateContentSettings(isChecked); }, [updateContentSettings]); function getVisibilityValue(setting?: ApiPrivacySettings) { const { visibility } = setting || {}; const blockCount = setting ? setting.blockChatIds.length + setting.blockUserIds.length : 0; const allowCount = setting ? setting.allowChatIds.length + setting.allowUserIds.length : 0; const total = []; if (blockCount) total.push(`-${blockCount}`); if (allowCount) total.push(`+${allowCount}`); const exceptionString = total.length ? `(${total.join(',')})` : ''; switch (visibility) { case 'everybody': return `${lang('P2PEverybody')} ${exceptionString}`; case 'contacts': return `${lang('P2PContacts')} ${exceptionString}`; case 'nobody': return `${lang('P2PNobody')} ${exceptionString}`; } return undefined; } return (
onScreenSelect(SettingsScreens.PrivacyBlockedUsers)} > {lang('BlockedUsers')} {blockedCount || ''} {canSetPasscode && ( onScreenSelect( hasPasscode ? SettingsScreens.PasscodeEnabled : SettingsScreens.PasscodeDisabled, )} >
{lang('Passcode')} {lang(hasPasscode ? 'PasswordOn' : 'PasswordOff')}
)} onScreenSelect( hasPassword ? SettingsScreens.TwoFaEnabled : SettingsScreens.TwoFaDisabled, )} >
{lang('TwoStepVerification')} {lang(hasPassword ? 'PasswordOn' : 'PasswordOff')}
{webAuthCount > 0 && ( onScreenSelect(SettingsScreens.ActiveWebsites)} > {lang('PrivacySettings.WebSessions')} {webAuthCount} )}

{lang('PrivacyTitle')}

onScreenSelect(SettingsScreens.PrivacyPhoneNumber)} >
{lang('PrivacyPhoneTitle')} {getVisibilityValue(privacyPhoneNumber)}
onScreenSelect(SettingsScreens.PrivacyLastSeen)} >
{lang('LastSeenTitle')} {getVisibilityValue(privacyLastSeen)}
onScreenSelect(SettingsScreens.PrivacyProfilePhoto)} >
{lang('PrivacyProfilePhotoTitle')} {getVisibilityValue(privacyProfilePhoto)}
onScreenSelect(SettingsScreens.PrivacyPhoneCall)} >
{lang('WhoCanCallMe')} {getVisibilityValue(privacyPhoneCall)}
onScreenSelect(SettingsScreens.PrivacyPhoneP2P)} >
{lang('PrivacyP2P')} {getVisibilityValue(privacyPhoneP2P)}
onScreenSelect(SettingsScreens.PrivacyForwarding)} >
{lang('PrivacyForwardsTitle')} {getVisibilityValue(privacyForwarding)}
} className="no-icon" onClick={handleVoiceMessagesClick} >
{lang('PrivacyVoiceMessagesTitle')} {getVisibilityValue(privacyVoiceMessages)}
onScreenSelect(SettingsScreens.PrivacyGroupChats)} >
{lang('WhoCanAddMe')} {getVisibilityValue(privacyGroupChats)}
{canDisplayAutoarchiveSetting && (

{lang('NewChatsFromNonContacts')}

)}

{lang('lng_settings_window_system')}

{canChangeSensitive && (

{lang('lng_settings_sensitive_title')}

)}
); }; export default memo(withGlobal( (global): StateProps => { const { settings: { byKey: { hasPassword, isSensitiveEnabled, canChangeSensitive, shouldArchiveAndMuteNewNonContact, canDisplayChatInTitle, }, privacy, }, blocked, passcode: { hasPasscode, }, appConfig, } = global; return { isCurrentUserPremium: selectIsCurrentUserPremium(global), hasPassword, hasPasscode: Boolean(hasPasscode), blockedCount: blocked.totalCount, webAuthCount: global.activeWebSessions.orderedHashes.length, isSensitiveEnabled, canDisplayAutoarchiveSetting: Boolean(appConfig?.canDisplayAutoarchiveSetting), shouldArchiveAndMuteNewNonContact, canChangeSensitive, privacyPhoneNumber: privacy.phoneNumber, privacyLastSeen: privacy.lastSeen, privacyProfilePhoto: privacy.profilePhoto, privacyForwarding: privacy.forwards, privacyVoiceMessages: privacy.voiceMessages, privacyGroupChats: privacy.chatInvite, privacyPhoneCall: privacy.phoneCall, privacyPhoneP2P: privacy.phoneP2P, canDisplayChatInTitle, canSetPasscode: selectCanSetPasscode(global), }; }, )(SettingsPrivacy));