diff --git a/lib/utils/farcaster-utils.js b/lib/utils/farcaster-utils.js --- a/lib/utils/farcaster-utils.js +++ b/lib/utils/farcaster-utils.js @@ -1,7 +1,12 @@ // @flow +import invariant from 'invariant'; +import * as React from 'react'; + +import { setSyncedMetadataEntryActionType } from '../actions/synced-metadata-actions.js'; +import { IdentityClientContext } from '../shared/identity-client-context.js'; import { syncedMetadataNames } from '../types/synced-metadata-types.js'; -import { useSelector } from '../utils/redux-utils.js'; +import { useSelector, useDispatch } from '../utils/redux-utils.js'; function useCurrentUserFID(): ?string { return useSelector( @@ -12,4 +17,29 @@ ); } -export { useCurrentUserFID }; +function useLinkFID(): (fid: string) => Promise { + const identityClientContext = React.useContext(IdentityClientContext); + invariant(identityClientContext, 'identityClientContext should be set'); + + const { identityClient } = identityClientContext; + const { linkFarcasterAccount } = identityClient; + + const dispatch = useDispatch(); + + return React.useCallback( + async (fid: string) => { + await linkFarcasterAccount(fid); + + dispatch({ + type: setSyncedMetadataEntryActionType, + payload: { + name: syncedMetadataNames.CURRENT_USER_FID, + data: fid, + }, + }); + }, + [dispatch, linkFarcasterAccount], + ); +} + +export { useCurrentUserFID, useLinkFID }; diff --git a/native/account/registration/registration-server-call.js b/native/account/registration/registration-server-call.js --- a/native/account/registration/registration-server-call.js +++ b/native/account/registration/registration-server-call.js @@ -3,7 +3,6 @@ import * as React from 'react'; import { setDataLoadedActionType } from 'lib/actions/client-db-store-actions.js'; -import { setSyncedMetadataEntryActionType } from 'lib/actions/synced-metadata-actions.js'; import { legacyKeyserverRegisterActionTypes, legacyKeyserverRegister, @@ -19,7 +18,7 @@ type LegacyLogInStartingPayload, logInActionSources, } from 'lib/types/account-types.js'; -import { syncedMetadataNames } from 'lib/types/synced-metadata-types.js'; +import { useLinkFID } from 'lib/utils/farcaster-utils.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; import { usingCommServicesAccessToken } from 'lib/utils/services-utils.js'; @@ -202,6 +201,8 @@ const legacySiweServerCall = useLegacySIWEServerCall(); const identityWalletRegisterCall = useIdentityWalletRegisterCall(); const dispatch = useDispatch(); + const linkFID = useLinkFID(); + const returnedFunc = React.useCallback( (input: RegistrationServerCallInput) => new Promise( @@ -265,13 +266,7 @@ payload: keyserverURL, }); if (farcasterID) { - dispatch({ - type: setSyncedMetadataEntryActionType, - payload: { - name: syncedMetadataNames.CURRENT_USER_FID, - data: farcasterID, - }, - }); + await linkFID(farcasterID); } if (siweBackupSecrets) { await commCoreModule.setSIWEBackupSecrets(siweBackupSecrets); @@ -297,12 +292,13 @@ }, ), [ - currentStep, + currentStep.step, + dispatch, legacyKeyserverRegisterUsernameAccount, identityRegisterUsernameAccount, legacySiweServerCall, - dispatch, identityWalletRegisterCall, + linkFID, ], ); diff --git a/native/components/connect-farcaster-bottom-sheet.react.js b/native/components/connect-farcaster-bottom-sheet.react.js --- a/native/components/connect-farcaster-bottom-sheet.react.js +++ b/native/components/connect-farcaster-bottom-sheet.react.js @@ -5,11 +5,8 @@ import { View, StyleSheet } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; -import { setSyncedMetadataEntryActionType } from 'lib/actions/synced-metadata-actions.js'; import { useIsAppForegrounded } from 'lib/shared/lifecycle-utils.js'; -import { syncedMetadataNames } from 'lib/types/synced-metadata-types.js'; -import { useCurrentUserFID } from 'lib/utils/farcaster-utils.js'; -import { useDispatch } from 'lib/utils/redux-utils.js'; +import { useCurrentUserFID, useLinkFID } from 'lib/utils/farcaster-utils.js'; import FarcasterPrompt from './farcaster-prompt.react.js'; import FarcasterWebView, { @@ -34,21 +31,15 @@ function ConnectFarcasterBottomSheet(props: Props): React.Node { const { navigation } = props; - const dispatch = useDispatch(); - const fid = useCurrentUserFID(); + const linkFID = useLinkFID(); + const onSuccess = React.useCallback( - (newFID: string) => { - dispatch({ - type: setSyncedMetadataEntryActionType, - payload: { - name: syncedMetadataNames.CURRENT_USER_FID, - data: newFID, - }, - }); + async (newFID: string) => { + await linkFID(newFID); }, - [dispatch], + [linkFID], ); const { goBack } = navigation; diff --git a/native/profile/farcaster-account-settings.react.js b/native/profile/farcaster-account-settings.react.js --- a/native/profile/farcaster-account-settings.react.js +++ b/native/profile/farcaster-account-settings.react.js @@ -3,12 +3,9 @@ import * as React from 'react'; import { View } from 'react-native'; -import { - setSyncedMetadataEntryActionType, - clearSyncedMetadataEntryActionType, -} from 'lib/actions/synced-metadata-actions.js'; +import { clearSyncedMetadataEntryActionType } from 'lib/actions/synced-metadata-actions.js'; import { syncedMetadataNames } from 'lib/types/synced-metadata-types.js'; -import { useCurrentUserFID } from 'lib/utils/farcaster-utils.js'; +import { useCurrentUserFID, useLinkFID } from 'lib/utils/farcaster-utils.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; import type { ProfileNavigationProp } from './profile.react.js'; @@ -44,18 +41,15 @@ const [webViewState, setWebViewState] = React.useState('closed'); + const linkFID = useLinkFID(); + const onSuccess = React.useCallback( - (newFID: string) => { + async (newFID: string) => { setWebViewState('closed'); - dispatch({ - type: setSyncedMetadataEntryActionType, - payload: { - name: syncedMetadataNames.CURRENT_USER_FID, - data: newFID, - }, - }); + + await linkFID(newFID); }, - [dispatch], + [linkFID], ); const onPressConnectFarcaster = React.useCallback(() => {