import type { FC } from '../../../lib/teact/teact'; import { memo, useCallback, useEffect, useMemo } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; import type { ApiPrivacySettings } from '../../../api/types'; import type { GlobalState } from '../../../global/types'; import { SettingsScreens } from '../../../types'; import { ACCOUNT_TTL_OPTIONS } from '../../../config'; import { selectCanSetPasscode, selectIsCurrentUserFrozen, selectIsCurrentUserPremium, } from '../../../global/selectors'; import { selectSharedSettings } from '../../../global/selectors/sharedState'; import { getClosestEntry } from '../../../util/getClosestEntry'; import useHistoryBack from '../../../hooks/useHistoryBack'; import useLang from '../../../hooks/useLang'; import useLastCallback from '../../../hooks/useLastCallback'; import useOldLang from '../../../hooks/useOldLang'; import StarIcon from '../../common/icons/StarIcon'; import Button from '../../ui/Button'; import Checkbox from '../../ui/Checkbox'; import ListItem from '../../ui/ListItem'; type OwnProps = { isActive?: boolean; 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; shouldChargeForMessages: boolean; canDisplayChatInTitle?: boolean; isCurrentUserFrozen?: boolean; needAgeVideoVerification?: boolean; privacy: GlobalState['settings']['privacy']; accountDaysTtl?: number; }; const DAYS_PER_MONTH = 30; const SettingsPrivacy: FC = ({ isActive, isCurrentUserPremium, hasPassword, hasPasscode, blockedCount, webAuthCount, isSensitiveEnabled, canChangeSensitive, canDisplayAutoarchiveSetting, shouldArchiveAndMuteNewNonContact, shouldNewNonContactPeersRequirePremium, shouldChargeForMessages, canDisplayChatInTitle, canSetPasscode, needAgeVideoVerification, privacy, onReset, isCurrentUserFrozen, accountDaysTtl, }) => { const { openDeleteAccountModal, loadPrivacySettings, loadBlockedUsers, updateContentSettings, loadGlobalPrivacySettings, updateGlobalPrivacySettings, loadWebAuthorizations, setSharedSettingOption, openSettingsScreen, loadAccountDaysTtl, openAgeVerificationModal, } = getActions(); useEffect(() => { if (!isCurrentUserFrozen) { loadBlockedUsers(); loadPrivacySettings(); loadWebAuthorizations(); } }, [isCurrentUserFrozen]); useEffect(() => { if (isActive && !isCurrentUserFrozen) { loadGlobalPrivacySettings(); loadAccountDaysTtl(); } }, [isActive, isCurrentUserFrozen, loadGlobalPrivacySettings]); const oldLang = useOldLang(); const lang = useLang(); useHistoryBack({ isActive, onBack: onReset, }); const handleArchiveAndMuteChange = useCallback((isEnabled: boolean) => { updateGlobalPrivacySettings({ shouldArchiveAndMuteNewNonContact: isEnabled, }); }, [updateGlobalPrivacySettings]); const handleChatInTitleChange = useCallback((isChecked: boolean) => { setSharedSettingOption({ canDisplayChatInTitle: isChecked, }); }, []); const handleUpdateContentSettings = useCallback((isChecked: boolean) => { updateContentSettings({ isSensitiveEnabled: isChecked }); }, [updateContentSettings]); const handleAgeVerification = useCallback(() => { openAgeVerificationModal(); }, [openAgeVerificationModal]); const handleOpenDeleteAccountModal = useLastCallback(() => { if (!accountDaysTtl) return; openDeleteAccountModal({ days: accountDaysTtl }); }); const dayOption = useMemo(() => { if (!accountDaysTtl) return undefined; return getClosestEntry(ACCOUNT_TTL_OPTIONS, accountDaysTtl / DAYS_PER_MONTH).toString(); }, [accountDaysTtl]); function getVisibilityValue(setting?: ApiPrivacySettings) { if (!setting) return oldLang('Loading'); const { visibility, shouldAllowPremium, botsPrivacy } = setting; const isAllowBots = botsPrivacy === 'allow'; const isVisibilityEverybody = visibility === 'everybody'; const shouldShowBotsString = isAllowBots && !isVisibilityEverybody; const blockCount = setting.blockChatIds.length + setting.blockUserIds.length; const allowCount = setting.allowChatIds.length + setting.allowUserIds.length; const total = []; if (blockCount) total.push(`-${blockCount}`); if (allowCount && !isVisibilityEverybody) total.push(`+${allowCount}`); const botPrivacyString = shouldShowBotsString ? lang('PrivacyValueBots') : ''; const totalString = lang.conjunction(total); const exceptionString = [botPrivacyString, totalString].filter(Boolean).join(' '); if (shouldShowBotsString && !isVisibilityEverybody) return exceptionString; if (shouldAllowPremium) { return oldLang(exceptionString ? 'ContactsAndPremium' : 'PrivacyPremium'); } switch (visibility) { case 'everybody': return `${oldLang('P2PEverybody')} ${exceptionString}`; case 'contacts': return `${oldLang('P2PContacts')} ${exceptionString}`; case 'nobody': return `${oldLang('P2PNobody')} ${exceptionString}`; } return undefined; } return (
openSettingsScreen({ screen: SettingsScreens.PrivacyBlockedUsers })} > {oldLang('BlockedUsers')} {blockedCount || ''} {canSetPasscode && ( openSettingsScreen({ screen: hasPasscode ? SettingsScreens.PasscodeEnabled : SettingsScreens.PasscodeDisabled, })} >
{oldLang('Passcode')} {oldLang(hasPasscode ? 'PasswordOn' : 'PasswordOff')}
)} openSettingsScreen({ screen: hasPassword ? SettingsScreens.TwoFaEnabled : SettingsScreens.TwoFaDisabled, })} >
{oldLang('TwoStepVerification')} {oldLang(hasPassword ? 'PasswordOn' : 'PasswordOff')}
{webAuthCount > 0 && ( openSettingsScreen({ screen: SettingsScreens.ActiveWebsites })} > {oldLang('PrivacySettings.WebSessions')} {webAuthCount} )}

{oldLang('PrivacyTitle')}

openSettingsScreen({ screen: SettingsScreens.PrivacyPhoneNumber })} >
{oldLang('PrivacyPhoneTitle')} {getVisibilityValue(privacy.phoneNumber)}
openSettingsScreen({ screen: SettingsScreens.PrivacyLastSeen })} >
{oldLang('LastSeenTitle')} {getVisibilityValue(privacy.lastSeen)}
openSettingsScreen({ screen: SettingsScreens.PrivacyProfilePhoto })} >
{oldLang('PrivacyProfilePhotoTitle')} {getVisibilityValue(privacy.profilePhoto)}
openSettingsScreen({ screen: SettingsScreens.PrivacyBio })} >
{oldLang('PrivacyBio')} {getVisibilityValue(privacy.bio)}
openSettingsScreen({ screen: SettingsScreens.PrivacyBirthday })} >
{oldLang('PrivacyBirthday')} {getVisibilityValue(privacy.birthday)}
openSettingsScreen({ screen: SettingsScreens.PrivacyGifts })} >
{lang('PrivacyGifts')} {getVisibilityValue(privacy.gifts)}
openSettingsScreen({ screen: SettingsScreens.PrivacyForwarding })} >
{oldLang('PrivacyForwardsTitle')} {getVisibilityValue(privacy.forwards)}
openSettingsScreen({ screen: SettingsScreens.PrivacyPhoneCall })} >
{oldLang('WhoCanCallMe')} {getVisibilityValue(privacy.phoneCall)}
} className="no-icon" onClick={() => openSettingsScreen({ screen: SettingsScreens.PrivacyVoiceMessages })} >
{oldLang('PrivacyVoiceMessagesTitle')} {getVisibilityValue(privacy.voiceMessages)}
} className="no-icon" onClick={() => openSettingsScreen({ screen: SettingsScreens.PrivacyMessages })} >
{oldLang('PrivacyMessagesTitle')} {shouldChargeForMessages ? lang('PrivacyPaidMessagesValue') : shouldNewNonContactPeersRequirePremium ? oldLang('PrivacyMessagesContactsAndPremium') : oldLang('P2PEverybody')}
openSettingsScreen({ screen: SettingsScreens.PrivacyGroupChats })} >
{oldLang('WhoCanAddMe')} {getVisibilityValue(privacy.chatInvite)}
{canChangeSensitive && (

{oldLang('lng_settings_sensitive_title')}

{!isSensitiveEnabled && needAgeVideoVerification && ( )}
)} {canDisplayAutoarchiveSetting && (

{oldLang('NewChatsFromNonContacts')}

)}

{oldLang('lng_settings_window_system')}

{lang('DeleteMyAccount')}

{lang('DeleteAccountIfAwayFor')} {lang('Months', { count: dayOption }, { pluralValue: 1 })}
); }; export default memo(withGlobal( (global): Complete => { const { settings: { byKey: { hasPassword, isSensitiveEnabled, canChangeSensitive, shouldArchiveAndMuteNewNonContact, shouldNewNonContactPeersRequirePremium, nonContactPeersPaidStars, }, privacy, accountDaysTtl, }, blocked, passcode: { hasPasscode, }, appConfig, } = global; const { canDisplayChatInTitle } = selectSharedSettings(global); const shouldChargeForMessages = Boolean(nonContactPeersPaidStars); const isCurrentUserFrozen = selectIsCurrentUserFrozen(global); const isCurrentUserPremium = selectIsCurrentUserPremium(global); return { isCurrentUserPremium, hasPassword, hasPasscode: Boolean(hasPasscode), blockedCount: blocked.totalCount, webAuthCount: global.activeWebSessions.orderedHashes.length, isSensitiveEnabled, canDisplayAutoarchiveSetting: appConfig.canDisplayAutoarchiveSetting || isCurrentUserPremium, shouldArchiveAndMuteNewNonContact, canChangeSensitive, shouldNewNonContactPeersRequirePremium, shouldChargeForMessages, needAgeVideoVerification: Boolean(appConfig.needAgeVideoVerification), privacy, canDisplayChatInTitle, canSetPasscode: selectCanSetPasscode(global), isCurrentUserFrozen, accountDaysTtl, }; }, )(SettingsPrivacy));