import type { FC } from '../../../lib/teact/teact'; import React, { memo, useCallback, useEffect } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; import type { GlobalState } from '../../../global/types'; import type { ApiPrivacySettings } from '../../../types'; import { SettingsScreens } from '../../../types'; import { selectCanSetPasscode, selectIsCurrentUserPremium } from '../../../global/selectors'; import useHistoryBack from '../../../hooks/useHistoryBack'; import useLang from '../../../hooks/useLang'; import PremiumIcon from '../../common/PremiumIcon'; import Checkbox from '../../ui/Checkbox'; import ListItem from '../../ui/ListItem'; 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; shouldNewNonContactPeersRequirePremium?: boolean; canDisplayChatInTitle?: boolean; privacy: GlobalState['settings']['privacy']; }; const SettingsPrivacy: FC = ({ isActive, isCurrentUserPremium, hasPassword, hasPasscode, blockedCount, webAuthCount, isSensitiveEnabled, canChangeSensitive, canDisplayAutoarchiveSetting, shouldArchiveAndMuteNewNonContact, shouldNewNonContactPeersRequirePremium, canDisplayChatInTitle, canSetPasscode, privacy, onScreenSelect, onReset, }) => { const { loadPrivacySettings, loadBlockedUsers, loadContentSettings, updateContentSettings, loadGlobalPrivacySettings, updateGlobalPrivacySettings, loadWebAuthorizations, setSettingOption, } = getActions(); useEffect(() => { loadBlockedUsers(); 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 handleChatInTitleChange = useCallback((isChecked: boolean) => { setSettingOption({ canDisplayChatInTitle: isChecked, }); }, []); const handleUpdateContentSettings = useCallback((isChecked: boolean) => { updateContentSettings(isChecked); }, [updateContentSettings]); function getVisibilityValue(setting?: ApiPrivacySettings) { const { visibility, shouldAllowPremium } = 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(',')})` : ''; if (shouldAllowPremium) { return lang(exceptionString ? 'ContactsAndPremium' : 'PrivacyPremium'); } 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(privacy.phoneNumber)}
onScreenSelect(SettingsScreens.PrivacyLastSeen)} >
{lang('LastSeenTitle')} {getVisibilityValue(privacy.lastSeen)}
onScreenSelect(SettingsScreens.PrivacyProfilePhoto)} >
{lang('PrivacyProfilePhotoTitle')} {getVisibilityValue(privacy.profilePhoto)}
onScreenSelect(SettingsScreens.PrivacyBio)} >
{lang('PrivacyBio')} {getVisibilityValue(privacy.bio)}
onScreenSelect(SettingsScreens.PrivacyBirthday)} >
{lang('PrivacyBirthday')} {getVisibilityValue(privacy.birthday)}
onScreenSelect(SettingsScreens.PrivacyForwarding)} >
{lang('PrivacyForwardsTitle')} {getVisibilityValue(privacy.forwards)}
onScreenSelect(SettingsScreens.PrivacyPhoneCall)} >
{lang('WhoCanCallMe')} {getVisibilityValue(privacy.phoneCall)}
} className="no-icon" // eslint-disable-next-line react/jsx-no-bind onClick={() => onScreenSelect(SettingsScreens.PrivacyVoiceMessages)} >
{lang('PrivacyVoiceMessagesTitle')} {getVisibilityValue(privacy.voiceMessages)}
} className="no-icon" // eslint-disable-next-line react/jsx-no-bind onClick={() => onScreenSelect(SettingsScreens.PrivacyMessages)} >
{lang('PrivacyMessagesTitle')} {shouldNewNonContactPeersRequirePremium ? lang('PrivacyMessagesContactsAndPremium') : lang('P2PEverybody')}
onScreenSelect(SettingsScreens.PrivacyGroupChats)} >
{lang('WhoCanAddMe')} {getVisibilityValue(privacy.chatInvite)}
{canChangeSensitive && (

{lang('lng_settings_sensitive_title')}

)} {canDisplayAutoarchiveSetting && (

{lang('NewChatsFromNonContacts')}

)}

{lang('lng_settings_window_system')}

); }; export default memo(withGlobal( (global): StateProps => { const { settings: { byKey: { hasPassword, isSensitiveEnabled, canChangeSensitive, shouldArchiveAndMuteNewNonContact, canDisplayChatInTitle, shouldNewNonContactPeersRequirePremium, }, 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, shouldNewNonContactPeersRequirePremium, privacy, canDisplayChatInTitle, canSetPasscode: selectCanSetPasscode(global), }; }, )(SettingsPrivacy));