Skip to content

Commit

Permalink
fix(react-account): triggering a mint character causes another mint m…
Browse files Browse the repository at this point in the history
…odal to appear
  • Loading branch information
runjuu committed Aug 23, 2023
1 parent 74711f6 commit e5cc6e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export function createAccountTypeBasedMutationHooks<
actionDesc,
withParams,
connectType,
mustHaveCharacter,
}: {
actionDesc: string;
withParams: Params extends void ? false : true;
connectType?: UseConnectedActionOptions["connectType"];
mustHaveCharacter?: false;
},
useFactory: AccountTypeBasedHooksFactory<Params, Variables, Data>,
): AccountTypeBasedMutationHooks<Params, Variables, Data> {
Expand Down Expand Up @@ -140,12 +142,14 @@ export function createAccountTypeBasedMutationHooks<
connectType,
noAutoResume: options?.noAutoResume,
supportOPSign: factory.wallet?.supportOPSign,
mustHaveCharacter,
});

const mutateAsync = useConnectedAction(mutation.mutateAsync, {
connectType,
noAutoResume: options?.noAutoResume,
supportOPSign: factory.wallet?.supportOPSign,
mustHaveCharacter,
});

return { ...mutation, mutate, mutateAsync } as typeof mutation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const useMintCharacter = createAccountTypeBasedMutationHooks<
actionDesc: "mint-character",
withParams: false,
connectType: "wallet",
mustHaveCharacter: false,
},
() => {
const createCharacter = useCreateCharacter();
Expand Down
13 changes: 11 additions & 2 deletions packages/react-account/src/hooks/use-connected-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ConnectType = "wallet" | "email" | "any";
export type UseConnectedActionOptions<P extends any[] = unknown[], V = void> = {
noAutoResume?: boolean;
supportOPSign?: boolean;
mustHaveCharacter?: boolean;
connectType?: ConnectType;
fallback?: (...params: P) => V;
};
Expand All @@ -39,10 +40,15 @@ export function useConnectedAction<P extends any[], V>(
connectType = "any",
noAutoResume = false,
supportOPSign = false,
mustHaveCharacter = true,
fallback,
}: UseConnectedActionOptions<P, V> = {},
): (...params: P) => Promise<V> {
const checkIsConnected = useCheckIsConnected({ connectType, supportOPSign });
const checkIsConnected = useCheckIsConnected({
connectType,
supportOPSign,
mustHaveCharacter,
});
const { autoResume, resetAutoResume } = useAutoResume({
noAutoResume,
checkIsConnected,
Expand Down Expand Up @@ -113,9 +119,11 @@ export function useConnectedAction<P extends any[], V>(
function useCheckIsConnected({
connectType: type,
supportOPSign,
mustHaveCharacter,
}: {
connectType: ConnectType;
supportOPSign: boolean;
mustHaveCharacter: boolean;
}) {
const isWalletConnected = !!useAddress();
const character = useAccountCharacter();
Expand All @@ -125,7 +133,8 @@ function useCheckIsConnected({
const state = useAccountState.getState();
const isEmailAccountConnected = !!state.email;
const isWalletAccountConnected = ((): boolean => {
if (state.email || !state.wallet?.characterId) return false;
if (state.email) return false;
if (mustHaveCharacter && !state.wallet?.characterId) return false;

if (supportOPSign && isOpSignEnabled) {
return true;
Expand Down

0 comments on commit e5cc6e9

Please sign in to comment.