diff --git a/src/components/left/main/AccountMenuItems.tsx b/src/components/left/main/AccountMenuItems.tsx index 60317d4a5..47158ce8a 100644 --- a/src/components/left/main/AccountMenuItems.tsx +++ b/src/components/left/main/AccountMenuItems.tsx @@ -2,9 +2,11 @@ import { memo, useMemo } from '../../../lib/teact/teact'; import { getActions } from '../../../global'; import type { ApiUser } from '../../../api/types'; -import type { CustomPeer } from '../../../types'; +import type { AccountInfo, CustomPeer } from '../../../types'; +import { temporarilySuspendCacheUpdate } from '../../../global/cache'; import { getCurrentMaxAccountCount, getCurrentProdAccountCount } from '../../../global/helpers'; +import { IS_SAFARI } from '../../../util/browser/windowEnvironment'; import { getAccountSlotUrl } from '../../../util/multiaccount'; import { REM } from '../../common/helpers/mediaDimensions'; @@ -43,12 +45,27 @@ const AccountMenuItems = ({ const shouldShowLimit = currentCount >= maxCount; - const handleLimitClick = useLastCallback(() => { - showNotification({ - title: lang('PremiumLimitAccountsTitle'), - message: currentUser.isPremium ? lang('PremiumLimitAccounts') : lang('PremiumLimitAccountsNoPremium'), - duration: NOTIFICATION_DURATION, - }); + const handleAccountClick = useLastCallback((account: AccountInfo) => { + if (account.userId === currentUser.id) { + onSelectCurrent?.(); + return; + } + + // IDB locks up if we write large payload on navigation + if (IS_SAFARI) temporarilySuspendCacheUpdate(); + }); + + const handleNewAccountClick = useLastCallback(() => { + if (shouldShowLimit) { + showNotification({ + title: lang('PremiumLimitAccountsTitle'), + message: currentUser.isPremium ? lang('PremiumLimitAccounts') : lang('PremiumLimitAccountsNoPremium'), + duration: NOTIFICATION_DURATION, + }); + return; + } + + if (IS_SAFARI) temporarilySuspendCacheUpdate(); }); const newAccountUrl = useMemo(() => { @@ -96,7 +113,7 @@ const AccountMenuItems = ({ previewUrl={account.avatarUri} /> )} - onClick={account.userId === currentUser.id ? onSelectCurrent : undefined} + onClick={() => handleAccountClick(account)} href={account.userId !== currentUser.id ? getAccountSlotUrl(Number(slot)) : undefined} > {account.isTest && T} @@ -111,7 +128,7 @@ const AccountMenuItems = ({ icon="add" rel="noopener" // Allow referrer to be passed href={!shouldShowLimit ? newAccountUrl : undefined} - onClick={shouldShowLimit ? handleLimitClick : undefined} + onClick={handleNewAccountClick} > {lang('MenuAddAccount')} diff --git a/src/global/cache.ts b/src/global/cache.ts index 27bd5b634..062d5f3bc 100644 --- a/src/global/cache.ts +++ b/src/global/cache.ts @@ -55,6 +55,7 @@ const updateCacheForced = () => updateCache(true); let isCaching = false; let isRemovingCache = false; +let cacheUpdateSuspensionTimestamp = 0; let unsubscribeFromBeforeUnload: NoneToVoidFunction | undefined; export function cacheGlobal(global: GlobalState) { @@ -382,7 +383,15 @@ function updateCache(force?: boolean) { forceUpdateCache(); } +export function temporarilySuspendCacheUpdate() { + cacheUpdateSuspensionTimestamp = Date.now() + UPDATE_THROTTLE; +} + export function forceUpdateCache(noEncrypt = false) { + if (Date.now() < cacheUpdateSuspensionTimestamp) { + return; + } + const global = getGlobal(); const { hasPasscode, isScreenLocked } = global.passcode;