Skip to content

Commit

Permalink
[lib] Avoid useless updateRelationships call when logged out in handl…
Browse files Browse the repository at this point in the history
…eFarcasterMutuals

Summary: This addresses [ENG-9229](https://linear.app/comm/issue/ENG-9229/not-logged-in-when-calling-updaterelationships).

Test Plan: We use a similar strategy [here](https://github.com/CommE2E/comm/blob/7488a4e6fc3a832b2ad7f54df401e04935b981d7/lib/keyserver-conn/recovery-utils.js#L164-L171), which has been tested. This change is safe because it's simple and is using an approach I've tested before

Reviewers: will, varun

Reviewed By: varun

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D13298
  • Loading branch information
Ashoat committed Sep 12, 2024
1 parent 29f2689 commit a7e8494
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/components/farcaster-data-handler.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ function FarcasterDataHandler(props: Props): React.Node {

const prevCanQueryRef = React.useRef<?boolean>();

// It's possible for the user to log out while handleFarcasterMutuals below
// is running. It's not a big deal, but this can lead to a useless server call
// at the end. To avoid that useless server call, we want to check whether the
// user is logged in beforehand, but the value of loggedIn bound in the
// callback will be outdated. Instead, we can check this ref, which will be
// updated on every render.
const loggedInRef = React.useRef(loggedIn);
loggedInRef.current = loggedIn;

const handleFarcasterMutuals = React.useCallback(async () => {
const canQuery = isActive && !!fid && loggedIn;
if (canQuery === prevCanQueryRef.current) {
Expand Down Expand Up @@ -100,6 +109,10 @@ function FarcasterDataHandler(props: Props): React.Node {
[(currentUserID: string)]: fid,
};

if (!loggedInRef.current) {
return;
}

void dispatchActionPromise(
updateRelationshipsActionTypes,
createThreadsAndRobotextForFarcasterMutuals(
Expand Down

0 comments on commit a7e8494

Please sign in to comment.