Skip to content

Commit

Permalink
move loginController inside the useEffect (#8809)
Browse files Browse the repository at this point in the history
* move loginController inside the useEffect

* swap to regular abort controller
  • Loading branch information
grahamlangford authored Jul 11, 2024
1 parent cb55136 commit 2672805
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/contrib/google/sheets/core/useGoogleAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,29 @@ import { useContext, useEffect } from "react";
import ModIntegrationsContext from "@/mods/ModIntegrationsContext";
import { validateRegistryId } from "@/types/helpers";
import reportError from "@/telemetry/reportError";
import { ReusableAbortController } from "abort-utils";
import { oauth2Storage } from "@/auth/authConstants";
import { isEmpty } from "lodash";

const GOOGLE_PKCE_INTEGRATION_ID = validateRegistryId("google/oauth2-pkce");
const loginController = new ReusableAbortController();

function useGoogleAccountLoginListener({
data: googleAccount,
refetch,
}: FetchableAsyncState<SanitizedIntegrationConfig | null>) {
useEffect(() => {
const loginController = new AbortController();

// Automatically refetch the google account on login
oauth2Storage.onChanged((newValue) => {
const { id } = googleAccount ?? {};
// eslint-disable-next-line security/detect-object-injection -- not user provided
if (id && !isEmpty(newValue[id])) {
refetch();
loginController.abortAndReset();
}
}, loginController.signal);

return () => {
loginController.abortAndReset();
loginController.abort();
};
}, [googleAccount, refetch]);
}
Expand Down

0 comments on commit 2672805

Please sign in to comment.