import React, { memo, useMemo } from '../../../lib/teact/teact'; import { getActions } from '../../../global'; import type { ApiUser } from '../../../api/types'; import type { CustomPeer } from '../../../types'; import { getCurrentMaxAccountCount, getCurrentProdAccountCount } from '../../../global/helpers'; import { getAccountSlotUrl } from '../../../util/multiaccount'; import { REM } from '../../common/helpers/mediaDimensions'; import useLang from '../../../hooks/useLang'; import useLastCallback from '../../../hooks/useLastCallback'; import useMultiaccountInfo from '../../../hooks/useMultiaccountInfo'; import Avatar from '../../common/Avatar'; import FullNameTitle from '../../common/FullNameTitle'; import MenuItem from '../../ui/MenuItem'; import MenuSeparator from '../../ui/MenuSeparator'; type OwnProps = { currentUser: ApiUser; totalLimit: number; onSelectCurrent?: VoidFunction; }; const NOTIFICATION_DURATION = 7000; const AccountMenuItems = ({ currentUser, totalLimit, onSelectCurrent, }: OwnProps) => { const { showNotification } = getActions(); const lang = useLang(); const accounts = useMultiaccountInfo(currentUser); const currentCount = getCurrentProdAccountCount(); const maxCount = getCurrentMaxAccountCount(); const currentAccountInfo = useMemo(() => { return Object.values(accounts).find((account) => account.userId === currentUser.id); }, [accounts, currentUser.id]); const shouldShowLimit = currentCount >= maxCount; const handleLimitClick = useLastCallback(() => { showNotification({ title: lang('PremiumLimitAccountsTitle'), message: currentUser.isPremium ? lang('PremiumLimitAccounts') : lang('PremiumLimitAccountsNoPremium'), duration: NOTIFICATION_DURATION, }); }); const newAccountUrl = useMemo(() => { if (!Object.values(accounts).length) { return undefined; } if (currentCount === totalLimit) { return undefined; } let freeIndex = 1; while (accounts[freeIndex]) { freeIndex += 1; } return getAccountSlotUrl(freeIndex, true); }, [accounts, currentCount, totalLimit]); return (