From 051897a153da96e1642b38f66a9bf5c8aaa60d98 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 16 Jun 2021 19:12:38 +0300 Subject: [PATCH] Revert unstable code --- src/api/gramjs/methods/client.ts | 1 - src/bundles/auth.ts | 1 + src/components/auth/Auth.scss | 12 +++------ src/components/auth/Auth.tsx | 11 +++++--- src/components/auth/AuthPhoneNumber.tsx | 8 +++--- src/components/auth/AuthQrCode.async.tsx | 13 ++++++++++ src/components/auth/AuthQrCode.tsx | 14 +++++------ src/components/middle/MessageList.tsx | 3 --- src/components/middle/MessageScroll.tsx | 25 +++++++++++++------ .../middle/message/MessageContextMenu.tsx | 8 +++++- src/global/types.ts | 2 +- src/hooks/useHeavyAnimationCheck.ts | 6 ++--- src/hooks/useIntersectionObserver.ts | 14 ++++------- src/lib/gramjs/client/auth.ts | 19 +++----------- src/modules/actions/api/initial.ts | 2 +- 15 files changed, 72 insertions(+), 67 deletions(-) create mode 100644 src/components/auth/AuthQrCode.async.tsx diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index fae7eb222..93432d901 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -68,7 +68,6 @@ export async function init(_onUpdate: OnApiUpdate, sessionData?: ApiSessionData) firstAndLastNames: onRequestRegistration, qrCode: onRequestQrCode, onError: onAuthError, - initialMethod: 'qrCode', }); if (DEBUG) { diff --git a/src/bundles/auth.ts b/src/bundles/auth.ts index de01ccb84..c69454d8a 100644 --- a/src/bundles/auth.ts +++ b/src/bundles/auth.ts @@ -1,3 +1,4 @@ export { default as AuthCode } from '../components/auth/AuthCode'; export { default as AuthPassword } from '../components/auth/AuthPassword'; export { default as AuthRegister } from '../components/auth/AuthRegister'; +export { default as AuthQrCode } from '../components/auth/AuthQrCode'; diff --git a/src/components/auth/Auth.scss b/src/components/auth/Auth.scss index 1ea9f4de7..f9c4d5473 100644 --- a/src/components/auth/Auth.scss +++ b/src/components/auth/Auth.scss @@ -9,10 +9,6 @@ @media (min-width: 600px) and (min-height: 450px) { padding: 1.5rem; padding-top: 6.8rem; - - &.qr { - padding-top: 5rem; - } } #logo, .AvatarEditable label { @@ -108,14 +104,12 @@ } #auth-qr-form { - .qr-container, .qr-loading { - height: 280px; - } - .qr-container { + height: 280px; + opacity: 1; transform: scale(1); - transition: transform 300ms cubic-bezier(0.34, 1.56, 0.64, 1), opacity 300ms; + transition: transform .3s cubic-bezier(0.34, 1.56, 0.64, 1), opacity .3s; &.pre-animate { opacity: 0.5; diff --git a/src/components/auth/Auth.tsx b/src/components/auth/Auth.tsx index c1a8a2b36..b791e07b7 100644 --- a/src/components/auth/Auth.tsx +++ b/src/components/auth/Auth.tsx @@ -11,7 +11,7 @@ import AuthPhoneNumber from './AuthPhoneNumber'; import AuthCode from './AuthCode.async'; import AuthPassword from './AuthPassword.async'; import AuthRegister from './AuthRegister.async'; -import AuthQrCode from './AuthQrCode'; +import AuthQrCode from './AuthQrCode.async'; import './Auth.scss'; @@ -24,6 +24,9 @@ const Auth: FC = ({ authState, reset, initApi }) => initApi(); }, [reset, initApi]); + useEffect(() => { + }, []); + switch (authState) { case 'authorizationStateWaitCode': return ; @@ -31,11 +34,11 @@ const Auth: FC = ({ authState, reset, initApi }) => return ; case 'authorizationStateWaitRegistration': return ; - case 'authorizationStateWaitPhoneNumber': - return ; case 'authorizationStateWaitQrCode': - default: return ; + case 'authorizationStateWaitPhoneNumber': + default: + return ; } }; diff --git a/src/components/auth/AuthPhoneNumber.tsx b/src/components/auth/AuthPhoneNumber.tsx index 75d460a63..5f09f5d85 100644 --- a/src/components/auth/AuthPhoneNumber.tsx +++ b/src/components/auth/AuthPhoneNumber.tsx @@ -25,7 +25,7 @@ type StateProps = Pick; type DispatchProps = Pick; const MIN_NUMBER_LENGTH = 7; @@ -45,7 +45,7 @@ const AuthPhoneNumber: FC = ({ setAuthRememberMe, loadNearestCountry, clearAuthError, - goToAuthQrCode, + gotToAuthQrCode, }) => { // eslint-disable-next-line no-null/no-null const inputRef = useRef(null); @@ -183,7 +183,7 @@ const AuthPhoneNumber: FC = ({ ) )} {isAuthReady && ( - )} @@ -209,6 +209,6 @@ export default memo(withGlobal( 'setAuthRememberMe', 'clearAuthError', 'loadNearestCountry', - 'goToAuthQrCode', + 'gotToAuthQrCode', ]), )(AuthPhoneNumber)); diff --git a/src/components/auth/AuthQrCode.async.tsx b/src/components/auth/AuthQrCode.async.tsx new file mode 100644 index 000000000..18d416fab --- /dev/null +++ b/src/components/auth/AuthQrCode.async.tsx @@ -0,0 +1,13 @@ +import React, { FC, memo } from '../../lib/teact/teact'; +import { Bundles } from '../../util/moduleLoader'; + +import useModuleLoader from '../../hooks/useModuleLoader'; +import Loading from '../ui/Loading'; + +const AuthQrCodeAsync: FC = () => { + const AuthQrCode = useModuleLoader(Bundles.Auth, 'AuthQrCode'); + + return AuthQrCode ? : ; +}; + +export default memo(AuthQrCodeAsync); diff --git a/src/components/auth/AuthQrCode.tsx b/src/components/auth/AuthQrCode.tsx index 86bda6dcc..ae4e3a029 100644 --- a/src/components/auth/AuthQrCode.tsx +++ b/src/components/auth/AuthQrCode.tsx @@ -9,6 +9,7 @@ import { pick } from '../../util/iteratees'; import Loading from '../ui/Loading'; import Button from '../ui/Button'; +import buildClassName from '../../util/buildClassName'; import useHistoryBack from '../../hooks/useHistoryBack'; type StateProps = Pick; @@ -31,7 +32,6 @@ const AuthCode: FC = ({ container.innerHTML = ''; container.classList.remove('pre-animate'); - QrCreator.render({ text: `${DATA_PREFIX}${authQrCode.token}`, radius: 0.5, @@ -45,13 +45,11 @@ const AuthCode: FC = ({ return (
-
- {authQrCode ? ( -
- ) : ( -
- )} -

Log in to Telegram by QR Code

+
+
+ {!authQrCode && } +
+

Log in to Telegram by QR Code

  1. Open Telegram on your phone
  2. Go to Settings > Devices > Scan QR
  3. diff --git a/src/components/middle/MessageList.tsx b/src/components/middle/MessageList.tsx index 0c7d66c95..443b0f4a1 100644 --- a/src/components/middle/MessageList.tsx +++ b/src/components/middle/MessageList.tsx @@ -54,7 +54,6 @@ import fastSmoothScroll, { isAnimatingScroll } from '../../util/fastSmoothScroll import renderText from '../common/helpers/renderText'; import useLang, { LangFn } from '../../hooks/useLang'; import useWindowSize from '../../hooks/useWindowSize'; -import useBackgroundMode from '../../hooks/useBackgroundMode'; import Loading from '../ui/Loading'; import MessageScroll from './MessageScroll'; @@ -228,8 +227,6 @@ const MessageList: FC = ({ } }); - useBackgroundMode(freezeForReading, unfreezeForReading); - useOnChange(() => { memoFocusingIdRef.current = focusingId; diff --git a/src/components/middle/MessageScroll.tsx b/src/components/middle/MessageScroll.tsx index 07bc2064a..250c7f491 100644 --- a/src/components/middle/MessageScroll.tsx +++ b/src/components/middle/MessageScroll.tsx @@ -1,5 +1,7 @@ import { MutableRefObject } from 'react'; -import React, { FC, useCallback, useRef } from '../../lib/teact/teact'; +import React, { + FC, useCallback, useEffect, useRef, +} from '../../lib/teact/teact'; import { MESSAGE_LIST_SENSITIVE_AREA } from '../../config'; import resetScroll from '../../util/resetScroll'; @@ -23,6 +25,9 @@ type OwnProps = { const FAB_THRESHOLD = 50; const TOOLS_FREEZE_TIMEOUT = 100; +// Local flag is used because `freeze/unfreeze` methods are controlled by heavy animation +let areToolsFrozen = false; + const MessageScroll: FC = ({ containerRef, className, @@ -44,6 +49,10 @@ const MessageScroll: FC = ({ const fabTriggerRef = useRef(null); const toggleScrollTools = useCallback(() => { + if (areToolsFrozen) { + return; + } + if (!messageIds || !messageIds.length) { onFabToggle(false); onNotchToggle(false); @@ -109,33 +118,35 @@ const MessageScroll: FC = ({ const { observe: observeIntersectionForNotch, - freeze: freezeForNotch, - unfreeze: unfreezeForNotch, } = useIntersectionObserver({ rootRef: containerRef, }, toggleScrollTools); useOnIntersect(fabTriggerRef, observeIntersectionForNotch); + // Do not load more and show FAB when focusing useOnChange(() => { if (focusingId) { freezeForLoadMore(); + freezeForFab(); } else { + unfreezeForFab(); unfreezeForLoadMore(); } }, [focusingId]); // Workaround for FAB and notch flickering with tall incoming message useOnChange(() => { - freezeForFab(); - freezeForNotch(); + areToolsFrozen = true; setTimeout(() => { - unfreezeForNotch(); - unfreezeForFab(); + areToolsFrozen = false; }, TOOLS_FREEZE_TIMEOUT); }, [messageIds]); + // Workaround for stuck FAB when many unread messages + useEffect(toggleScrollTools, [firstUnreadId]); + return (
    diff --git a/src/components/middle/message/MessageContextMenu.tsx b/src/components/middle/message/MessageContextMenu.tsx index e10907af2..377744ba6 100644 --- a/src/components/middle/message/MessageContextMenu.tsx +++ b/src/components/middle/message/MessageContextMenu.tsx @@ -1,10 +1,11 @@ -import React, { FC, useCallback } from '../../../lib/teact/teact'; +import React, { FC, useCallback, useEffect } from '../../../lib/teact/teact'; import { ApiMessage } from '../../../api/types'; import { IAnchorPosition } from '../../../types'; import { getMessageCopyOptions } from './helpers/copyOptions'; import useContextMenuPosition from '../../../hooks/useContextMenuPosition'; +import { dispatchHeavyAnimationEvent } from '../../../hooks/useHeavyAnimationCheck'; import useLang from '../../../hooks/useLang'; import Menu from '../../ui/Menu'; @@ -45,6 +46,7 @@ type OwnProps = { onCopyLink?: () => void; }; +const ANIMATION_DURATION = 200; const SCROLLBAR_WIDTH = 10; const MessageContextMenu: FC = ({ @@ -79,6 +81,10 @@ const MessageContextMenu: FC = ({ onCloseAnimationEnd, onCopyLink, }) => { + useEffect(() => { + dispatchHeavyAnimationEvent(ANIMATION_DURATION); + }, [isOpen]); + const copyOptions = getMessageCopyOptions(message, onClose, canCopyLink ? onCopyLink : undefined); const getTriggerElement = useCallback(() => { diff --git a/src/global/types.ts b/src/global/types.ts index eb7e2aaae..a443a62d1 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -401,7 +401,7 @@ export type ActionTypes = ( 'toggleSafeLinkModal' | 'openHistoryCalendar' | 'closeHistoryCalendar' | // auth 'setAuthPhoneNumber' | 'setAuthCode' | 'setAuthPassword' | 'signUp' | 'returnToAuthPhoneNumber' | 'signOut' | - 'setAuthRememberMe' | 'clearAuthError' | 'uploadProfilePhoto' | 'goToAuthQrCode' | 'clearCache' | + 'setAuthRememberMe' | 'clearAuthError' | 'uploadProfilePhoto' | 'gotToAuthQrCode' | 'clearCache' | // chats 'preloadTopChatMessages' | 'loadChats' | 'loadMoreChats' | 'openChat' | 'openChatWithInfo' | 'openSupportChat' | 'openTipsChat' | diff --git a/src/hooks/useHeavyAnimationCheck.ts b/src/hooks/useHeavyAnimationCheck.ts index 8060fa863..a362c5a6b 100644 --- a/src/hooks/useHeavyAnimationCheck.ts +++ b/src/hooks/useHeavyAnimationCheck.ts @@ -7,10 +7,8 @@ let timeout: number | undefined; let isAnimating = false; export const dispatchHeavyAnimationEvent = (duration: number) => { - if (!isAnimating) { - isAnimating = true; - document.dispatchEvent(new Event(ANIMATION_START_EVENT)); - } + document.dispatchEvent(new Event(ANIMATION_START_EVENT)); + isAnimating = true; if (timeout) { clearTimeout(timeout); diff --git a/src/hooks/useIntersectionObserver.ts b/src/hooks/useIntersectionObserver.ts index 8bb940955..42cc5045a 100644 --- a/src/hooks/useIntersectionObserver.ts +++ b/src/hooks/useIntersectionObserver.ts @@ -41,23 +41,19 @@ export function useIntersectionObserver({ }, rootCallback?: RootCallback): Response { const controllerRef = useRef(); const rootCallbackRef = useRef(); - const freezeFlagsRef = useRef(0); + const isFrozenRef = useRef(); const onUnfreezeRef = useRef(); rootCallbackRef.current = rootCallback; const freeze = useCallback(() => { - freezeFlagsRef.current++; + isFrozenRef.current = true; }, []); const unfreeze = useCallback(() => { - if (!freezeFlagsRef.current) { - return; - } + isFrozenRef.current = false; - freezeFlagsRef.current--; - - if (!freezeFlagsRef.current && onUnfreezeRef.current) { + if (onUnfreezeRef.current) { onUnfreezeRef.current(); onUnfreezeRef.current = undefined; } @@ -108,7 +104,7 @@ export function useIntersectionObserver({ entriesAccumulator.set(entry.target, entry); }); - if (freezeFlagsRef.current) { + if (isFrozenRef.current) { onUnfreezeRef.current = () => { observerCallback(); }; diff --git a/src/lib/gramjs/client/auth.ts b/src/lib/gramjs/client/auth.ts index f3bdbd6e8..30546ca41 100644 --- a/src/lib/gramjs/client/auth.ts +++ b/src/lib/gramjs/client/auth.ts @@ -13,7 +13,6 @@ export interface UserAuthParams { qrCode: (qrCode: { token: Buffer; expires: number }) => Promise; onError: (err: Error) => void; forceSMS?: boolean; - initialMethod?: 'phoneNumber' | 'qrCode'; } export interface BotAuthParams { @@ -25,7 +24,6 @@ interface ApiCredentials { apiHash: string; } -const DEFAULT_INITIAL_METHOD = 'phoneNumber'; const QR_CODE_TIMEOUT = 30000; export async function authFlow( @@ -33,20 +31,11 @@ export async function authFlow( apiCredentials: ApiCredentials, authParams: UserAuthParams | BotAuthParams, ) { - let me: Api.TypeUser; - - if ('botAuthToken' in authParams) { - me = await signInBot(client, apiCredentials, authParams); - } else { - const { initialMethod = DEFAULT_INITIAL_METHOD } = authParams; - - if (initialMethod === 'phoneNumber') { - me = await signInUser(client, apiCredentials, authParams); - } else { - me = await signInUserWithQrCode(client, apiCredentials, authParams); - } - } + const me = 'phoneNumber' in authParams + ? await signInUser(client, apiCredentials, authParams) + : await signInBot(client, apiCredentials, authParams); + // TODO @logger client._log.info('Signed in successfully as', utils.getDisplayName(me)); } diff --git a/src/modules/actions/api/initial.ts b/src/modules/actions/api/initial.ts index af3a101f8..c71b03164 100644 --- a/src/modules/actions/api/initial.ts +++ b/src/modules/actions/api/initial.ts @@ -101,7 +101,7 @@ addReducer('returnToAuthPhoneNumber', (global) => { }; }); -addReducer('goToAuthQrCode', (global) => { +addReducer('gotToAuthQrCode', (global) => { void callApi('restartAuthWithQr'); return {