From 59460baee1161641b7e5a2eeb1829da2b7534597 Mon Sep 17 00:00:00 2001 From: Shahe Shahinyan Date: Thu, 21 Dec 2023 09:51:09 +0400 Subject: [PATCH 1/4] Migrate 'SignInButtons' component to TypeScript --- .../{index.ios.js => index.ios.ts} | 7 +-- .../AppleAuthWrapper/{index.js => index.ts} | 0 .../{index.android.js => index.android.tsx} | 4 +- .../{index.desktop.js => index.desktop.tsx} | 2 +- .../{index.ios.js => index.ios.tsx} | 6 +-- .../{index.website.js => index.website.tsx} | 48 +++++++------------ ...{GetUserLanguage.js => GetUserLanguage.ts} | 2 +- .../{index.desktop.js => index.desktop.tsx} | 2 +- .../{index.native.js => index.native.tsx} | 2 +- .../{index.website.js => index.website.tsx} | 26 +++++----- .../{IconButton.js => IconButton.tsx} | 31 ++++-------- src/libs/actions/Session/index.ts | 8 ++-- src/types/modules/appleAuth.d.ts | 29 +++++++++++ src/types/modules/dom.d.ts | 31 ++++++++++++ src/types/modules/google.d.ts | 36 ++++++++++++++ src/types/modules/navigator.d.ts | 9 ++++ 16 files changed, 159 insertions(+), 84 deletions(-) rename src/components/SignInButtons/AppleAuthWrapper/{index.ios.js => index.ios.ts} (81%) rename src/components/SignInButtons/AppleAuthWrapper/{index.js => index.ts} (100%) rename src/components/SignInButtons/AppleSignIn/{index.android.js => index.android.tsx} (95%) rename src/components/SignInButtons/AppleSignIn/{index.desktop.js => index.desktop.tsx} (96%) rename src/components/SignInButtons/AppleSignIn/{index.ios.js => index.ios.tsx} (94%) rename src/components/SignInButtons/AppleSignIn/{index.website.js => index.website.tsx} (77%) rename src/components/SignInButtons/{GetUserLanguage.js => GetUserLanguage.ts} (78%) rename src/components/SignInButtons/GoogleSignIn/{index.desktop.js => index.desktop.tsx} (97%) rename src/components/SignInButtons/GoogleSignIn/{index.native.js => index.native.tsx} (98%) rename src/components/SignInButtons/GoogleSignIn/{index.website.js => index.website.tsx} (85%) rename src/components/SignInButtons/{IconButton.js => IconButton.tsx} (63%) create mode 100644 src/types/modules/appleAuth.d.ts create mode 100644 src/types/modules/dom.d.ts create mode 100644 src/types/modules/google.d.ts create mode 100644 src/types/modules/navigator.d.ts diff --git a/src/components/SignInButtons/AppleAuthWrapper/index.ios.js b/src/components/SignInButtons/AppleAuthWrapper/index.ios.ts similarity index 81% rename from src/components/SignInButtons/AppleAuthWrapper/index.ios.js rename to src/components/SignInButtons/AppleAuthWrapper/index.ios.ts index 69882d89b1fe..0622ca515ce4 100644 --- a/src/components/SignInButtons/AppleAuthWrapper/index.ios.js +++ b/src/components/SignInButtons/AppleAuthWrapper/index.ios.ts @@ -6,18 +6,19 @@ import * as Session from '@userActions/Session'; * Apple Sign In wrapper for iOS * revokes the session if the credential is revoked. * - * @returns {null} + * @returns null */ function AppleAuthWrapper() { useEffect(() => { if (!appleAuth.isSupported) { return; } - const listener = appleAuth.onCredentialRevoked(() => { + const removeListener = appleAuth.onCredentialRevoked(() => { Session.signOut(); }); + return () => { - listener.remove(); + removeListener(); }; }, []); diff --git a/src/components/SignInButtons/AppleAuthWrapper/index.js b/src/components/SignInButtons/AppleAuthWrapper/index.ts similarity index 100% rename from src/components/SignInButtons/AppleAuthWrapper/index.js rename to src/components/SignInButtons/AppleAuthWrapper/index.ts diff --git a/src/components/SignInButtons/AppleSignIn/index.android.js b/src/components/SignInButtons/AppleSignIn/index.android.tsx similarity index 95% rename from src/components/SignInButtons/AppleSignIn/index.android.js rename to src/components/SignInButtons/AppleSignIn/index.android.tsx index 9dc736789c61..ac84987afefa 100644 --- a/src/components/SignInButtons/AppleSignIn/index.android.js +++ b/src/components/SignInButtons/AppleSignIn/index.android.tsx @@ -18,7 +18,7 @@ const config = { /** * Apple Sign In method for Android that returns authToken. - * @returns {Promise} + * @returns Promise that returns a string when resolved */ function appleSignInRequest() { appleAuthAndroid.configure(config); @@ -32,7 +32,7 @@ function appleSignInRequest() { /** * Apple Sign In button for Android. - * @returns {React.Component} + * @returns */ function AppleSignIn() { const handleSignIn = () => { diff --git a/src/components/SignInButtons/AppleSignIn/index.desktop.js b/src/components/SignInButtons/AppleSignIn/index.desktop.tsx similarity index 96% rename from src/components/SignInButtons/AppleSignIn/index.desktop.js rename to src/components/SignInButtons/AppleSignIn/index.desktop.tsx index cc7ae5b623a5..15d168f45d2e 100644 --- a/src/components/SignInButtons/AppleSignIn/index.desktop.js +++ b/src/components/SignInButtons/AppleSignIn/index.desktop.tsx @@ -10,7 +10,7 @@ const appleSignInWebRouteForDesktopFlow = `${CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL} /** * Apple Sign In button for desktop flow - * @returns {React.Component} + * @returns React Component */ function AppleSignIn() { const styles = useThemeStyles(); diff --git a/src/components/SignInButtons/AppleSignIn/index.ios.js b/src/components/SignInButtons/AppleSignIn/index.ios.tsx similarity index 94% rename from src/components/SignInButtons/AppleSignIn/index.ios.js rename to src/components/SignInButtons/AppleSignIn/index.ios.tsx index f5c6333dcf7b..ea97c9605420 100644 --- a/src/components/SignInButtons/AppleSignIn/index.ios.js +++ b/src/components/SignInButtons/AppleSignIn/index.ios.tsx @@ -7,7 +7,7 @@ import CONST from '@src/CONST'; /** * Apple Sign In method for iOS that returns identityToken. - * @returns {Promise} + * @returns Promise that returns a string when resolved */ function appleSignInRequest() { return appleAuth @@ -20,7 +20,7 @@ function appleSignInRequest() { .then((response) => appleAuth.getCredentialStateForUser(response.user).then((credentialState) => { if (credentialState !== appleAuth.State.AUTHORIZED) { - Log.alert('[Apple Sign In] Authentication failed. Original response: ', response); + Log.alert('[Apple Sign In] Authentication failed. Original response: ', {response}); throw new Error('Authentication failed'); } return response.identityToken; @@ -30,7 +30,7 @@ function appleSignInRequest() { /** * Apple Sign In button for iOS. - * @returns {React.Component} + * @returns */ function AppleSignIn() { const handleSignIn = () => { diff --git a/src/components/SignInButtons/AppleSignIn/index.website.js b/src/components/SignInButtons/AppleSignIn/index.website.tsx similarity index 77% rename from src/components/SignInButtons/AppleSignIn/index.website.js rename to src/components/SignInButtons/AppleSignIn/index.website.tsx index adae0a691e13..a1b46995bf5a 100644 --- a/src/components/SignInButtons/AppleSignIn/index.website.js +++ b/src/components/SignInButtons/AppleSignIn/index.website.tsx @@ -1,36 +1,27 @@ import get from 'lodash/get'; -import PropTypes from 'prop-types'; import React, {useEffect, useState} from 'react'; -import Config from 'react-native-config'; +import Config, {NativeConfig} from 'react-native-config'; import getUserLanguage from '@components/SignInButtons/GetUserLanguage'; import withNavigationFocus from '@components/withNavigationFocus'; import Log from '@libs/Log'; import * as Session from '@userActions/Session'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; +import {AppleIDSignInOnFailureEvent, AppleIDSignInOnSuccessEvent} from '@src/types/modules/dom'; // react-native-config doesn't trim whitespace on iOS for some reason so we // add a trim() call to lodashGet here to prevent headaches. -const lodashGet = (config, key, defaultValue) => get(config, key, defaultValue).trim(); +const lodashGet = (config: NativeConfig, key: string, defaultValue: string) => get(config, key, defaultValue).trim(); -const requiredPropTypes = { - isDesktopFlow: PropTypes.bool.isRequired, +type AppleSignInDivProps = { + isDesktopFlow: boolean; }; -const singletonPropTypes = { - ...requiredPropTypes, - - // From withNavigationFocus - isFocused: PropTypes.bool.isRequired, +type SingletonAppleSignInButtonProps = AppleSignInDivProps & { + isFocused: boolean; }; -const propTypes = { - // Prop to indicate if this is the desktop flow or not. - isDesktopFlow: PropTypes.bool, -}; -const defaultProps = { - isDesktopFlow: false, -}; +type AppleSignInProps = AppleSignInDivProps; /** * Apple Sign In Configuration for Web. @@ -49,23 +40,23 @@ const config = { * Apple Sign In success and failure listeners. */ -const successListener = (event) => { +const successListener = (event: AppleIDSignInOnSuccessEvent) => { const token = event.detail.authorization.id_token; Session.beginAppleSignIn(token); }; -const failureListener = (event) => { +const failureListener = (event: AppleIDSignInOnFailureEvent) => { if (!event.detail || event.detail.error === 'popup_closed_by_user') { return null; } - Log.warn(`Apple sign-in failed: ${event.detail}`); + Log.warn(`Apple sign-in failed: ${event.detail.error}`); }; /** * Apple Sign In button for Web. - * @returns {React.Component} + * @returns React Component */ -function AppleSignInDiv({isDesktopFlow}) { +function AppleSignInDiv({isDesktopFlow = false}: AppleSignInDivProps) { useEffect(() => { // `init` renders the button, so it must be called after the div is // first mounted. @@ -108,24 +99,20 @@ function AppleSignInDiv({isDesktopFlow}) { ); } -AppleSignInDiv.propTypes = requiredPropTypes; - // The Sign in with Apple script may fail to render button if there are multiple // of these divs present in the app, as it matches based on div id. So we'll // only mount the div when it should be visible. -function SingletonAppleSignInButton({isFocused, isDesktopFlow}) { +function SingletonAppleSignInButton({isFocused, isDesktopFlow}: SingletonAppleSignInButtonProps) { if (!isFocused) { return null; } return ; } -SingletonAppleSignInButton.propTypes = singletonPropTypes; - // withNavigationFocus is used to only render the button when it is visible. const SingletonAppleSignInButtonWithFocus = withNavigationFocus(SingletonAppleSignInButton); -function AppleSignIn({isDesktopFlow}) { +function AppleSignIn({isDesktopFlow = false}: AppleSignInProps) { const [scriptLoaded, setScriptLoaded] = useState(false); useEffect(() => { if (window.appleAuthScriptLoaded) { @@ -148,7 +135,4 @@ function AppleSignIn({isDesktopFlow}) { return ; } -AppleSignIn.propTypes = propTypes; -AppleSignIn.defaultProps = defaultProps; - -export default withNavigationFocus(AppleSignIn); +export default AppleSignIn; diff --git a/src/components/SignInButtons/GetUserLanguage.js b/src/components/SignInButtons/GetUserLanguage.ts similarity index 78% rename from src/components/SignInButtons/GetUserLanguage.js rename to src/components/SignInButtons/GetUserLanguage.ts index 7f45f1fa1e89..176f23dfef58 100644 --- a/src/components/SignInButtons/GetUserLanguage.js +++ b/src/components/SignInButtons/GetUserLanguage.ts @@ -5,7 +5,7 @@ const localeCodes = { const GetUserLanguage = () => { const userLanguage = navigator.language || navigator.userLanguage; - const languageCode = userLanguage.split('-')[0]; + const languageCode = userLanguage.split('-')[0] as keyof typeof localeCodes; return localeCodes[languageCode] || 'en_US'; }; diff --git a/src/components/SignInButtons/GoogleSignIn/index.desktop.js b/src/components/SignInButtons/GoogleSignIn/index.desktop.tsx similarity index 97% rename from src/components/SignInButtons/GoogleSignIn/index.desktop.js rename to src/components/SignInButtons/GoogleSignIn/index.desktop.tsx index 9284a5332e3d..8ad9830d7325 100644 --- a/src/components/SignInButtons/GoogleSignIn/index.desktop.js +++ b/src/components/SignInButtons/GoogleSignIn/index.desktop.tsx @@ -13,7 +13,7 @@ const googleSignInWebRouteForDesktopFlow = `${CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL /** * Google Sign In button for desktop flow. - * @returns {React.Component} + * @returns React Component */ function GoogleSignIn() { const styles = useThemeStyles(); diff --git a/src/components/SignInButtons/GoogleSignIn/index.native.js b/src/components/SignInButtons/GoogleSignIn/index.native.tsx similarity index 98% rename from src/components/SignInButtons/GoogleSignIn/index.native.js rename to src/components/SignInButtons/GoogleSignIn/index.native.tsx index c7ac763cfb73..85621e5ee2ba 100644 --- a/src/components/SignInButtons/GoogleSignIn/index.native.js +++ b/src/components/SignInButtons/GoogleSignIn/index.native.tsx @@ -43,7 +43,7 @@ function googleSignInRequest() { /** * Google Sign In button for iOS. - * @returns {React.Component} + * @returns React Component */ function GoogleSignIn() { return ( diff --git a/src/components/SignInButtons/GoogleSignIn/index.website.js b/src/components/SignInButtons/GoogleSignIn/index.website.tsx similarity index 85% rename from src/components/SignInButtons/GoogleSignIn/index.website.js rename to src/components/SignInButtons/GoogleSignIn/index.website.tsx index 8f8a977bdb09..21584784a792 100644 --- a/src/components/SignInButtons/GoogleSignIn/index.website.js +++ b/src/components/SignInButtons/GoogleSignIn/index.website.tsx @@ -1,28 +1,24 @@ -import PropTypes from 'prop-types'; import React, {useCallback} from 'react'; import {View} from 'react-native'; -import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; +import withLocalize, {WithLocalizeProps} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import * as Session from '@userActions/Session'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; -const propTypes = { - /** Whether we're rendering in the Desktop Flow, if so show a different button. */ - isDesktopFlow: PropTypes.bool, - - ...withLocalizePropTypes, -}; - -const defaultProps = { - isDesktopFlow: false, +type GoogleSignInProps = WithLocalizeProps & { + isDesktopFlow: boolean; }; /** Div IDs for styling the two different Google Sign-In buttons. */ const mainId = 'google-sign-in-main'; const desktopId = 'google-sign-in-desktop'; -const signIn = (response) => { +type Response = { + credential: string; +}; + +const signIn = (response: Response) => { Session.beginGoogleSignIn(response.credential); }; @@ -31,12 +27,14 @@ const signIn = (response) => { * We have to load the gis script and then determine if the page is focused before rendering the button. * @returns {React.Component} */ -function GoogleSignIn({translate, isDesktopFlow}) { + +function GoogleSignIn({translate, isDesktopFlow = false}: GoogleSignInProps) { const styles = useThemeStyles(); const loadScript = useCallback(() => { const google = window.google; if (google) { google.accounts.id.initialize({ + // eslint-disable-next-line @typescript-eslint/naming-convention client_id: CONFIG.GOOGLE_SIGN_IN.WEB_CLIENT_ID, callback: signIn, }); @@ -92,7 +90,5 @@ function GoogleSignIn({translate, isDesktopFlow}) { } GoogleSignIn.displayName = 'GoogleSignIn'; -GoogleSignIn.propTypes = propTypes; -GoogleSignIn.defaultProps = defaultProps; export default withLocalize(GoogleSignIn); diff --git a/src/components/SignInButtons/IconButton.js b/src/components/SignInButtons/IconButton.tsx similarity index 63% rename from src/components/SignInButtons/IconButton.js rename to src/components/SignInButtons/IconButton.tsx index 19a5bd9b27b8..bcbf562935e5 100644 --- a/src/components/SignInButtons/IconButton.js +++ b/src/components/SignInButtons/IconButton.tsx @@ -1,38 +1,29 @@ -import PropTypes from 'prop-types'; import React from 'react'; import Icon from '@components/Icon'; import * as Expensicons from '@components/Icon/Expensicons'; import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback'; -import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; +import withLocalize, {WithLocalizeProps} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import CONST from '@src/CONST'; - -const propTypes = { - /** The on press method */ - onPress: PropTypes.func, - - /** Which provider you are using to sign in */ - provider: PropTypes.string.isRequired, - - ...withLocalizePropTypes, -}; - -const defaultProps = { - onPress: () => {}, -}; +import {TranslationPaths} from '@src/languages/types'; const providerData = { [CONST.SIGN_IN_METHOD.APPLE]: { icon: Expensicons.AppleLogo, - accessibilityLabel: 'common.signInWithApple', + accessibilityLabel: 'common.signInWithApple' as TranslationPaths, }, [CONST.SIGN_IN_METHOD.GOOGLE]: { icon: Expensicons.GoogleLogo, - accessibilityLabel: 'common.signInWithGoogle', + accessibilityLabel: 'common.signInWithGoogle' as TranslationPaths, }, }; -function IconButton({onPress, translate, provider}) { +type IconButtonProps = WithLocalizeProps & { + onPress: () => void; + provider: keyof typeof providerData; +}; + +function IconButton({onPress, provider, translate}: IconButtonProps) { const styles = useThemeStyles(); return ( | null; }; @@ -313,11 +313,11 @@ function beginAppleSignIn(idToken: string) { * Shows Google sign-in process, and if an auth token is successfully obtained, * passes the token on to the Expensify API to sign in with */ -function beginGoogleSignIn(token: string) { +function beginGoogleSignIn(token: string | null) { const {optimisticData, successData, failureData} = signInAttemptState(); type BeginGoogleSignInParams = { - token: string; + token: string | null; preferredLocale: ValueOf | null; }; diff --git a/src/types/modules/appleAuth.d.ts b/src/types/modules/appleAuth.d.ts new file mode 100644 index 000000000000..5c06d5625ea1 --- /dev/null +++ b/src/types/modules/appleAuth.d.ts @@ -0,0 +1,29 @@ +type ClientConfigI = { + clientId?: string | undefined; + redirectURI?: string | undefined; + scope?: string | undefined; + state?: string | undefined; + nonce?: string | undefined; + usePopup?: boolean | undefined; +}; + +type AuthI = { + init: (config: ClientConfigI) => void; + signIn: (signInConfig?: ClientConfigI) => Promise; + renderButton: () => void; +}; + +type AppleID = { + auth: AuthI; +}; + +declare global { + // eslint-disable-next-line @typescript-eslint/consistent-type-definitions + interface Window { + AppleID: AppleID; + appleAuthScriptLoaded: boolean; + } +} + +// We used the export {} line to mark this file as an external module +export {}; diff --git a/src/types/modules/dom.d.ts b/src/types/modules/dom.d.ts new file mode 100644 index 000000000000..c4c60e1c5016 --- /dev/null +++ b/src/types/modules/dom.d.ts @@ -0,0 +1,31 @@ +// eslint-disable-next-line rulesdir/no-inline-named-export +export type AppleIDSignInOnSuccessEvent = { + detail: { + authorization: { + // eslint-disable-next-line @typescript-eslint/naming-convention + id_token: string; + }; + }; +}; + +// eslint-disable-next-line rulesdir/no-inline-named-export +export type AppleIDSignInOnFailureEvent = { + detail: { + error: string; + }; +}; + +type AppleAuthEventMap = { + AppleIDSignInOnSuccess: AppleIDSignInOnSuccessEvent; + AppleIDSignInOnFailure: AppleIDSignInOnFailureEvent; +}; + +declare global { + // eslint-disable-next-line @typescript-eslint/consistent-type-definitions + interface Document { + addEventListener(type: K, listener: (this: Document, ev: CustomEventMap[K]) => void): void; + removeEventListener(type: K, listener: (this: Document, ev: CustomEventMap[K]) => void): void; + dispatchEvent(ev: CustomEventMap[K]): void; + } +} +export {}; diff --git a/src/types/modules/google.d.ts b/src/types/modules/google.d.ts new file mode 100644 index 000000000000..b3aa1e745467 --- /dev/null +++ b/src/types/modules/google.d.ts @@ -0,0 +1,36 @@ +type Response = { + credential: string; +}; + +type Initialize = { + // eslint-disable-next-line @typescript-eslint/naming-convention + client_id: string; + callback: (response: Response) => void; +}; + +type Options = { + theme?: 'outline'; + size?: 'large'; + type?: 'standard' | 'icon'; + shape?: 'circle' | 'pill'; + width?: string; +}; + +type Google = { + accounts: { + id: { + initialize: ({client_id, callback}: Initialize) => void; + renderButton: (client_id, options: Options) => void; + }; + }; +}; + +declare global { + // eslint-disable-next-line @typescript-eslint/consistent-type-definitions + interface Window { + google: Google; + } +} + +// We used the export {} line to mark this file as an external module +export {}; diff --git a/src/types/modules/navigator.d.ts b/src/types/modules/navigator.d.ts new file mode 100644 index 000000000000..aca32a543b67 --- /dev/null +++ b/src/types/modules/navigator.d.ts @@ -0,0 +1,9 @@ +declare global { + // eslint-disable-next-line @typescript-eslint/consistent-type-definitions + interface Navigator { + userLanguage: string; + } +} + +// We used the export {} line to mark this file as an external module +export {}; From 5e38df37e9603de6a30306a2e1d820314bff8d1c Mon Sep 17 00:00:00 2001 From: Shahe Shahinyan Date: Thu, 21 Dec 2023 19:04:57 +0400 Subject: [PATCH 2/4] Fix Review Comments --- .../{index.ios.ts => index.ios.tsx} | 2 -- .../AppleAuthWrapper/{index.ts => index.tsx} | 0 .../AppleSignIn/index.android.tsx | 3 +-- .../AppleSignIn/index.desktop.tsx | 1 - .../SignInButtons/AppleSignIn/index.ios.tsx | 3 +-- .../AppleSignIn/index.website.tsx | 19 ++++++------- .../SignInButtons/GetUserLanguage.ts | 11 +++++--- .../GoogleSignIn/index.desktop.tsx | 7 +---- .../GoogleSignIn/index.native.tsx | 1 - .../GoogleSignIn/index.website.tsx | 16 +++++------ src/components/SignInButtons/IconButton.tsx | 27 ++++++++++++------- src/components/withNavigationFocus.tsx | 2 ++ src/types/modules/appleAuth.d.ts | 22 +++++++-------- src/types/modules/dom.d.ts | 21 +++++---------- src/types/modules/google.d.ts | 3 +-- 15 files changed, 67 insertions(+), 71 deletions(-) rename src/components/SignInButtons/AppleAuthWrapper/{index.ios.ts => index.ios.tsx} (96%) rename src/components/SignInButtons/AppleAuthWrapper/{index.ts => index.tsx} (100%) diff --git a/src/components/SignInButtons/AppleAuthWrapper/index.ios.ts b/src/components/SignInButtons/AppleAuthWrapper/index.ios.tsx similarity index 96% rename from src/components/SignInButtons/AppleAuthWrapper/index.ios.ts rename to src/components/SignInButtons/AppleAuthWrapper/index.ios.tsx index 0622ca515ce4..12ead0267db3 100644 --- a/src/components/SignInButtons/AppleAuthWrapper/index.ios.ts +++ b/src/components/SignInButtons/AppleAuthWrapper/index.ios.tsx @@ -5,8 +5,6 @@ import * as Session from '@userActions/Session'; /** * Apple Sign In wrapper for iOS * revokes the session if the credential is revoked. - * - * @returns null */ function AppleAuthWrapper() { useEffect(() => { diff --git a/src/components/SignInButtons/AppleAuthWrapper/index.ts b/src/components/SignInButtons/AppleAuthWrapper/index.tsx similarity index 100% rename from src/components/SignInButtons/AppleAuthWrapper/index.ts rename to src/components/SignInButtons/AppleAuthWrapper/index.tsx diff --git a/src/components/SignInButtons/AppleSignIn/index.android.tsx b/src/components/SignInButtons/AppleSignIn/index.android.tsx index ac84987afefa..cfd1c48ee8b5 100644 --- a/src/components/SignInButtons/AppleSignIn/index.android.tsx +++ b/src/components/SignInButtons/AppleSignIn/index.android.tsx @@ -20,7 +20,7 @@ const config = { * Apple Sign In method for Android that returns authToken. * @returns Promise that returns a string when resolved */ -function appleSignInRequest() { +function appleSignInRequest(): Promise { appleAuthAndroid.configure(config); return appleAuthAndroid .signIn() @@ -32,7 +32,6 @@ function appleSignInRequest() { /** * Apple Sign In button for Android. - * @returns */ function AppleSignIn() { const handleSignIn = () => { diff --git a/src/components/SignInButtons/AppleSignIn/index.desktop.tsx b/src/components/SignInButtons/AppleSignIn/index.desktop.tsx index 15d168f45d2e..792c16ed0b4a 100644 --- a/src/components/SignInButtons/AppleSignIn/index.desktop.tsx +++ b/src/components/SignInButtons/AppleSignIn/index.desktop.tsx @@ -10,7 +10,6 @@ const appleSignInWebRouteForDesktopFlow = `${CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL} /** * Apple Sign In button for desktop flow - * @returns React Component */ function AppleSignIn() { const styles = useThemeStyles(); diff --git a/src/components/SignInButtons/AppleSignIn/index.ios.tsx b/src/components/SignInButtons/AppleSignIn/index.ios.tsx index ea97c9605420..06a8878f4788 100644 --- a/src/components/SignInButtons/AppleSignIn/index.ios.tsx +++ b/src/components/SignInButtons/AppleSignIn/index.ios.tsx @@ -9,7 +9,7 @@ import CONST from '@src/CONST'; * Apple Sign In method for iOS that returns identityToken. * @returns Promise that returns a string when resolved */ -function appleSignInRequest() { +function appleSignInRequest(): Promise { return appleAuth .performRequest({ requestedOperation: appleAuth.Operation.LOGIN, @@ -30,7 +30,6 @@ function appleSignInRequest() { /** * Apple Sign In button for iOS. - * @returns */ function AppleSignIn() { const handleSignIn = () => { diff --git a/src/components/SignInButtons/AppleSignIn/index.website.tsx b/src/components/SignInButtons/AppleSignIn/index.website.tsx index a1b46995bf5a..f256330c2344 100644 --- a/src/components/SignInButtons/AppleSignIn/index.website.tsx +++ b/src/components/SignInButtons/AppleSignIn/index.website.tsx @@ -1,8 +1,7 @@ -import get from 'lodash/get'; import React, {useEffect, useState} from 'react'; import Config, {NativeConfig} from 'react-native-config'; import getUserLanguage from '@components/SignInButtons/GetUserLanguage'; -import withNavigationFocus from '@components/withNavigationFocus'; +import withNavigationFocus, {WithNavigationFocusProps} from '@components/withNavigationFocus'; import Log from '@libs/Log'; import * as Session from '@userActions/Session'; import CONFIG from '@src/CONFIG'; @@ -11,7 +10,7 @@ import {AppleIDSignInOnFailureEvent, AppleIDSignInOnSuccessEvent} from '@src/typ // react-native-config doesn't trim whitespace on iOS for some reason so we // add a trim() call to lodashGet here to prevent headaches. -const lodashGet = (config: NativeConfig, key: string, defaultValue: string) => get(config, key, defaultValue).trim(); +const getConfig = (config: NativeConfig, key: string, defaultValue: string) => (config?.[key] ?? defaultValue).trim(); type AppleSignInDivProps = { isDesktopFlow: boolean; @@ -21,16 +20,18 @@ type SingletonAppleSignInButtonProps = AppleSignInDivProps & { isFocused: boolean; }; -type AppleSignInProps = AppleSignInDivProps; +type AppleSignInProps = WithNavigationFocusProps & { + isDesktopFlow?: boolean; +}; /** * Apple Sign In Configuration for Web. */ const config = { - clientId: lodashGet(Config, 'ASI_CLIENTID_OVERRIDE', CONFIG.APPLE_SIGN_IN.SERVICE_ID), + clientId: getConfig(Config, 'ASI_CLIENTID_OVERRIDE', CONFIG.APPLE_SIGN_IN.SERVICE_ID), scope: 'name email', // never used, but required for configuration - redirectURI: lodashGet(Config, 'ASI_REDIRECTURI_OVERRIDE', CONFIG.APPLE_SIGN_IN.REDIRECT_URI), + redirectURI: getConfig(Config, 'ASI_REDIRECTURI_OVERRIDE', CONFIG.APPLE_SIGN_IN.REDIRECT_URI), state: '', nonce: '', usePopup: true, @@ -54,9 +55,8 @@ const failureListener = (event: AppleIDSignInOnFailureEvent) => { /** * Apple Sign In button for Web. - * @returns React Component */ -function AppleSignInDiv({isDesktopFlow = false}: AppleSignInDivProps) { +function AppleSignInDiv({isDesktopFlow}: AppleSignInDivProps) { useEffect(() => { // `init` renders the button, so it must be called after the div is // first mounted. @@ -135,4 +135,5 @@ function AppleSignIn({isDesktopFlow = false}: AppleSignInProps) { return ; } -export default AppleSignIn; +AppleSignIn.displayName = 'AppleSignIn'; +export default withNavigationFocus(AppleSignIn); diff --git a/src/components/SignInButtons/GetUserLanguage.ts b/src/components/SignInButtons/GetUserLanguage.ts index 176f23dfef58..611ba415008b 100644 --- a/src/components/SignInButtons/GetUserLanguage.ts +++ b/src/components/SignInButtons/GetUserLanguage.ts @@ -1,11 +1,16 @@ +import {ValueOf} from 'type-fest'; + const localeCodes = { en: 'en_US', es: 'es_ES', -}; +} as const; + +type LanguageCode = keyof typeof localeCodes; +type LocaleCode = ValueOf; -const GetUserLanguage = () => { +const GetUserLanguage = (): LocaleCode => { const userLanguage = navigator.language || navigator.userLanguage; - const languageCode = userLanguage.split('-')[0] as keyof typeof localeCodes; + const languageCode = userLanguage.split('-')[0] as LanguageCode; return localeCodes[languageCode] || 'en_US'; }; diff --git a/src/components/SignInButtons/GoogleSignIn/index.desktop.tsx b/src/components/SignInButtons/GoogleSignIn/index.desktop.tsx index 8ad9830d7325..3c2abb1679f0 100644 --- a/src/components/SignInButtons/GoogleSignIn/index.desktop.tsx +++ b/src/components/SignInButtons/GoogleSignIn/index.desktop.tsx @@ -1,19 +1,15 @@ import React from 'react'; import {View} from 'react-native'; import IconButton from '@components/SignInButtons/IconButton'; -import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ROUTES from '@src/ROUTES'; -const propTypes = {...withLocalizePropTypes}; - const googleSignInWebRouteForDesktopFlow = `${CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL}${ROUTES.GOOGLE_SIGN_IN}`; /** * Google Sign In button for desktop flow. - * @returns React Component */ function GoogleSignIn() { const styles = useThemeStyles(); @@ -30,6 +26,5 @@ function GoogleSignIn() { } GoogleSignIn.displayName = 'GoogleSignIn'; -GoogleSignIn.propTypes = propTypes; -export default withLocalize(GoogleSignIn); +export default GoogleSignIn; diff --git a/src/components/SignInButtons/GoogleSignIn/index.native.tsx b/src/components/SignInButtons/GoogleSignIn/index.native.tsx index 85621e5ee2ba..2744d8958080 100644 --- a/src/components/SignInButtons/GoogleSignIn/index.native.tsx +++ b/src/components/SignInButtons/GoogleSignIn/index.native.tsx @@ -43,7 +43,6 @@ function googleSignInRequest() { /** * Google Sign In button for iOS. - * @returns React Component */ function GoogleSignIn() { return ( diff --git a/src/components/SignInButtons/GoogleSignIn/index.website.tsx b/src/components/SignInButtons/GoogleSignIn/index.website.tsx index 21584784a792..5d419c8744e5 100644 --- a/src/components/SignInButtons/GoogleSignIn/index.website.tsx +++ b/src/components/SignInButtons/GoogleSignIn/index.website.tsx @@ -1,23 +1,20 @@ import React, {useCallback} from 'react'; import {View} from 'react-native'; -import withLocalize, {WithLocalizeProps} from '@components/withLocalize'; +import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import * as Session from '@userActions/Session'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; +import Response from '@src/types/modules/google'; -type GoogleSignInProps = WithLocalizeProps & { - isDesktopFlow: boolean; +type GoogleSignInProps = { + isDesktopFlow?: boolean; }; /** Div IDs for styling the two different Google Sign-In buttons. */ const mainId = 'google-sign-in-main'; const desktopId = 'google-sign-in-desktop'; -type Response = { - credential: string; -}; - const signIn = (response: Response) => { Session.beginGoogleSignIn(response.credential); }; @@ -28,7 +25,8 @@ const signIn = (response: Response) => { * @returns {React.Component} */ -function GoogleSignIn({translate, isDesktopFlow = false}: GoogleSignInProps) { +function GoogleSignIn({isDesktopFlow = false}: GoogleSignInProps) { + const {translate} = useLocalize(); const styles = useThemeStyles(); const loadScript = useCallback(() => { const google = window.google; @@ -91,4 +89,4 @@ function GoogleSignIn({translate, isDesktopFlow = false}: GoogleSignInProps) { GoogleSignIn.displayName = 'GoogleSignIn'; -export default withLocalize(GoogleSignIn); +export default GoogleSignIn; diff --git a/src/components/SignInButtons/IconButton.tsx b/src/components/SignInButtons/IconButton.tsx index bcbf562935e5..e0c72c8d6292 100644 --- a/src/components/SignInButtons/IconButton.tsx +++ b/src/components/SignInButtons/IconButton.tsx @@ -1,8 +1,10 @@ import React from 'react'; +import {SvgProps} from 'react-native-svg'; +import {ValueOf} from 'type-fest'; import Icon from '@components/Icon'; import * as Expensicons from '@components/Icon/Expensicons'; import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback'; -import withLocalize, {WithLocalizeProps} from '@components/withLocalize'; +import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import CONST from '@src/CONST'; import {TranslationPaths} from '@src/languages/types'; @@ -10,20 +12,27 @@ import {TranslationPaths} from '@src/languages/types'; const providerData = { [CONST.SIGN_IN_METHOD.APPLE]: { icon: Expensicons.AppleLogo, - accessibilityLabel: 'common.signInWithApple' as TranslationPaths, + accessibilityLabel: 'common.signInWithApple', }, [CONST.SIGN_IN_METHOD.GOOGLE]: { icon: Expensicons.GoogleLogo, - accessibilityLabel: 'common.signInWithGoogle' as TranslationPaths, + accessibilityLabel: 'common.signInWithGoogle', }, -}; +} satisfies Record< + ValueOf, + { + icon: React.FC; + accessibilityLabel: TranslationPaths; + } +>; -type IconButtonProps = WithLocalizeProps & { - onPress: () => void; - provider: keyof typeof providerData; +type IconButtonProps = { + onPress?: () => void; + provider: ValueOf; }; -function IconButton({onPress, provider, translate}: IconButtonProps) { +function IconButton({onPress = () => {}, provider}: IconButtonProps) { + const {translate} = useLocalize(); const styles = useThemeStyles(); return ( void; - signIn: (signInConfig?: ClientConfigI) => Promise; +type Auth = { + init: (config: ClientConfig) => void; + signIn: (signInConfig?: ClientConfig) => Promise; renderButton: () => void; }; type AppleID = { - auth: AuthI; + auth: Auth; }; declare global { diff --git a/src/types/modules/dom.d.ts b/src/types/modules/dom.d.ts index c4c60e1c5016..60bd9c9ae983 100644 --- a/src/types/modules/dom.d.ts +++ b/src/types/modules/dom.d.ts @@ -1,5 +1,4 @@ -// eslint-disable-next-line rulesdir/no-inline-named-export -export type AppleIDSignInOnSuccessEvent = { +type AppleIDSignInOnSuccessEvent = { detail: { authorization: { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -8,24 +7,18 @@ export type AppleIDSignInOnSuccessEvent = { }; }; -// eslint-disable-next-line rulesdir/no-inline-named-export -export type AppleIDSignInOnFailureEvent = { +type AppleIDSignInOnFailureEvent = { detail: { error: string; }; }; -type AppleAuthEventMap = { - AppleIDSignInOnSuccess: AppleIDSignInOnSuccessEvent; - AppleIDSignInOnFailure: AppleIDSignInOnFailureEvent; -}; - declare global { // eslint-disable-next-line @typescript-eslint/consistent-type-definitions - interface Document { - addEventListener(type: K, listener: (this: Document, ev: CustomEventMap[K]) => void): void; - removeEventListener(type: K, listener: (this: Document, ev: CustomEventMap[K]) => void): void; - dispatchEvent(ev: CustomEventMap[K]): void; + interface DocumentEventMap extends GlobalEventHandlersEventMap { + AppleIDSignInOnSuccess: AppleIDSignInOnSuccessEvent; + AppleIDSignInOnFailure: AppleIDSignInOnFailureEvent; } } -export {}; + +export type {AppleIDSignInOnFailureEvent, AppleIDSignInOnSuccessEvent}; diff --git a/src/types/modules/google.d.ts b/src/types/modules/google.d.ts index b3aa1e745467..3c29e62bf9b3 100644 --- a/src/types/modules/google.d.ts +++ b/src/types/modules/google.d.ts @@ -32,5 +32,4 @@ declare global { } } -// We used the export {} line to mark this file as an external module -export {}; +export default Response; From c2fcaf49a9053bc4107ff264e98b6d2e98c1aeb1 Mon Sep 17 00:00:00 2001 From: Shahe Shahinyan Date: Thu, 28 Dec 2023 16:29:35 +0400 Subject: [PATCH 3/4] Fix Review Comments --- src/components/SignInButtons/AppleSignIn/index.ios.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/SignInButtons/AppleSignIn/index.ios.tsx b/src/components/SignInButtons/AppleSignIn/index.ios.tsx index 06a8878f4788..3fb1179d0365 100644 --- a/src/components/SignInButtons/AppleSignIn/index.ios.tsx +++ b/src/components/SignInButtons/AppleSignIn/index.ios.tsx @@ -7,7 +7,7 @@ import CONST from '@src/CONST'; /** * Apple Sign In method for iOS that returns identityToken. - * @returns Promise that returns a string when resolved + * @returns Promise that returns a string when resolved */ function appleSignInRequest(): Promise { return appleAuth From cdea74d5eb9d1b9c08c0d43d9ae0514ddac517fd Mon Sep 17 00:00:00 2001 From: Shahe Shahinyan Date: Thu, 28 Dec 2023 19:09:27 +0400 Subject: [PATCH 4/4] fix Typescript check error --- src/components/SignInButtons/IconButton.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/SignInButtons/IconButton.tsx b/src/components/SignInButtons/IconButton.tsx index e0c72c8d6292..848ca5463854 100644 --- a/src/components/SignInButtons/IconButton.tsx +++ b/src/components/SignInButtons/IconButton.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import {SvgProps} from 'react-native-svg'; import {ValueOf} from 'type-fest'; import Icon from '@components/Icon'; import * as Expensicons from '@components/Icon/Expensicons'; @@ -8,6 +7,7 @@ import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import CONST from '@src/CONST'; import {TranslationPaths} from '@src/languages/types'; +import IconAsset from '@src/types/utils/IconAsset'; const providerData = { [CONST.SIGN_IN_METHOD.APPLE]: { @@ -21,7 +21,7 @@ const providerData = { } satisfies Record< ValueOf, { - icon: React.FC; + icon: IconAsset; accessibilityLabel: TranslationPaths; } >;