From 3c1f9f7db9be51d9c90908a7d7b656db1d35be4b Mon Sep 17 00:00:00 2001 From: Ashoat Tevosyan Date: Wed, 16 Oct 2024 23:17:06 -0400 Subject: [PATCH] [lib] Factor code out for updating FID in Redux Summary: In the next diff, we'll add some code to invalidate the `UserIdentityCache` for the current user when this happens. We want to do this whenever we update the FID locally, so it makes sense to factor all those locations out first. Depends on D13735 Test Plan: Flow; this is a simple refactor Reviewers: will Reviewed By: will Subscribers: tomek Differential Revision: https://phab.comm.dev/D13736 --- .../farcaster-data-handler.react.js | 19 ++++---- lib/utils/farcaster-utils.js | 46 +++++++++++-------- .../registration/registration-server-call.js | 13 ++---- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/lib/components/farcaster-data-handler.react.js b/lib/components/farcaster-data-handler.react.js index b77d9376f9..d26a4b7c09 100644 --- a/lib/components/farcaster-data-handler.react.js +++ b/lib/components/farcaster-data-handler.react.js @@ -6,13 +6,15 @@ import { NeynarClientContext } from './neynar-client-provider.react.js'; import { useUserIdentityCache } from './user-identity-cache.react.js'; import { setAuxUserFIDsActionType } from '../actions/aux-user-actions.js'; import { updateRelationshipsActionTypes } from '../actions/relationship-actions.js'; -import { setSyncedMetadataEntryActionType } from '../actions/synced-metadata-actions.js'; import { useIsLoggedInToIdentityAndAuthoritativeKeyserver } from '../hooks/account-hooks.js'; import { useUpdateRelationships } from '../hooks/relationship-hooks.js'; import { IdentityClientContext } from '../shared/identity-client-context.js'; import { relationshipActions } from '../types/relationship-types.js'; -import { syncedMetadataNames } from '../types/synced-metadata-types.js'; -import { useCurrentUserFID, useUnlinkFID } from '../utils/farcaster-utils.js'; +import { + useCurrentUserFID, + useUnlinkFID, + useSetLocalFID, +} from '../utils/farcaster-utils.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector, useDispatch } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; @@ -147,6 +149,7 @@ function FarcasterDataHandler(props: Props): React.Node { const [fidLoaded, setFIDLoaded] = React.useState(false); + const setLocalFID = useSetLocalFID(); const handleCurrentUserFID = React.useCallback(async () => { if ( canQueryHandleCurrentUserFID === @@ -181,13 +184,7 @@ function FarcasterDataHandler(props: Props): React.Node { } if (identityFID) { - dispatch({ - type: setSyncedMetadataEntryActionType, - payload: { - name: syncedMetadataNames.CURRENT_USER_FID, - data: identityFID, - }, - }); + setLocalFID(identityFID); } setFIDLoaded(true); @@ -198,7 +195,7 @@ function FarcasterDataHandler(props: Props): React.Node { neynarClient, fid, unlinkFID, - dispatch, + setLocalFID, getCachedUserIdentity, ]); diff --git a/lib/utils/farcaster-utils.js b/lib/utils/farcaster-utils.js index cfc1556a6f..72e338728e 100644 --- a/lib/utils/farcaster-utils.js +++ b/lib/utils/farcaster-utils.js @@ -30,6 +30,25 @@ function useCurrentUserFID(): ?string { return currentUserFID; } +function useSetLocalFID(): (fid: ?string) => void { + const dispatch = useDispatch(); + return React.useCallback( + (fid: ?string) => { + // If we're unsetting the FID, we should set it to NO_FID_METADATA to + // avoid prompting the user for it again + const fidToSet = fid ?? NO_FID_METADATA; + dispatch({ + type: setSyncedMetadataEntryActionType, + payload: { + name: syncedMetadataNames.CURRENT_USER_FID, + data: fidToSet, + }, + }); + }, + [dispatch], + ); +} + function useLinkFID(): (fid: string) => Promise { const identityClientContext = React.useContext(IdentityClientContext); invariant(identityClientContext, 'identityClientContext should be set'); @@ -37,21 +56,14 @@ function useLinkFID(): (fid: string) => Promise { const { identityClient } = identityClientContext; const { linkFarcasterAccount } = identityClient; - const dispatch = useDispatch(); + const setLocalFID = useSetLocalFID(); return React.useCallback( async (fid: string) => { await linkFarcasterAccount(fid); - - dispatch({ - type: setSyncedMetadataEntryActionType, - payload: { - name: syncedMetadataNames.CURRENT_USER_FID, - data: fid, - }, - }); + setLocalFID(fid); }, - [dispatch, linkFarcasterAccount], + [setLocalFID, linkFarcasterAccount], ); } @@ -62,25 +74,19 @@ function useUnlinkFID(): () => Promise { const { identityClient } = identityClientContext; const { unlinkFarcasterAccount } = identityClient; - const dispatch = useDispatch(); + const setLocalFID = useSetLocalFID(); return React.useCallback(async () => { await unlinkFarcasterAccount(); - - dispatch({ - type: setSyncedMetadataEntryActionType, - payload: { - name: syncedMetadataNames.CURRENT_USER_FID, - data: NO_FID_METADATA, - }, - }); - }, [dispatch, unlinkFarcasterAccount]); + setLocalFID(null); + }, [setLocalFID, unlinkFarcasterAccount]); } export { DISABLE_CONNECT_FARCASTER_ALERT, NO_FID_METADATA, useCurrentUserFID, + useSetLocalFID, useLinkFID, useUnlinkFID, }; diff --git a/native/account/registration/registration-server-call.js b/native/account/registration/registration-server-call.js index e03ceed273..204dff382e 100644 --- a/native/account/registration/registration-server-call.js +++ b/native/account/registration/registration-server-call.js @@ -23,7 +23,7 @@ import { } from 'lib/types/account-types.js'; import { syncedMetadataNames } from 'lib/types/synced-metadata-types.js'; import { getMessageForException } from 'lib/utils/errors.js'; -import { NO_FID_METADATA } from 'lib/utils/farcaster-utils.js'; +import { useSetLocalFID } 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'; @@ -302,6 +302,7 @@ function useRegistrationServerCall(): RegistrationServerCallInput => Promise new Promise( @@ -357,14 +358,7 @@ function useRegistrationServerCall(): RegistrationServerCallInput => Promise Promise