Skip to content

Commit

Permalink
[lib] Factor code out for updating FID in Redux
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ashoat committed Oct 17, 2024
1 parent 2ba2d1a commit 3c1f9f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
19 changes: 8 additions & 11 deletions lib/components/farcaster-data-handler.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 ===
Expand Down Expand Up @@ -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);
Expand All @@ -198,7 +195,7 @@ function FarcasterDataHandler(props: Props): React.Node {
neynarClient,
fid,
unlinkFID,
dispatch,
setLocalFID,
getCachedUserIdentity,
]);

Expand Down
46 changes: 26 additions & 20 deletions lib/utils/farcaster-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,40 @@ 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<void> {
const identityClientContext = React.useContext(IdentityClientContext);
invariant(identityClientContext, 'identityClientContext should be set');

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],
);
}

Expand All @@ -62,25 +74,19 @@ function useUnlinkFID(): () => Promise<void> {
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,
};
13 changes: 4 additions & 9 deletions native/account/registration/registration-server-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -302,6 +302,7 @@ function useRegistrationServerCall(): RegistrationServerCallInput => Promise<voi
);

const dispatch = useDispatch();
const setLocalFID = useSetLocalFID();
const returnedFunc = React.useCallback(
(input: RegistrationServerCallInput) =>
new Promise<void>(
Expand Down Expand Up @@ -357,14 +358,7 @@ function useRegistrationServerCall(): RegistrationServerCallInput => Promise<voi
payload: passedKeyserverURL,
});
}
const fidToSave = farcasterID ?? NO_FID_METADATA;
dispatch({
type: setSyncedMetadataEntryActionType,
payload: {
name: syncedMetadataNames.CURRENT_USER_FID,
data: fidToSave,
},
});
setLocalFID(farcasterID);
if (siweBackupSecrets) {
await commCoreModule.setSIWEBackupSecrets(siweBackupSecrets);
}
Expand Down Expand Up @@ -407,6 +401,7 @@ function useRegistrationServerCall(): RegistrationServerCallInput => Promise<voi
legacyKeyserverRegisterEthereumAccount,
identityRegisterEthereumAccount,
dispatch,
setLocalFID,
],
);

Expand Down

0 comments on commit 3c1f9f7

Please sign in to comment.