[Perf] Reduce usage of frequently updated currentUser

This commit is contained in:
Alexander Zinchuk 2023-07-05 13:15:11 +02:00
parent 1c82160285
commit be7920f9e0
5 changed files with 32 additions and 34 deletions

View File

@ -3,9 +3,8 @@ import { getActions, withGlobal } from '../../../global';
import type { FC } from '../../../lib/teact/teact';
import { SettingsScreens } from '../../../types';
import type { ApiUser } from '../../../api/types';
import { selectIsPremiumPurchaseBlocked, selectUser } from '../../../global/selectors';
import { selectIsPremiumPurchaseBlocked } from '../../../global/selectors';
import useLang from '../../../hooks/useLang';
import useHistoryBack from '../../../hooks/useHistoryBack';
@ -22,7 +21,7 @@ type OwnProps = {
type StateProps = {
sessionCount: number;
currentUser?: ApiUser;
currentUserId?: string;
canBuyPremium?: boolean;
};
@ -30,7 +29,7 @@ const SettingsMain: FC<OwnProps & StateProps> = ({
isActive,
onScreenSelect,
onReset,
currentUser,
currentUserId,
sessionCount,
canBuyPremium,
}) => {
@ -41,13 +40,12 @@ const SettingsMain: FC<OwnProps & StateProps> = ({
} = getActions();
const lang = useLang();
const profileId = currentUser?.id;
useEffect(() => {
if (profileId) {
loadProfilePhotos({ profileId });
if (currentUserId) {
loadProfilePhotos({ profileId: currentUserId });
}
}, [profileId, loadProfilePhotos]);
}, [currentUserId, loadProfilePhotos]);
useHistoryBack({
isActive,
@ -61,16 +59,16 @@ const SettingsMain: FC<OwnProps & StateProps> = ({
return (
<div className="settings-content custom-scroll">
<div className="settings-main-menu">
{currentUser && (
{currentUserId && (
<ProfileInfo
userId={currentUser.id}
userId={currentUserId}
canPlayVideo={Boolean(isActive)}
forceShowSelf
/>
)}
{currentUser && (
{currentUserId && (
<ChatExtra
chatOrUserId={currentUser.id}
chatOrUserId={currentUserId}
forceShowSelf
/>
)}
@ -160,7 +158,7 @@ export default memo(withGlobal<OwnProps>(
return {
sessionCount: global.activeSessions.orderedHashes.length,
currentUser: currentUserId ? selectUser(global, currentUserId) : undefined,
currentUserId,
canBuyPremium: !selectIsPremiumPurchaseBlocked(global),
};
},

View File

@ -4,7 +4,7 @@ import React, {
import { getActions } from '../../../global';
import type { FC } from '../../../lib/teact/teact';
import type { ApiPhoto, ApiUser } from '../../../api/types';
import type { ApiPhoto } from '../../../api/types';
import useFlag from '../../../hooks/useFlag';
import useLang from '../../../hooks/useLang';
@ -17,13 +17,13 @@ import ConfirmDialog from '../../ui/ConfirmDialog';
import styles from './SettingsPrivacyPublicPhoto.module.scss';
type OwnProps = {
currentUser: ApiUser;
currentUserId: string;
hasCurrentUserFullInfo?: boolean;
currentUserFallbackPhoto?: ApiPhoto;
};
const SettingsPrivacyPublicProfilePhoto: FC<OwnProps> = ({
currentUser,
currentUserId,
hasCurrentUserFullInfo,
currentUserFallbackPhoto,
}) => {
@ -40,9 +40,9 @@ const SettingsPrivacyPublicProfilePhoto: FC<OwnProps> = ({
useEffect(() => {
if (!hasCurrentUserFullInfo) {
loadFullUser({ userId: currentUser.id });
loadFullUser({ userId: currentUserId });
}
}, [hasCurrentUserFullInfo, currentUser.id, loadFullUser]);
}, [hasCurrentUserFullInfo, currentUserId, loadFullUser]);
const handleSelectFile = useCallback((file: File) => {
uploadProfilePhoto({

View File

@ -7,7 +7,7 @@ import type { ApiPrivacySettings } from '../../../types';
import { SettingsScreens } from '../../../types';
import { getPrivacyKey } from './helpers/privacy';
import { selectUser, selectUserFullInfo } from '../../../global/selectors';
import { selectUserFullInfo } from '../../../global/selectors';
import useLang from '../../../hooks/useLang';
import useHistoryBack from '../../../hooks/useHistoryBack';
@ -26,7 +26,7 @@ type StateProps =
Partial<ApiPrivacySettings> & {
chatsById?: Record<string, ApiChat>;
usersById?: Record<string, ApiUser>;
currentUser: ApiUser;
currentUserId: string;
hasCurrentUserFullInfo?: boolean;
currentUserFallbackPhoto?: ApiPhoto;
};
@ -42,7 +42,7 @@ const SettingsPrivacyVisibility: FC<OwnProps & StateProps> = ({
blockUserIds,
blockChatIds,
chatsById,
currentUser,
currentUserId,
hasCurrentUserFullInfo,
currentUserFallbackPhoto,
}) => {
@ -227,7 +227,7 @@ const SettingsPrivacyVisibility: FC<OwnProps & StateProps> = ({
{screen === SettingsScreens.PrivacyProfilePhoto && exceptionLists.shouldShowAllowed && (
<SettingsPrivacyPublicProfilePhoto
currentUser={currentUser}
currentUserId={currentUserId}
hasCurrentUserFullInfo={hasCurrentUserFullInfo}
currentUserFallbackPhoto={currentUserFallbackPhoto}
/>
@ -241,12 +241,12 @@ export default memo(withGlobal<OwnProps>(
let privacySettings: ApiPrivacySettings | undefined;
const {
currentUserId,
chats: { byId: chatsById },
settings: { privacy },
} = global;
const currentUser = selectUser(global, global.currentUserId!)!;
const currentUserFullInfo = selectUserFullInfo(global, global.currentUserId!);
const currentUserFullInfo = selectUserFullInfo(global, currentUserId!);
switch (screen) {
case SettingsScreens.PrivacyPhoneNumber:
@ -284,7 +284,7 @@ export default memo(withGlobal<OwnProps>(
if (!privacySettings) {
return {
currentUser,
currentUserId: currentUserId!,
hasCurrentUserFullInfo: Boolean(currentUserFullInfo),
currentUserFallbackPhoto: currentUserFullInfo?.fallbackPhoto,
};
@ -293,7 +293,7 @@ export default memo(withGlobal<OwnProps>(
return {
...privacySettings,
chatsById,
currentUser,
currentUserId: currentUserId!,
hasCurrentUserFullInfo: Boolean(currentUserFullInfo),
currentUserFallbackPhoto: currentUserFullInfo?.fallbackPhoto,
};

View File

@ -37,6 +37,7 @@ import {
selectCanAnimateInterface,
selectChatFolder,
} from '../../global/selectors';
import { getUserFullName } from '../../global/helpers';
import buildClassName from '../../util/buildClassName';
import { waitForTransitionEnd } from '../../util/cssAnimationEndListeners';
import { processDeepLink } from '../../util/deeplink';
@ -135,7 +136,7 @@ type StateProps = {
attachBotToInstall?: ApiAttachBot;
requestedAttachBotInChat?: TabState['requestedAttachBotInChat'];
requestedDraft?: TabState['requestedDraft'];
currentUser?: ApiUser;
currentUserName?: string;
urlAuth?: TabState['urlAuth'];
limitReached?: ApiLimitTypeWithModal;
deleteFolderDialog?: ApiChatFolder;
@ -191,7 +192,7 @@ const Main: FC<OwnProps & StateProps> = ({
requestedAttachBotInChat,
requestedDraft,
webApp,
currentUser,
currentUserName,
urlAuth,
isPremiumModalOpen,
isPaymentModalOpen,
@ -516,7 +517,7 @@ const Main: FC<OwnProps & StateProps> = ({
<Dialogs isOpen={hasDialogs} />
{audioMessage && <AudioPlayer key={audioMessage.id} message={audioMessage} noUi />}
<SafeLinkModal url={safeLinkModalUrl} />
<UrlAuthModal urlAuth={urlAuth} currentUser={currentUser} />
<UrlAuthModal urlAuth={urlAuth} currentUserName={currentUserName} />
<HistoryCalendar isOpen={isHistoryCalendarOpen} />
<StickerSetModal
isOpen={Boolean(openedStickerSetShortName)}
@ -643,7 +644,7 @@ export default memo(withGlobal<OwnProps>(
attachBotToInstall: requestedAttachBotInstall?.bot,
requestedAttachBotInChat,
webApp,
currentUser,
currentUserName: getUserFullName(currentUser),
urlAuth,
isCurrentUserPremium: selectIsCurrentUserPremium(global),
isPremiumModalOpen: premiumModal?.isOpen,

View File

@ -4,7 +4,6 @@ import React, {
import { getActions, getGlobal } from '../../global';
import type { FC } from '../../lib/teact/teact';
import type { ApiUser } from '../../api/types';
import type { TabState } from '../../global/types';
import { ensureProtocol } from '../../util/ensureProtocol';
@ -21,11 +20,11 @@ import styles from './UrlAuthModal.module.scss';
export type OwnProps = {
urlAuth?: TabState['urlAuth'];
currentUser?: ApiUser;
currentUserName?: string;
};
const UrlAuthModal: FC<OwnProps> = ({
urlAuth, currentUser,
urlAuth, currentUserName,
}) => {
const { closeUrlAuthModal, acceptBotUrlAuth, acceptLinkUrlAuth } = getActions();
const [isLoginChecked, setLoginChecked] = useState(true);
@ -82,7 +81,7 @@ const UrlAuthModal: FC<OwnProps> = ({
label={(
<>
{renderText(
lang('Conversation.OpenBotLinkLogin', [domain, getUserFullName(currentUser)]),
lang('Conversation.OpenBotLinkLogin', [domain, currentUserName]),
['simple_markdown'],
)}
</>